create.html
<!DOCTYPE html>
<html><head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="create/user" method="post">
<#import "spring.ftl" as spring />
用户名 :<input type="text" name="userName" value="${userDetail.userName!}" />
<div style="color:red;display:inline">
<#if userDetail??>
<@spring.bind "userDetail.userName" />
<@spring.showErrors "<br>"/>
<br/>
</#if>
</div>
密码 :<input type="text" name="password" value="${userDetail.password!}" />
<div style="color:red;display:inline">
<#if userDetail??>
<@spring.bind "userDetail.password" />
<@spring.showErrors "<br>"/>
<br/>
</#if>
</div>
身份证号:<input type="text" name="id" value="${userDetail.id!}" />
<div style="color:red;display:inline">
<#if userDetail??>
<@spring.bind "userDetail.id" />
<@spring.showErrors "<br>"/>
<br/>
</#if>
</div>
<input type="submit" value="提交"/>
</form>
</body>
</html>
UserController.java
package com.lovo.controller;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.lt.model.User;
@Controller
public class UserController {
@RequestMapping(value = "create/user", method = RequestMethod.POST)
public String doCreateUser(
@Valid @ModelAttribute("userDetail") User user,
BindingResult bindingResult,
HttpServletRequest request) {
// 如果有校验错误,返回添加用户的页面
if (bindingResult.hasErrors()) {
return "create";
}
if (isExistUserName(user.getUserName())) {
bindingResult.rejectValue("userName", "用户名已经存在","用户名已经存在");
return "create";
}
return "UserInfo";
}
@RequestMapping(value = "create.html", method = RequestMethod.GET)
public String fCreateUser(
@ModelAttribute("userDetail") User user,
HttpServletRequest request) {
return "create";
}
public boolean isExistUserName(String userName){
if (userName.equals("liutao")) {
return true;
}else {
return false;
}
}
}
User.java
package com.lt.model;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Length;
public class User {
@Length(min = 5, max = 20, message = "用户名长度必须位于5到20之间")
private String userName;
@Pattern(regexp="\\S{6,25}",message="密码为6到25个非空白字符")
private String password;
@Pattern(regexp="\\d{18}",message="身份证号码为18位数字")
private String id;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/util
http://www.springframework.org/schema/util/spring-util-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
>
<!-- 开启SpringMVC注解驱动 -->
<mvc:annotation-driven/>
<!-- 自动扫描Bean -->
<context:component-scan base-package="com.lovo"/>
<!-- 配置处理器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- 配置Controller处理器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 配置freeMarker的模板路径 -->
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<!-- freemarker视图解析器 -->
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="suffix" value=".html" />
<property name="contentType" value="text/html;charset=UTF-8" />
<!-- 此变量值为pageContext.request, 页面使用方法:rc.contextPath -->
<property name="requestContextAttribute" value="rc" />
</bean>
<!-- 配置访问静态资源 -->
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:default-servlet-handler/>
</beans>