通过示例学习,
然后举一返三,
是最快的吸收知识的方式。
web.xml和springmvc-config.xml文件的内容同前面。
这一系列使用eclipse建立java项目时,选择动态web项目即可。
User.java
package org.fkit.domain;
import java.io.Serializable;
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String username;
private String sex;
private Integer age;
public User() {
super();
}
public User(String username, String sex, Integer age) {
super();
this.username = username;
this.sex = sex;
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
UserValidator.java
package org.fkit.validator;
import org.fkit.domain.User;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
public class UserValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return User.class.equals(clazz);
}
@Override
public void validate(Object object, Errors errors) {
ValidationUtils.rejectIfEmpty(errors, "username", null, "用户名不能为空");
ValidationUtils.rejectIfEmpty(errors, "sex", null, "性别不能为空");
ValidationUtils.rejectIfEmpty(errors, "age", null, "年龄不能为空");
}
}
UserController.java
package org.fkit.controller;
import org.fkit.domain.User;
import org.fkit.validator.UserValidator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.DataBinder;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class UserController {
@RequestMapping(value="/registerForm", method=RequestMethod.GET)
public String registerForm(Model model) {
User user = new User();
model.addAttribute("user", user);
return "registerForm";
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.setValidator(new UserValidator());
}
@RequestMapping(value="/register", method=RequestMethod.POST)
public String register(@Validated User user, Errors errors) {
if (errors.hasFieldErrors())
return "registerForm";
return "helloWorld";
}
}
registerForm.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 content="text/html; charset=UTF-8" http-equiv="Content-Type">
<title>welcome</title>
</head>
<body>
<h3>注册页面</h3>
<br>
<form:form modelAttribute="user" action="register" method="post">
<table>
<tr>
<td><label>姓名: </label></td>
<td><form:input path="username"/></td>
<td><font color="red"><form:errors path="username" /></font></td>
</tr>
<tr>
<td><label>性别: </label></td>
<td><form:input path="sex"/></td>
<td><font color="red"><form:errors path="sex" /></font></td>
</tr>
<tr>
<td><label>年龄: </label></td>
<td><form:input path="age"/></td>
<td><font color="red"><form:errors path="age" /></font></td>
</tr>
<tr>
<td><input id="submit" type="submit" value="register"></td>
</tr>
</table>
</form:form>
</body>
</html>
测试: