接上篇,今天学习Spring的验证,demo是一个极其简单的用户登陆功能,嗯,深得也不会啊!
public class UserModel {
private String id; // 用户ID
private String name; // 用户姓名
private String password; // 用户密码
//getter setter方法
public interface UserService {
String userLogin(UserModel user);
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public String userLogin(UserModel user) {
String password = userDao.userLogin(user);
if (password.equals(user.getPassword())) {
return "success";
} else {
return "false";
}
}
}
public interface UserDao {
String userLogin(UserModel user);
}
@Service
public class UserJdbcDaoImpl implements UserDao {
@Override
public String userLogin(UserModel user) {
retun "123456";
}
}
@Controller
@RequestMapping("/index")
public class IndexController {
@RequestMapping("/helloWorld")
public String helloworld() {
// return "success"; //跳转到success页面
return "index";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Insert title here
@Controller
@RequestMapping(value = "user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/login", method = RequestMethod.POST)
protected String handle(UserModel user, BindingResult result, Model model) {
String flag = userService.userLogin(user);
if ("success".equals(flag)) {
// Map map = new HashMap();
// map.put("name", user.getName());
// map.put("password", user.getPassword());
// return new ModelAndView("success", map);
return "success";
}
return "fail";
}
}
登陆成功后的页面如下:
哈哈,成功了。^_^.
@Component
public class UserValidator implements Validator {
@Override
public boolean supports(Class> zlass) {
// 要验证的Model,为假则不验证。
return UserModel.class.isAssignableFrom(zlass);
// return false;
}
@Override
public void validate(Object object, Errors errors) {
UserModel userVO = (UserModel) object;
ValidationUtils.rejectIfEmpty(errors, "name", "user.name.required", "name的内容不能为空");
ValidationUtils.rejectIfEmpty(errors, "password", "user.password.required", "password的内容不能为空");
int length = userVO.getName().length();
if (length > 20) {
errors.rejectValue("name", "user.name.too_long", "用户名不能超过{20}个字符");
}
length = userVO.getPassword().length();
if (length < 6) {
errors.rejectValue("password", "user.password.too_short", "密码太短,不能少于{6}个字符");
} else if (length > 20) {
errors.rejectValue("password", "user.password.too_long", "密码太长,不能长于{20}个字符");
}
}
}
@Controller
@RequestMapping(value = "user")
public class UserController {
@Autowired
private UserService userService;
@Resource
private Validator validator;
@RequestMapping(value = "/login", method = RequestMethod.POST)
protected String handle(UserModel user, BindingResult result, Model model) {
this.validator.validate(user, result);//添加验证
if (result.hasErrors()) {
model.addAttribute("user", user);// 把user对象返回到页面, 这样不至于表单被清空了
return "index";
}
String flag = userService.userLogin(user);
if ("success".equals(flag)) {
........
}
这样,当我们在登陆页面什么都不写时,validator实现类的result就会包含错误信息,那么,result.hasErrors就会为true,我们就又返回到登陆页面了。