您的位置:首页 > 编程语言 > Java开发

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

2012-11-28 20:36 597 查看


web.xml:

<?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:

<?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:

<%@ 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:

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);

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:

<%@ 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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: