Spring MVC 表单控制器实现验证用户登录验证
2015-01-29 20:35
459 查看
转自:http://blog.csdn.net/happyunbound/article/details/8236106
web.xml:
[html] view
plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<!-- 定义Servlet名称 -->
<servlet-name>dispatcherServlet</servlet-name>
<!-- Servlet具体实现类 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化上下文对象 -->
<init-param>
<!-- 参数名称 -->
<param-name>contextConfigLocation</param-name>
<!-- 加载配置文件 -->
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<!-- 设置启动的优先级 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 采用通配符映射所有以html类型的请求 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
applicationContext.xml:
[html] view
plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">
<!-- 表单控制器 -->
<bean name="/userLogin.html" class="com.jwy.controller.UserLoginController">
<property name="commandClass">
<value>com.jwy.controller.User</value>
</property>
<!-- 输入表单数据页面 -->
<property name="formView">
<value>index.jsp</value>
</property>
<!-- 表单提交后转入页面 -->
<property name="successView">
<value>login.jsp</value>
</property>
</bean>
</beans>
index.jsp:
[html] view
plaincopy
<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件名映射控制器</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
<!--
.STYLE3 {
font-size: 9pt
}
-->
</style>
</head>
<body>
<center>
<span class="STYLE3">用户登录</span>
</center>
<form method="post" action="userLogin.html">
<center>${map.error }</center>
<table align="center">
<tr>
<td height="23"><span class="STYLE3">输入用户名:</span></td>
<td height="23"><input name="userName" type="text"></td>
</tr>
<tr>
<td height="23"><span class="STYLE3">输入密码:</span></td>
<td height="23"><input name="userPwd" type="password"></td>
</tr>
<tr>
<td height="23" colspan="2" align="center">
<input type="submit" value="登录">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>
User.java:
[java] view
plaincopy
package com.jwy.controller;
/**
*
* @author Jingweiyu
*/
public class User {
private String userName;
private String userPwd;
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the userPwd
*/
public String getUserPwd() {
return userPwd;
}
/**
* @param userPwd the userPwd to set
*/
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
}
UserLoginController.java: map.put("user", user);
[java] view
plaincopy
package com.jwy.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
/**
*
* @author Jingweiyu
*/
public class UserLoginController extends SimpleFormController {
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(java.lang.Object)
*/
@Override
protected ModelAndView onSubmit(Object command) throws Exception {
User user = (User)command;
String usereName = user.getUserName();
String userPwd = user.getUserPwd();
Map map = new HashMap();
if("mr".equals(usereName)&&"mrsoft".equals(userPwd)){
map.put("user", user);
return new ModelAndView(getSuccessView(),"map",map);
}else{
map.put("error", "用户名或密码不正确,请重新输入!");
return new ModelAndView(getFormView(),"map",map);
}
}
}
login.html:
[html] view
plaincopy
<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户登录</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
<!--
.STYLE2 {
font-size: 9pt
}
-->
</style>
</head>
<body><br><br>
<center>
系统登录成功<br><br><br>${map.user.userName},欢迎光临!
</center>
</body>
</html>
web.xml:
[html] view
plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<!-- 定义Servlet名称 -->
<servlet-name>dispatcherServlet</servlet-name>
<!-- Servlet具体实现类 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化上下文对象 -->
<init-param>
<!-- 参数名称 -->
<param-name>contextConfigLocation</param-name>
<!-- 加载配置文件 -->
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<!-- 设置启动的优先级 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 采用通配符映射所有以html类型的请求 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
applicationContext.xml:
[html] view
plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">
<!-- 表单控制器 -->
<bean name="/userLogin.html" class="com.jwy.controller.UserLoginController">
<property name="commandClass">
<value>com.jwy.controller.User</value>
</property>
<!-- 输入表单数据页面 -->
<property name="formView">
<value>index.jsp</value>
</property>
<!-- 表单提交后转入页面 -->
<property name="successView">
<value>login.jsp</value>
</property>
</bean>
</beans>
index.jsp:
[html] view
plaincopy
<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件名映射控制器</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
<!--
.STYLE3 {
font-size: 9pt
}
-->
</style>
</head>
<body>
<center>
<span class="STYLE3">用户登录</span>
</center>
<form method="post" action="userLogin.html">
<center>${map.error }</center>
<table align="center">
<tr>
<td height="23"><span class="STYLE3">输入用户名:</span></td>
<td height="23"><input name="userName" type="text"></td>
</tr>
<tr>
<td height="23"><span class="STYLE3">输入密码:</span></td>
<td height="23"><input name="userPwd" type="password"></td>
</tr>
<tr>
<td height="23" colspan="2" align="center">
<input type="submit" value="登录">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>
User.java:
[java] view
plaincopy
package com.jwy.controller;
/**
*
* @author Jingweiyu
*/
public class User {
private String userName;
private String userPwd;
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the userPwd
*/
public String getUserPwd() {
return userPwd;
}
/**
* @param userPwd the userPwd to set
*/
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
}
UserLoginController.java: map.put("user", user);
[java] view
plaincopy
package com.jwy.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
/**
*
* @author Jingweiyu
*/
public class UserLoginController extends SimpleFormController {
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(java.lang.Object)
*/
@Override
protected ModelAndView onSubmit(Object command) throws Exception {
User user = (User)command;
String usereName = user.getUserName();
String userPwd = user.getUserPwd();
Map map = new HashMap();
if("mr".equals(usereName)&&"mrsoft".equals(userPwd)){
map.put("user", user);
return new ModelAndView(getSuccessView(),"map",map);
}else{
map.put("error", "用户名或密码不正确,请重新输入!");
return new ModelAndView(getFormView(),"map",map);
}
}
}
login.html:
[html] view
plaincopy
<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户登录</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
<!--
.STYLE2 {
font-size: 9pt
}
-->
</style>
</head>
<body><br><br>
<center>
系统登录成功<br><br><br>${map.user.userName},欢迎光临!
</center>
</body>
</html>
相关文章推荐
- Spring MVC 表单控制器实现验证用户登录验证
- 用Spring MVC实现用户登录的完整实例
- 在Forms验证模式下,实现多个站点(SubDomain相同)共享同一用户登录状态
- ASP.NET jQuery 食谱11 (通过使用jQuery validation插件简单实现用户登录页面验证功能)
- ASP.NET jQuery 实例11 通过使用jQuery validation插件简单实现用户登录页面验证功能
- 使用AjaxPro框架实现无刷新用户登录验证【原创】
- 在PHP中利用LDAP通过活动目录(Active Directory)实现域用户登录验证的办法
- 三层架构实战篇——抽象工厂+反射实现验证用户登录!
- 利用Servlet和jsp实现客户端与服务器端的用户登录信息验证
- RHCE_LAB(2)SSH远程登录自动验证(不输入用户登录密码)的实现
- 使用AjaxPro框架实现无刷新用户登录验证【原创】
- asp.net继承page类重写方法 实现最基本的用户登录验证 权限验证等
- 在Forms验证模式下,实现多个站点(SubDomain相同)共享同一用户登录状态
- 三层架构实战篇——抽象工厂+反射实现验证用户登录!
- 在Forms验证模式下,实现多个站点(SubDomain相同)共享同一用户登录状态
- flex3+struts1.3 简单实现用户登录验证
- 使用Servlet过滤器实现用户登录验证
- 使用Servlet过滤器实现用户登录验证
- [Asp.Net MVC4]验证用户登录实现
- 三层架构实战篇——抽象工厂+反射实现验证用户登录!