springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(下)
昨天介紹了mybatis與spring的整合,今天我們完成剩下的springmvc的整合工作。
要整合springmvc首先得在web.xml中配置springmvc的前端控制器DispatcherServlet,它是springmvc的核心,為springmvc提供集中訪問(wèn)點(diǎn),springmvc對(duì)頁(yè)面的分派與調(diào)度功能主要靠它完成。
在我們之前配置的web.xml中加入以下springmvc的配置:
web.xml
<!-- 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 的請(qǐng)求,交給DispatcherServlet處理,性能最好 --> <url-pattern>*.do</url-pattern> </servlet-mapping> <!--用于設(shè)定默認(rèn)首頁(yè)--> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list>
配置完后,我們需要在對(duì)springmvc框架進(jìn)行配置,配置文件名為spring-mvc.xml,也是存放在config文件夾下:
<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會(huì)自動(dòng)的到com.mjl.controller 下掃描帶有@controller @service @component等注解等類,將他們自動(dòng)實(shí)例化--> <context:component-scan base-package="com.mjl.controller" /> <!--<mvc:annotation-driven /> 會(huì)自動(dòng)注冊(cè)DefaultAnnotationHandlerMapping與 AnnotationMethodHandlerAdapter 兩個(gè)bean,它解決了一些@controllerz注解使用時(shí)的提前配置--> <mvc:annotation-driven /> <!--配置 頁(yè)面控制器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp" /> </bean> </beans>
當(dāng)springmvc配置完成后,就需要編寫(xiě)業(yè)務(wù)啦,也就是service包下的東西,首先編寫(xiě)一個(gè)接口類userservice,里面存放了我們抽象出來(lái)的登錄方法login
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)建一個(gè)userservice的實(shí)現(xiàn)類userserviceimpl用于實(shí)現(xiàn)我們所抽象出來(lái)的登錄方法
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ù)層組件,在使用時(shí)會(huì)被注解的類會(huì)自動(dòng)由 //spring進(jìn)行注入,無(wú)需我們創(chuàng)建實(shí)例 @Service("UserService") public class UserServiceImpl implements UserService { //自動(dòng)注入iuserdao 用于訪問(wèn)數(shù)據(jù)庫(kù) @Autowired IUserDao Mapper; //登錄方法的實(shí)現(xiàn),從jsp頁(yè)面獲取username與password public boolean login(String username, String password) { // System.out.println("輸入的賬號(hào):" + username + "輸入的密碼:" + password); //對(duì)輸入賬號(hào)進(jìn)行查詢,取出數(shù)據(jù)庫(kù)中保存對(duì)信息 User user = Mapper.selectByName(username); if (user != null) { // System.out.println("查詢出來(lái)的賬號(hào):" + user.getUsername() + "密碼:" + user.getPassword()); // System.out.println("---------"); if (user.getUsername().equals(username) && user.getPassword().equals(password)) return true; } return false; } }
編寫(xiě)完業(yè)務(wù)層代碼后,我們就可以寫(xiě)控制層代碼啦,控制層的代碼用于處理頁(yè)面提交的業(yè)務(wù)
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)定訪問(wèn)時(shí)對(duì)url位置 @RequestMapping("/user") //在默認(rèn)情況下springmvc的實(shí)例都是單例模式,所以使用scope域?qū)⑵渥⒔鉃槊看味紕?chuàng)建一個(gè)新的實(shí)例 @Scope("prototype") public class UserController { //自動(dòng)注入業(yè)務(wù)層的userService類 @Autowired UserService userService; //login業(yè)務(wù)的訪問(wèn)位置為/user/login @RequestMapping("/login") public String login(User user,HttpServletRequest request){ //調(diào)用login方法來(lái)驗(yàn)證是否是注冊(cè)用戶 boolean loginType = userService.login(user.getUsername(),user.getPassword()); if(loginType){ //如果驗(yàn)證通過(guò),則將用戶信息傳到前臺(tái) request.setAttribute("user",user); //并跳轉(zhuǎn)到success.jsp頁(yè)面 return "success"; }else{ //若不對(duì),則將錯(cuò)誤信息顯示到錯(cuò)誤頁(yè)面 request.setAttribute("message","用戶名密碼錯(cuò)誤"); return "error"; } } }
控制層代碼寫(xiě)完后,就可以進(jìn)行前端頁(yè)面代碼編寫(xiě)了,登錄代碼
<%-- 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>
登入成功代碼
<%-- 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>
登入失敗代碼
<%-- 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)大功告成,跑一遍看看能不能使用吧
若我輸入用戶名:1234 密碼:1234 則會(huì)提示登錄失敗,如下圖所示:
到這里,本文已經(jīng)全部結(jié)束,希望能對(duì)在整合springmvc,spring,my baits框架時(shí)有困惑的同學(xué)有所幫助,本文的代碼已經(jīng)上傳github,以后我也會(huì)慢慢的增加功能,也會(huì)上傳相關(guān)代碼,希望大家能夠共同進(jìn)步!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springsecurity實(shí)現(xiàn)用戶登錄認(rèn)證快速使用示例代碼(前后端分離項(xiàng)目)
- Springboot+Shiro記錄用戶登錄信息并獲取當(dāng)前登錄用戶信息的實(shí)現(xiàn)代碼
- Spring mvc 實(shí)現(xiàn)用戶登錄的方法(攔截器)
- spring aop action中驗(yàn)證用戶登錄狀態(tài)的實(shí)例代碼
- springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(上)
- SpringMvc實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
- Spring實(shí)現(xiàn)加法計(jì)算器和用戶登錄功能
相關(guān)文章
java調(diào)用webservice接口,并解析返回參數(shù)問(wèn)題
這篇文章主要介紹了java調(diào)用webservice接口,并解析返回參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07利用Java+MySQL實(shí)現(xiàn)附近功能實(shí)例
現(xiàn)在很多手機(jī)軟件都用附近搜索功能,但具體是怎么實(shí)現(xiàn)的呢?下面這篇文章就來(lái)給大家介紹關(guān)于利用Java+MySQL實(shí)現(xiàn)附近功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-12-12Request的包裝類HttpServletRequestWrapper的使用說(shuō)明
這篇文章主要介紹了Request的包裝類HttpServletRequestWrapper的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08IntelliJ IDEAx導(dǎo)出安卓(Android)apk文件圖文教程
這篇文章主要為大家詳細(xì)介紹了IntelliJ IDEAx導(dǎo)出安卓(Android)apk文件圖文教程,文中步驟介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10Spring整合MyBatis(Maven+MySQL)圖文教程詳解
這篇文章主要介紹了Spring整合MyBatis(Maven+MySQL)圖文教程詳解的相關(guān)資料,需要的朋友可以參考下2016-07-07基于SpringBoot和Vue3的博客平臺(tái)文章列表與分頁(yè)功能實(shí)現(xiàn)
在前面的教程中,我們已經(jīng)實(shí)現(xiàn)了基于Spring Boot和Vue3的發(fā)布、編輯、刪除文章功能。本教程將繼續(xù)引導(dǎo)您實(shí)現(xiàn)博客平臺(tái)的文章列表與分頁(yè)功能,需要的朋友可以參考閱讀2023-04-04