Spring MVC 表单控制器实现验证用户登录验证

来源:互联网 发布:手机单怎么预防淘宝客 编辑:程序博客网 时间:2024/05/18 21:09

转自:http://blog.csdn.net/happyunbound/article/details/8236106

web.xml:

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  5.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  6.     <servlet>  
  7.         <!-- 定义Servlet名称 -->  
  8.         <servlet-name>dispatcherServlet</servlet-name>  
  9.         <!-- Servlet具体实现类 -->  
  10.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  11.         <!-- 初始化上下文对象 -->  
  12.         <init-param>  
  13.             <!-- 参数名称 -->  
  14.             <param-name>contextConfigLocation</param-name>  
  15.             <!-- 加载配置文件 -->  
  16.             <param-value>/WEB-INF/applicationContext.xml</param-value>  
  17.         </init-param>  
  18.         <!-- 设置启动的优先级 -->  
  19.         <load-on-startup>1</load-on-startup>  
  20.     </servlet>  
  21.     <!-- 采用通配符映射所有以html类型的请求 -->  
  22.     <servlet-mapping>  
  23.         <servlet-name>dispatcherServlet</servlet-name>  
  24.         <url-pattern>*.html</url-pattern>  
  25.     </servlet-mapping>  
  26.     <welcome-file-list>  
  27.         <welcome-file>index.jsp</welcome-file>  
  28.     </welcome-file-list>  
  29. </web-app>  


 

applicationContext.xml:

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  6.     <!-- 表单控制器 -->  
  7.     <bean name="/userLogin.html" class="com.jwy.controller.UserLoginController">  
  8.         <property name="commandClass">  
  9.             <value>com.jwy.controller.User</value>  
  10.         </property>  
  11.         <!-- 输入表单数据页面 -->  
  12.         <property name="formView">  
  13.             <value>index.jsp</value>  
  14.         </property>  
  15.         <!-- 表单提交后转入页面 -->  
  16.         <property name="successView">  
  17.             <value>login.jsp</value>  
  18.         </property>  
  19.     </bean>  
  20. </beans>  


index.jsp:

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html" pageEncoding="GBK"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  3. <html>  
  4.     <head>  
  5.         <title>文件名映射控制器</title>  
  6.         <!-- 
  7.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  8.     -->  
  9.         <style type="text/css">  
  10. <!--  
  11. .STYLE3 {  
  12.     font-size: 9pt  
  13. }  
  14. -->  
  15. </style>  
  16. </head>  
  17.     
  18.   <body>  
  19.   <center>  
  20.     <span class="STYLE3">用户登录</span>  
  21.   </center>  
  22.     <form method="post" action="userLogin.html">  
  23.     <center>${map.error }</center>  
  24.     <table align="center">  
  25.         <tr>  
  26.             <td height="23"><span class="STYLE3">输入用户名:</span></td>  
  27.           <td height="23"><input name="userName" type="text"></td>  
  28.         </tr>  
  29.         <tr>  
  30.             <td height="23"><span class="STYLE3">输入密码:</span></td>  
  31.           <td height="23"><input name="userPwd" type="password"></td>  
  32.         </tr>  
  33.         <tr>  
  34.             <td height="23" colspan="2" align="center">  
  35.                 <input type="submit" value="登录">  
  36.                 <input type="reset" value="重置">  
  37.             </td>  
  38.         </tr>  
  39.     </table>  
  40.     </form>  
  41.   </body>  
  42. </html>  

 

User.java:

[java] view plaincopy
  1. package com.jwy.controller;  
  2.   
  3. /** 
  4.  *  
  5.  * @author Jingweiyu  
  6.  */  
  7. public class User {  
  8.     private String userName;  
  9.     private String userPwd;  
  10.     /** 
  11.      * @return the userName 
  12.      */  
  13.     public String getUserName() {  
  14.         return userName;  
  15.     }  
  16.     /** 
  17.      * @param userName the userName to set 
  18.      */  
  19.     public void setUserName(String userName) {  
  20.         this.userName = userName;  
  21.     }  
  22.     /** 
  23.      * @return the userPwd 
  24.      */  
  25.     public String getUserPwd() {  
  26.         return userPwd;  
  27.     }  
  28.     /** 
  29.      * @param userPwd the userPwd to set 
  30.      */  
  31.     public void setUserPwd(String userPwd) {  
  32.         this.userPwd = userPwd;  
  33.     }  
  34. }  


UserLoginController.java:            map.put("user", user);

[java] view plaincopy
  1. package com.jwy.controller;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import org.springframework.web.servlet.ModelAndView;  
  7. import org.springframework.web.servlet.mvc.SimpleFormController;  
  8.   
  9. /** 
  10.  *  
  11.  * @author Jingweiyu  
  12.  */  
  13. public class UserLoginController extends SimpleFormController {  
  14.   
  15.     /* (non-Javadoc) 
  16.      * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(java.lang.Object) 
  17.      */  
  18.     @Override  
  19.     protected ModelAndView onSubmit(Object command) throws Exception {  
  20.         User user = (User)command;  
  21.         String usereName = user.getUserName();  
  22.         String userPwd = user.getUserPwd();  
  23.         Map map = new HashMap();  
  24.         if("mr".equals(usereName)&&"mrsoft".equals(userPwd)){  
  25.             map.put("user", user);  
  26.             return new ModelAndView(getSuccessView(),"map",map);  
  27.         }else{  
  28.             map.put("error""用户名或密码不正确,请重新输入!");  
  29.             return new ModelAndView(getFormView(),"map",map);  
  30.         }  
  31.     }  
  32. }  


login.html:

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html" pageEncoding="GBK"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  3. <html>  
  4.     <head>  
  5.         <title>用户登录</title>  
  6.         <!-- 
  7.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  8.     -->  
  9.         <style type="text/css">  
  10. <!--  
  11. .STYLE2 {  
  12.     font-size: 9pt  
  13. }  
  14. -->  
  15. </style>  
  16.     </head>  
  17.   
  18.     <body><br><br>  
  19.         <center>  
  20.             系统登录成功<br><br><br>${map.user.userName},欢迎光临!  
  21.         </center>  
  22.     </body>  
  23. </html>  

0 0