验证适用于用户类但不适用于地址类
注册.java
package com.semanticbits.controller;
import com.semanticbits.pojo.Address;
import com.semanticbits.pojo.User;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/register")
public class RegistrationController {
@RequestMapping(method = RequestMethod.GET)
public String showRegistrationForm(ModelMap map){
User user=new User();
user.setAddress(new Address());
map.addAttribute("user", user);
return "register";
}
//reference data
@ModelAttribute("genderList")
public List<String> showGenderOnForm(){
List<String> genderList=new ArrayList<>();
genderList.add("Select gender");
genderList.add("Male");
genderList.add("Female");
return genderList;
}
@RequestMapping(method = RequestMethod.POST)
public String processRegistrationForm(@ModelAttribute("user") User user, BindingResult result) {
validator.validate(user, result);
//validator.validate(user.getAddress(), result);
if (result.hasErrors()) {
return "register";
}
return "welcome";
}
}
地址.java
package com.semanticbits.pojo;
import javax.persistence.Embeddable;
import org.springmodules.validation.bean.conf.loader.annotation.handler.NotBlank;
import org.springmodules.validation.bean.conf.loader.annotation.handler.Size;
@Embeddable
public class Address {
@NotBlank(message = "Please enter city")
private String city;
@NotBlank(message = "Please enter state")
private String state;
@NotBlank(message = "Please enter zip code")
@Size(max = 6,min = 6,message = "Zip code should be of 6 digits only")
private int zipCode;
//setters and getters
}
用户.java
/* * 要更改此模板,请选择工具 | 模板 * 并在编辑器中打开模板。*/ 包 com.semanticbits.pojo;
import com.semanticbits.pojo.Address;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springmodules.validation.bean.conf.loader.annotation.handler.Length;
import org.springmodules.validation.bean.conf.loader.annotation.handler.Max;
import org.springmodules.validation.bean.conf.loader.annotation.handler.Min;
import org.springmodules.validation.bean.conf.loader.annotation.handler.NotBlank;
@Entity
@Table
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int userId;
@NotBlank(message = "Please enter username")
@Length(min = 6,message ="Please enter minimum six characters" )
private String username;
@NotBlank(message = "Please enter password")
private String password;
@NotBlank(message = "Please enter your first name")
private String firstName;
@NotBlank(message ="Please enter your last name")
private String lastName;
@Min(value=18,message = "Age should be greater than 18")
@Max(value=100,message="Age should be less than 99")
private int age;
private String gender;
@Embedded
private Address address;
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 int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
注册.jsp
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login here</title>
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<style>
.form-error-message{ font-weight: bold; color: red; }
.errorblock {
color: #000;
background-color: #ffEEEE;
border: 3px solid #ff0000;
padding: 8px;
margin: 16px;
}
</style>
<body>
<div class="container">
<div class="modal-form"/>
<h1>Registration form</h1>
<form:form commandName="user" action="register.htm" method="POST">
<form:errors path="*" cssClass="errorblock" element="div" />
<table>
<tr>
<td> Username :</td>
<td><form:input path="username"/></td>
<td><form:errors path="username" cssClass="form-error-message"/>
</tr>
<tr>
<td>Password :</td>
<td><form:input path="password"/></td>
<td><form:errors path="password" cssClass="form-error-message"/>
<tr>
<tr>
<td>Confirm Password :</td>
<td><input type="password" name="confirmPassword"/></td>
<tr/>
<tr>
<td>First Name :</td>
<td><form:input path="firstName"/></td>
<td><form:errors path="firstName" cssClass="form-error-message"/></td>
<tr/>
<tr>
<td>Last Name :</td>
<td><form:input path="lastName"/></td>
<td><form:errors path="lastName" cssClass="form-error-message"/></td>
<tr/>
<tr>
<td>Age :</td>
<td><form:input path="age"/></td>
<td><form:errors path="age" cssClass="form-error-message"/></td>
<tr/>
<tr>
<td>Gender :</td>
<td><form:select items="${genderList}" path="gender"/></td>
<tr/>
<tr>
<td>City :</td>
<td><form:input path="address.city"/></td>
<td><form:errors path="address.city" cssClass="form-error-message"/></td>
<tr/>
<tr>
<td>State :</td>
<td><form:input path="address.state"/></td>
<td><form:errors path="address.state" cssClass="form-error-message"/></td>
</tr>
<tr>
<td>Zip Code :</td>
<td><form:input path="address.zipCode"/></td>
<td><form:errors path="address.zipCode" cssClass="form-error-message"/></td>
</tr>
<tr>
<td> <input type="checkbox" name="rememberme"/>Remember Me</td>
<td><input type="submit" class="btn-medium btn btn-info" value="Login"/></td>
</tr>
</form:form>
</div>
</div>
<script src="js/bootstrap.min.js"></script>
</body>
</html>