关于spring后端数据校验@Valid

实体类

package com.aaic.platform.entity.user;


import com.aaic.platform.entity.role.Role;
import lombok.Data;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import javax.validation.constraints.Pattern;
import java.util.Date;


/**
 *
 * @author 
 *
 */
@Entity
@Table(name="user")
@Data
public class User {
	
	@Id
	@Column(name="user_id")
	@GeneratedValue(strategy= GenerationType.IDENTITY)
	private Long userId;		//用户id
	
	@Column(name="user_name")
	@NotEmpty(message="用户名不能为空")
	private String userName;	//登录用户名
	
	@Column(name="user_tel")
	@NotEmpty(message="电话不能为空")
	private String userTel;		//用户电话
	
	@Column(name="real_name")
	@NotEmpty(message="姓名不能为空")
	private String realName;    //真实姓名

	@NotEmpty(message="邮箱不能为空")
//	@Pattern(regexp="^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\\.[a-zA-Z0-9_-]{2,3}){1,2})$",message="请填写正确邮箱号")
	private String eamil;		//邮件
	
	@NotEmpty(message="地址不能为空")
	private String address;		//地址

	private Boolean superman=false;
	
	private String sex;			//性别
	
	@Column(name="theme_skin")
	private String themeSkin;	//主题皮肤

	
	private String password;	//用户密码

	@Column(name="img_path")
	private String imgPath;		//用户头像路径

	
	@Column(name="is_lock")
	private Integer isLock=0;		//该用户是否被禁用
	
	@Column(name="last_login_ip")
	private String lastLoginIp;	//用户最后登录ip;
	
	@Column(name="last_login_time")
	private Date lastLoginTime;	//最后登录时间
	
	@Column(name="modify_time")
	private Date modifyTime;		//最后修改时间
	
	@Column(name="modify_user_id")
	private Long modifyUserId;	//最后修改此用户的用户id
	
	@Column(name="father_id")
	private Long fatherId;		//上司id

	@ManyToOne()
	@JoinColumn(name = "position_id")
	private Position position;	//外键关联 职位表
	
	@ManyToOne()
	@JoinColumn(name = "dept_id")
	private Dept dept;			//外键关联 部门表
	
	@ManyToOne()
	@JoinColumn(name = "role_id")
	private Role role;			//外键关联 角色表






	
}

controller

@RequestMapping(value="useredit",method = RequestMethod.POST)
	public String usereditpost(@Valid  User user, 
			@RequestParam("deptid") Long deptid,
			@RequestParam("positionid") Long positionid,
			@RequestParam("roleid") Long roleid,
			@RequestParam(value = "isbackpassword",required=false) boolean isbackpassword,
			BindingResult br,Model model)  {
		
		log.info("进行用户新建或修改");
        ......................
	}

访问controller时,没有进入controller直接报错

最后发现是 被@Valid标注的实体类要与BindingResult 挨着

修改后,就可以了

	@RequestMapping(value="useredit",method = RequestMethod.POST)
	public String usereditpost(@Valid  User user, BindingResult br,
			@RequestParam("deptid") Long deptid,
			@RequestParam("positionid") Long positionid,
			@RequestParam("roleid") Long roleid,
			@RequestParam(value = "isbackpassword",required=false) boolean isbackpassword,
			Model model)  {
		
		log.info("进行用户新建或修改");
        ....................
}

 

你可能感兴趣的:(关于spring后端数据校验@Valid)