菜鸟级springmvc+spring+mybatis整合开发用户登录功能(下)
昨天介紹了mybatis與spring的整合,今天我們完成剩下的springmvc的整合工作。
要整合springmvc首先得在web.xml中配置springmvc的前端控制器DispatcherServlet,它是springmvc的核心,為springmvc提供集中訪(fǎng)問(wèn)點(diǎn),springmvc對(duì)頁(yè)面的分派與調(diào)度功能主要靠它完成。
在我們之前配置的web.xml中加入以下springmvc的配置:
web.xml
1 <!-- Spring MVC 核心控制器 DispatcherServlet 配置 --> 2 <servlet> 3 <servlet-name>dispatcher</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 <init-param> 6 <!--用于標(biāo)明spring-mvc.xml配置的位置,我是存放在config文件夾下--> 7 <param-name>contextConfigLocation</param-name> 8 <param-value>classpath*:config/spring-mvc.xml</param-value> 9 </init-param> 10 <load-on-startup>1</load-on-startup> 11 </servlet> 12 <servlet-mapping> 13 <servlet-name>dispatcher</servlet-name> 14 <!-- 攔截所有*.do 的請(qǐng)求,交給DispatcherServlet處理,性能最好 --> 15 <url-pattern>*.do</url-pattern> 16 </servlet-mapping> 17 <!--用于設(shè)定默認(rèn)首頁(yè)--> 18 <welcome-file-list> 19 <welcome-file>login.jsp</welcome-file> 20 </welcome-file-list>配置完后,我們需要在對(duì)springmvc框架進(jìn)行配置,配置文件名為spring-mvc.xml,也是存放在config文件夾下:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:context="http://www.springframework.org/schema/context" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-3.0.xsd 9 http://www.springframework.org/schema/mvc 10 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 11 12 <!--掃描控制器,當(dāng)配置了它后,Spring會(huì)自動(dòng)的到com.mjl.controller 13 下掃描帶有@controller @service @component等注解等類(lèi),將他們自動(dòng)實(shí)例化--> 14 <context:component-scan base-package="com.mjl.controller" /> 15 16 <!--<mvc:annotation-driven /> 會(huì)自動(dòng)注冊(cè)DefaultAnnotationHandlerMapping與 17 AnnotationMethodHandlerAdapter 兩個(gè)bean,它解決了一些@controllerz注解使用時(shí)的提前配置--> 18 <mvc:annotation-driven /> 19 20 <!--配置 頁(yè)面控制器--> 21 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 22 <property name="prefix" value="/"/> 23 <property name="suffix" value=".jsp" /> 24 25 </bean> 26 27 </beans>當(dāng)springmvc配置完成后,就需要編寫(xiě)業(yè)務(wù)啦,也就是service包下的東西,首先編寫(xiě)一個(gè)接口類(lèi)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)類(lèi)userserviceimpl用于實(shí)現(xiàn)我們所抽象出來(lái)的登錄方法
1 package com.mjl.service; 2 3 import com.mjl.dao.IUserDao; 4 import com.mjl.model.User; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.context.annotation.Scope; 7 import org.springframework.stereotype.Service; 8 import org.springframework.ui.Model; 9 10 /** 11 * Created by alvin on 15/9/7. 12 */ 13 //@Service("UserService") 注解用于標(biāo)示此類(lèi)為業(yè)務(wù)層組件,在使用時(shí)會(huì)被注解的類(lèi)會(huì)自動(dòng)由 14 //spring進(jìn)行注入,無(wú)需我們創(chuàng)建實(shí)例 15 @Service("UserService") 16 public class UserServiceImpl implements UserService { 17 //自動(dòng)注入iuserdao 用于訪(fǎng)問(wèn)數(shù)據(jù)庫(kù) 18 @Autowired 19 IUserDao Mapper; 20 21 //登錄方法的實(shí)現(xiàn),從jsp頁(yè)面獲取username與password 22 public boolean login(String username, String password) { 23 // System.out.println("輸入的賬號(hào):" + username + "輸入的密碼:" + password); 24 //對(duì)輸入賬號(hào)進(jìn)行查詢(xún),取出數(shù)據(jù)庫(kù)中保存對(duì)信息 25 User user = Mapper.selectByName(username); 26 if (user != null) { 27 // System.out.println("查詢(xún)出來(lái)的賬號(hào):" + user.getUsername() + "密碼:" + user.getPassword()); 28 // System.out.println("---------"); 29 if (user.getUsername().equals(username) && user.getPassword().equals(password)) 30 return true; 31 32 } 33 return false; 34 35 } 36 }編寫(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)示本類(lèi)為web層控制組件 @Controller //@RequestMapping("/user")用于標(biāo)定訪(fǎng)問(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類(lèi) @AutowiredUserService userService;//login業(yè)務(wù)的訪(fǎng)問(wèn)位置為/user/login@RequestMapping("/login")public String login(User user,HttpServletRequest request){//調(diào)用login方法來(lái)驗(yàn)證是否是注冊(cè)用戶(hù)boolean loginType = userService.login(user.getUsername(),user.getPassword());if(loginType){//如果驗(yàn)證通過(guò),則將用戶(hù)信息傳到前臺(tái)request.setAttribute("user",user);//并跳轉(zhuǎn)到success.jsp頁(yè)面return "success";}else{//若不對(duì),則將錯(cuò)誤信息顯示到錯(cuò)誤頁(yè)面request.setAttribute("message","用戶(hù)名密碼錯(cuò)誤");return "error";}}}控制層代碼寫(xiě)完后,就可以進(jìn)行前端頁(yè)面代碼編寫(xiě)了,登錄代碼
1 <%-- 2 Created by IntelliJ IDEA. 3 User: alvin 4 Date: 15/9/7 5 Time: 下午10:05 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <% 10 String path = request.getContextPath(); 11 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 12 %> 13 <html> 14 <head> 15 <title></title> 16 </head> 17 <body> 18 <br> 19 <br> 20 <br> 21 <br> 22 <br> 23 <form name="form1" action="/user/login.do" method="post" > 24 <table width="300" border="1" align="center"> 25 <tr> 26 <td colspan="2">登入窗口</td> 27 </tr> 28 <tr> 29 <td>用戶(hù)名:</td> 30 <td><input type="text" name="username"> 31 </td> 32 </tr> 33 <tr> 34 <td>密碼:</td> 35 <td><input type="password" name="password"/> 36 </td> 37 </tr> 38 <tr> 39 <td colspan="2"> 40 <input type="submit" name="submit" value="登錄"/> 41 </td> 42 43 44 </tr> 45 </table> 46 </form> 47 </body> 48 </html>登入成功代碼?
1 <%-- 2 Created by IntelliJ IDEA. 3 User: alvin 4 Date: 15/9/8 5 Time: 下午6:21 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <% 10 String path = request.getContextPath(); 11 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 12 %> 13 <html> 14 <head> 15 <title></title> 16 </head> 17 <body> 18 19 登入成功! 20 <br> 21 您好!${user.username} 22 <br> 23 <a href="/login.jsp">返回</a> 24 </body> 25 </html>登入失敗代碼?
1 <%-- 2 Created by IntelliJ IDEA. 3 User: alvin 4 Date: 15/9/8 5 Time: 下午6:22 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <% 10 String path = request.getContextPath(); 11 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 12 %> 13 <html> 14 <head> 15 <title></title> 16 </head> 17 <body> 18 登入失敗! 19 ${message} 20 <br> 21 <a href="<%=path%>/login.jsp">返回</a> 22 </body> 23 </html>OK,已經(jīng)大功告成,跑一遍看看能不能使用吧?
若我輸入用戶(hù)名:1234 密碼:1234 則會(huì)提示登錄失敗,如下圖所示:
到這里,本文已經(jīng)全部結(jié)束,希望能對(duì)在整合springmvc,spring,my baits框架時(shí)有困惑的同學(xué)有所幫助,本文的代碼已經(jīng)上傳github,地址為:http://git@github.com:alvin-mei/testssm.git?以后我也會(huì)慢慢的增加功能,也會(huì)上傳相關(guān)代碼,希望大家能夠共同進(jìn)步!
轉(zhuǎn)載于:https://www.cnblogs.com/alivn/p/4796017.html
總結(jié)
以上是生活随笔為你收集整理的菜鸟级springmvc+spring+mybatis整合开发用户登录功能(下)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。