项目结构图:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>userapps</groupId>
<artifactId>userapps</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>userapps</name>
<description>userapps</description>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.0.2.GA</version>
</dependency>
<!-- Other dependencies -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
</dependencies>
</project> index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="toAddUserPage.html">添加用户信息</a>
</body>
</html> web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>userapps</display-name>
<!-- 字符过滤_防止添加到数据库中的数据为乱码 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app> dispatcher-servlet.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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
">
<!-- 搜索的控制类路径(C) -->
<context:component-scan base-package="com.userapps" />
<!-- 配置视图路径(V) -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 异常解析器 -->
<bean id="simpleMappingExceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop
key="org.springframework.web.multipart.MaxUploadSizeExceededException">common/fileerror</prop>
</props>
</property>
</bean>
</beans>
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加用户</title>
</head>
<body>
<form:form method="Post" action="adduser.html" commandName="user">
<table>
<tr>
<td>用户名:<FONT color="red"><form:errors
path="userName" /></FONT></td>
</tr>
<tr>
<td><form:input path="userName" /></td>
</tr>
<tr>
<td>密码:<FONT color="red"><form:errors
path="password" /></FONT></td>
</tr>
<tr>
<td><form:password path="password" /></td>
</tr>
<tr>
<td>确认密码:<FONT color="red"><form:errors
path="confirmPassword" /></FONT></td>
</tr>
<tr>
<td><form:password path="confirmPassword" /></td>
</tr>
<tr>
<td>Email:<FONT color="red"><form:errors path="email" /></FONT></td>
</tr>
<tr>
<td><form:input path="email" /></td>
</tr>
<tr>
<td><input type="submit" value="提交" /></td>
</tr>
</table>
</form:form>
</body>
</html> addSuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>success page</title>
</head>
<body>
Congratulate,add Success!
</body>
</html> User.java
package com.userapps.user.form;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
public class User {
private String userName;
@NotEmpty
@Size(min = 4, max = 20)
private String password;
@NotEmpty
private String confirmPassword;
@NotEmpty
@Email
private String email;
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setConfirmPassword(String confirmPassword) {
this.confirmPassword = confirmPassword;
}
public String getConfirmPassword() {
return confirmPassword;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
}
UserValidation.java
package com.userapps.user.controllers;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import com.userapps.user.form.User;
@Component("userValidator")
public class UserValidation {
public boolean supports(Class<?> klass) {
return User.class.isAssignableFrom(klass);
}
public void validate(Object target, Errors errors) {
User registration = (User) target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName",
"NotEmpty.registration.userName",
"用户名不能为空.");
String userName = registration.getUserName();
if ((userName.length()) > 50) {
errors.rejectValue("userName",
"lengthOfUser.registration.userName",
"User Name must not more than 50 characters.");
}
if (!(registration.getPassword()).equals(registration
.getConfirmPassword())) {
errors.rejectValue("password",
"matchingPassword.registration.password",
"Password and Confirm Password Not match.");
}
}
}
UserManagerController.java
package com.userapps.user.controllers;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.userapps.user.form.User;
@Controller
public class UserManagerController {
@Autowired
private UserValidation userValidation; // 用户自定义验证
@RequestMapping(value="/toAddUserPage")
public String toAddUserPage(Map<String, User> model) {
User user = new User();
model.put("user", user);
return "user/add"; // 跳转到添加用户界面
}
@RequestMapping(value="/adduser", method = RequestMethod.POST)
public String processRegistration(@Valid User user,
BindingResult result) {
// set custom Validation by user
userValidation.validate(user, result);
if (result.hasErrors()) {
return "user/add"; //验证不通过,跳转回添加用户界面
}
return "user/addSuccess"; //验证通过,跳转到添加成功界面
}
}
附上源码:
/Files/svygh123/userapps.rar