国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - springmvc+spring+mybatis實現(xiàn)用戶登錄功能(下)

springmvc+spring+mybatis實現(xiàn)用戶登錄功能(下)

2020-11-30 14:59alvin-m Java教程

這篇文章主要為大家詳細(xì)介紹了springmvc+spring+mybatis實現(xiàn)用戶登錄功能的第二篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下

昨天介紹了mybatis與spring的整合,今天我們完成剩下的springmvc的整合工作。

要整合springmvc首先得在web.xml中配置springmvc的前端控制器DispatcherServlet,它是springmvc的核心,為springmvc提供集中訪問點,springmvc對頁面的分派與調(diào)度功能主要靠它完成。

在我們之前配置的web.xml中加入以下springmvc的配置:

web.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!-- Spring MVC 核心控制器 DispatcherServlet 配置 -->
 <servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <!--用于標(biāo)明spring-mvc.xml配置的位置,我是存放在config文件夾下-->
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath*:config/spring-mvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <!-- 攔截所有*.do 的請求,交給DispatcherServlet處理,性能最好 -->
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
  <!--用于設(shè)定默認(rèn)首頁-->
 <welcome-file-list>
  <welcome-file>login.jsp</welcome-file>
 </welcome-file-list>

配置完后,我們需要在對springmvc框架進(jìn)行配置,配置文件名為spring-mvc.xml,也是存放在config文件夾下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 
 <!--掃描控制器,當(dāng)配置了它后,Spring會自動的到com.mjl.controller
 下掃描帶有@controller @service @component等注解等類,將他們自動實例化-->
 <context:component-scan base-package="com.mjl.controller" />
 
 <!--<mvc:annotation-driven /> 會自動注冊DefaultAnnotationHandlerMapping與
 AnnotationMethodHandlerAdapter 兩個bean,它解決了一些@controllerz注解使用時的提前配置-->
 <mvc:annotation-driven />
 
 <!--配置 頁面控制器-->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/"/>
  <property name="suffix" value=".jsp" />
 
 </bean>
 
</beans>

當(dāng)springmvc配置完成后,就需要編寫業(yè)務(wù)啦,也就是service包下的東西,首先編寫一個接口類userservice,里面存放了我們抽象出來的登錄方法login

?
1
2
3
4
5
6
7
8
9
10
package com.mjl.service;
 
import org.springframework.ui.Model;
 
/**
 * Created by alvin on 15/9/7.
 */
public interface UserService {
 public boolean login(String username,String password);
}

然后在創(chuàng)建一個userservice的實現(xiàn)類userserviceimpl用于實現(xiàn)我們所抽象出來的登錄方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.mjl.service;
 
import com.mjl.dao.IUserDao;
import com.mjl.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
 
/**
 * Created by alvin on 15/9/7.
 */
//@Service("UserService") 注解用于標(biāo)示此類為業(yè)務(wù)層組件,在使用時會被注解的類會自動由
 //spring進(jìn)行注入,無需我們創(chuàng)建實例
@Service("UserService")
public class UserServiceImpl implements UserService {
 //自動注入iuserdao 用于訪問數(shù)據(jù)庫
 @Autowired
 IUserDao Mapper;
 
 //登錄方法的實現(xiàn),從jsp頁面獲取username與password
 public boolean login(String username, String password) {
//  System.out.println("輸入的賬號:" + username + "輸入的密碼:" + password);
  //對輸入賬號進(jìn)行查詢,取出數(shù)據(jù)庫中保存對信息
  User user = Mapper.selectByName(username);
  if (user != null) {
//   System.out.println("查詢出來的賬號:" + user.getUsername() + "密碼:" + user.getPassword());
//   System.out.println("---------");
   if (user.getUsername().equals(username) && user.getPassword().equals(password))
    return true;
 
  }
  return false;
 
 }
}

編寫完業(yè)務(wù)層代碼后,我們就可以寫控制層代碼啦,控制層的代碼用于處理頁面提交的業(yè)務(wù)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.mjl.controller;
 
import com.mjl.model.User;
import com.mjl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
import javax.servlet.http.HttpServletRequest;
 
 
/**
 * Created by alvin on 15/9/7.
 */
 
//@Controller注解用于標(biāo)示本類為web層控制組件
@Controller
//@RequestMapping("/user")用于標(biāo)定訪問時對url位置
@RequestMapping("/user")
//在默認(rèn)情況下springmvc的實例都是單例模式,所以使用scope域?qū)⑵渥⒔鉃槊看味紕?chuàng)建一個新的實例
@Scope("prototype")
public class UserController {
 //自動注入業(yè)務(wù)層的userService類
 @Autowired
  UserService userService;
 
 //login業(yè)務(wù)的訪問位置為/user/login
 @RequestMapping("/login")
  public String login(User user,HttpServletRequest request){
  //調(diào)用login方法來驗證是否是注冊用戶
  boolean loginType = userService.login(user.getUsername(),user.getPassword());
  if(loginType){
   //如果驗證通過,則將用戶信息傳到前臺
   request.setAttribute("user",user);
   //并跳轉(zhuǎn)到success.jsp頁面
   return "success";
  }else{
   //若不對,則將錯誤信息顯示到錯誤頁面
   request.setAttribute("message","用戶名密碼錯誤");
   return "error";
  }
 }
 
}

控制層代碼寫完后,就可以進(jìn)行前端頁面代碼編寫了,登錄代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<%--
 Created by IntelliJ IDEA.
 User: alvin
 Date: 15/9/7
 Time: 下午10:05
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
 <title></title>
</head>
<body>
<br>
<br>
<br>
<br>
<br>
<form name="form1" action="/user/login.do" method="post" >
 <table width="300" border="1" align="center">
 <tr>
 <td colspan="2">登入窗口</td>
 </tr>
 <tr>
  <td>用戶名:</td>
  <td><input type="text" name="username">
  </td>
 </tr>
 <tr>
  <td>密碼:</td>
  <td><input type="password" name="password"/>
  </td>
 </tr>
 <tr>
 <td colspan="2">
  <input type="submit" name="submit" value="登錄"/>
 </td>
 
 
 </tr>
 </table>
</form>
</body>
</html>

登入成功代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<%--
 Created by IntelliJ IDEA.
 User: alvin
 Date: 15/9/8
 Time: 下午6:21
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
 <title></title>
</head>
<body>
 
登入成功!
<br>
您好!${user.username}
<br>
<a href="/login.jsp" rel="external nofollow" >返回</a>
</body>
</html>

登入失敗代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%--
 Created by IntelliJ IDEA.
 User: alvin
 Date: 15/9/8
 Time: 下午6:22
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
 <title></title>
</head>
<body>
登入失敗!
${message}
<br>
<a href="<%=path%>/login.jsp" rel="external nofollow" >返回</a>
</body>
</html>

OK,已經(jīng)大功告成,跑一遍看看能不能使用吧

springmvc+spring+mybatis實現(xiàn)用戶登錄功能(下)

springmvc+spring+mybatis實現(xiàn)用戶登錄功能(下)

若我輸入用戶名:1234 密碼:1234 則會提示登錄失敗,如下圖所示:

springmvc+spring+mybatis實現(xiàn)用戶登錄功能(下)

到這里,本文已經(jīng)全部結(jié)束,希望能對在整合springmvc,spring,my baits框架時有困惑的同學(xué)有所幫助,本文的代碼已經(jīng)上傳github,以后我也會慢慢的增加功能,也會上傳相關(guān)代碼,希望大家能夠共同進(jìn)步!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 狠狠操一区二区三区 | 在线看的毛片 | 久久久久久久国产精品 | 亚洲成人中文字幕 | 精品一区二区三区免费毛片爱 | 国产日韩欧美精品 | 久草成人网 | 国产精品99久久久久久动医院 | 久久综合狠狠综合久久综合88 | 国产精品69毛片高清亚洲 | 91欧美激情一区二区三区成人 | 欧美日韩高清不卡 | 免费一级黄色毛片 | 成人亚洲 | 日韩一二三区视频 | 欧美一区二区久久久 | 国产最好的精华液网站 | 91精品国产综合久久香蕉的用户体验 | 免费观看国产精品 | 国产精品久久久久久久久福交 | 国产精品视频一区二区三区不卡 | 久久免费精品 | 欧美成人免费在线视频 | 成人精品| 久久久久久中文字幕 | 日日搞夜夜操 | 爱爱网址 | 91视频.com | 日本国产欧美 | 精品av| a国产在线观看 | 成人性生交大片免费网站 | 日韩午夜av | 亚洲2020天天堂在线观看 | 国产亚洲精品女人久久久久久 | 久久99精品久久久久久国产越南 | 国产欧美在线 | 色综合中文 | 国产大片一区 | 国产精品高潮呻吟久久 | 视频在线一区 |