加入jar包
bean-validator.jar
在实体类中加入验证Annotation和消息提示
package com.stone.model; import javax.validation.constraints.Size; import org.hibernate.validator.constraints.Email; import org.hibernate.validator.constraints.NotEmpty; public class User { private int id; private String username; private String nickname; private String password; private String email; public User(String username, String nickname, String password, String email) { this.username = username; this.nickname = nickname; this.password = password; this.email = email; } public User() { super(); } public int getId() { return id; } public void setId(int id) { this.id = id; } @NotEmpty(message="用户名不能为空") public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } @NotEmpty(message="密码不能为空") @Size(min = 3, max = 10,message="密码长度必须大于3小于10") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Email(message="邮件的格式为[email protected]") public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", nickname=" + nickname + ", password=" + password + ", email=" + email + "]"; } }
在Controller中加入@Valid和BindingResult处理验证结果
package com.stone.controller; import java.util.HashMap; import java.util.Map; import javax.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.view.InternalResourceViewResolver; import com.stone.model.User; @Controller @RequestMapping("/user") public class UserController { private final static Map<String, User> users = new HashMap<String, User>(); public UserController() { users.put("ldh", new User("ldh", "刘德华", "123", "[email protected]")); users.put("zxy", new User("zxy", "张学友", "123", "[email protected]")); users.put("gfc", new User("gfc", "郭富城", "123", "[email protected]")); users.put("lm", new User("lm", "黎明", "123", "[email protected]")); } @RequestMapping("/users") public String list(Model model) { model.addAttribute("users", users); return "user/list"; } @RequestMapping(value = "/add", method = RequestMethod.GET) public String add(Model model) { model.addAttribute(new User()); return "user/add"; } @RequestMapping(value = "/add", method = RequestMethod.POST) public String add(@Valid User user, BindingResult bingding) { if (bingding.hasErrors()) { return "user/add"; } users.put(user.getUsername(), user); return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/user/users"; } }
在jsp中加入验证提示消息
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="sf" uri="http://www.springframework.org/tags/form"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘add.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <sf:form method="post" modelAttribute="user"> username:<sf:input path="username" /> <sf:errors path="username" /> <br /> password:<sf:password path="password" /> <sf:errors path="password" /> <br /> nickname:<sf:input path="nickname" /> <br /> email:<sf:input path="email" /> <sf:errors path="email" /> <br /> <input type="submit" /> </sf:form> </body> </html>
时间: 2024-10-12 17:06:14