no14-第十一章-后端数据校验
分类: ssm 专栏: ssm框架课 标签: 服务端数据校验
2023-01-02 17:30:59 996浏览
服务端数据校验
1.spring表单标签及数据校验
- 数据校验要引入的jar
hibernate-validator
jboss-logging
jakarta.validation-api
classmate
- 编写页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="fm" 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 http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="${pageContext.request.contextPath }/statics/calendar/WdatePicker.js"></script> <title>Insert title here</title> </head> <body> <fm:form method="post" modelAttribute="sysUser"> <fm:errors path="account"/><br/> 用户编码:<fm:input path="account"/><br/> <fm:errors path="realName"/><br/> 用户名称:<fm:input path="realName"/><br/> <fm:errors path="password"/><br/> 用户密码:<fm:password path="password"/><br/> <fm:errors path="birthday"/><br/> 用户生日:<fm:input path="birthday" Class="Wdate" readonly="readonly" onclick="WdatePicker();" class="Wdate"/><br/> 用户地址:<fm:input path="address"/><br/> 联系电话:<fm:input path="phone"/><br/> 用户角色: <fm:radiobutton path="roleId" value="1"/>系统管理员 <fm:radiobutton path="roleId" value="2"/>经理 <fm:radiobutton path="roleId" value="3" checked="checked"/>普通用户 <br/> <input type="submit" value="保存"/> </fm:form> </body> </html>
- 编写control代码
/** * 跳转到添加用户页面 * @param sysUser * @return */ @GetMapping("/add") public String toAdd(@ModelAttribute("sysUser") SysUser sysUser){ return "sysUser/add"; } /** * 添加用户 * @param sysUser * @param session * @return */ @PostMapping("/add") public String add(@Valid SysUser sysUser, BindingResult bindingResult , HttpSession session){ if(bindingResult.hasErrors()){ logger.debug("添加用户验证失败"); return "sysUser/add"; } sysUser.setCreatedUserId(((SysUser)session.getAttribute(Constants.USER_SESSION)).getId()); if(sysUserService.add(sysUser)){ return "redirect:/user/list"; } return "sysUser/add"; }
备注:@Valid和BindingResult必须紧紧挨着。
- 修改实体类代码
public class SysUser { private Integer id; //id @NotBlank(message = "用户编码不能为空") private String account; //用户编码 @NotBlank(message = "用户名称不能为空") private String realName; //用户名称 @NotBlank(message = "用户密码不能为空") // @Min(value = 3,message = "不能少于3个字符") // @Max(value = 10,message = "不能多于10个字符") @Length(min = 3,max = 10,message = "用户密码长度不能少于{min}个字符不能多于{max}个字符") private String password; //用户密码 private Integer sex; //性别 @DateTimeFormat(pattern = "yyyy-MM-dd") @Past(message = "日期必须是过去的时间") @NotNull(message = "出生日期必填") private Date birthday; //出生日期 @Pattern(regexp = "^1[35789]{1}\\d{9}$",message = "手机号格式不对") private String phone; //电话 @NotBlank(message = "地址不能为空") private String address; //地址
补充:实际开发一般不用spring标签,那传统的form表单如何校验?
@PostMapping("/add") public String add(@Valid User sysUser, BindingResult bindingResult , HttpSession session,HttpServletRequest request){ if(bindingResult.hasErrors()){ List<FieldError> fieldErrors = bindingResult.getFieldErrors(); for(FieldError fieldError : fieldErrors){ request.setAttribute(fieldError.getField(),fieldError.getDefaultMessage());//然后前端el表达式展示下就行 } return "useradd"; } sysUser.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId()); if(userService.add(sysUser)){ return "redirect:/user/list"; } return "useradd"; }
好博客就要一起分享哦!分享海报
您可能感兴趣的博客
他的专栏
他感兴趣的技术