** 使用 显示错误信息,每次刷新错误信息显示顺序都不一样 **
jsp :
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register</title>
<style type="text/css">
.errorMessage{
width: 300px;
}
input.error{
border: 1px solid red;
}
</style>
</head>
<body>
<h1>Register</h1>
<%--<form method="post">--%>
<sf:form method="post" commandName="spitter">
<sf:errors element="div" path="*" cssStyle="color: red; background-color: #ffcccc; border: 1px solid red" />
<table>
<tr>
<td>First Name:</td>
<td>
<%--<input type="text" name="firstName" />--%>
<sf:input path="firstName" cssErrorClass="error" />
</td>
<td class="errorMessage">
<sf:errors path="firstName" cssStyle="color: red;" />
</td>
</tr>
<tr>
<td>Last Name:</td>
<td>
<%--<input type="text" name="lastName" />--%>
<sf:input path="lastName" cssErrorClass="error" />
</td>
<td class="errorMessage">
<sf:errors path="lastName" cssStyle="color: red;" />
</td>
</tr>
<tr>
<td>Username:</td>
<td>
<%--<input type="text" name="username" />--%>
<sf:input path="username" cssErrorClass="error" />
</td>
<td class="errorMessage">
<sf:errors path="username" cssStyle="color: red;" />
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<%--<input type="password" name="password" />--%>
<sf:password path="password" cssErrorClass="error" />
</td>
<td class="errorMessage">
<sf:errors path="password" cssStyle="color: red;" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Register" />
</td>
</tr>
</table>
<%--</form>--%>
</sf:form>
</body>
</html>
controller :
package cn.mrchen.springmvc.controller;
import cn.mrchen.springmvc.ISpitterRepository;
import cn.mrchen.springmvc.vo.Spitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.validation.Valid;
@Controller
@RequestMapping("/spitter")
public class SpitterController {
private ISpitterRepository spitterRepository;
@Autowired
public SpitterController(ISpitterRepository spitterRepository) {
this.spitterRepository = spitterRepository;
}
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String register(Model model){
model.addAttribute("spitter", new Spitter());
return "registerForm";
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String register(@Valid Spitter spitter, BindingResult bindingResult){
if (bindingResult.hasErrors()){
return "registerForm";
}
spitterRepository.save(spitter);
return "redirect:/spitter/" + spitter.getUsername();
}
@RequestMapping(value = "/{username}", method = RequestMethod.GET)
public String profile(Model model, @PathVariable("username") String username){
model.addAttribute(spitterRepository.findByUsername(username));
return "profile";
}
}
vo :
package cn.mrchen.springmvc.vo;
import javax.validation.constraints.Size;
public class Spitter {
private Long id;
@Size(min = 1, max = 128, message = "{spitter.firstName.size}")
private String firstName;
@Size(min = 1, max = 128, message = "{spitter.lastName.size}")
private String lastName;
@Size(min = 1, max = 128, message = "{spitter.username.size}")
private String username;
@Size(min = 6, max = 20, message = "{spitter.password.size}")
private String password;
public Spitter(Long id, String firstName, String lastName, String username, String password) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.username = username;
this.password = password;
}
public Spitter(String firstName, String lastName, String username, String password) {
this(null, firstName, lastName, username, password);
}
public Spitter() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
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;
}
}
properties :
spitter.firstName.size=First Name size must between {min} to {max}.
spitter.lastName.size=Last Name size must between {min} to {max}.
spitter.username.size=Username size must between {min} to {max}.
spitter.password.size=Password size must between {min} to {max}.