sprigmvc jsr303 验证:
/**
@NotNull,@NotEmpty @NotBlank 注解的区别:
@NotNull:不能为null,但可以为empty.
@NotEmpty :, 不能为null,而且长度必须大于0
@NotBlank:只能作用于String上,不能为null,而且调用trim()后,长度必须大于0
@NotBlank:只能作用于String上,不能为null,而且调用trim()后,长度必须大于0
如:String name="";
@NotNull:true
@NotEmpty:false
@NotBlank:false
<!--
1.数据类型转换
2.数据类型格式化
3.数据校验.
1).如何校验 ? 注解 ?
①.使用 JSR 303 验证标准
②.加入hibernate-validator-5.1.2.Final.jar,jboss-logging-3.1.3.GA.jar,slf4j-api-1.6.4.jar,validation-api-1.1.0.Final.jar,classmate-1.0.0.jar验证框架的 jar 包
③.在 SpringMVC 配置文件中添加 <mvc:annotation-driven />
④.需要在 bean 的属性上添加对应的注解
⑤.在目标方法 bean 类型的前面添加 @Valid 注解
2).验证出错转向到哪一个页面 ?
注意: 需校验的 Bean 对象和其绑定结果对象或错误对象时成对出现的,它们之间不允许声明其他的入参
3).错误消息 ? 如何显示, 如何把错误消息进行国际化
-->
实际开发中用 springmvc的 <form:form标签用<form:input等标签回显--如果需要回显,必须加个modelAttribute="printTemplate"且modelAttribute域中必须有printTemplate这个对象,用<form:error显示错误message:
步骤:
1.jsp页面
<div class="rightinfo">
<div class="formbody">
<form:form
action="${pageContext.request.contextPath}/printTemplate/saveOrUpdatePrintTemplate"
method="POST" modelAttribute="printTemplate">
<form:hidden id="printTemplat_id" path="id" />
<form:hidden id="printTemplat_filename" path="filename" />
<ul class="forminfo">
<li><label>模板名称</label> <form:input
id="printTemplate_templatename" path="templatename"
cssClass="input02" /> <form:errors path="templatename"
cssStyle="display:inline;"></form:errors>
</li>
<li><label>选择公司类别</label>
<form:select id="printTemplate_authtp" path="authtp" cssClass="input02">
<form:option value="仪器公司授权 " selected="selected">仪器公司授权</form:option>
<form:option value="股份公司授权">股份公司授权</form:option>
</form:select>
</li>
<li><label>证书编号</label>
<form:input id="printTemplate_cnum"
path="cnum" cssClass="input02"/> <form:errors path="cnum"
cssStyle="display:inline;"></form:errors> <form:radiobutton
id="printTemplate_cnumstatus" path="cnumstatus" value="1"
checked="checked" />启用 <form:radiobutton
id="printTemplate_cnumstatus" path="cnumstatus" value="0" />禁用
</li>
</ul>
<div class="save_div">
<input id="fanhui" type="button"class="fanhui" value="返回" onclick="history.back(-1);"/>
<input id="save" type="submit"class="sousuo" value="确认保存" />
</div>
</form:form>
</div>
</div>
2.controller层:
@Valid是使用hibernate validation的时候使用 ,@Validated 是只用springValidator
校验机制使用
@Valid是jdk的接口,Hibernate的实现。
@ModelAttribute ("printTemplate") :会将 PrintTemplateprintTemplate这个对象赋给
modelAttribute中的 printTemplate方便form:from使用回显,
@ValidatedPrintTemplate printTemplate会在springmvc从表单取出数据对PrintTemplate printTemplate进行赋值之前,先从对pojo PrintTemplate上加了jsr303注解的如 @NotEmpty(message="模板名称不能为空,亲,请重新输入")等的属性先进行验证,
@NotEmpty(message="模板名称不能为空,亲,请重新输入")
private String templatename;
因为templatename如果为空或者为null那么 @NotEmpty返回false,此时在会将结果绑定到BindingResult br中,如果@NotEmpty返回true,那么正常赋值给templatename,如果为false
则将message="模板名称不能为空,亲,请重新输入" 绑定到 br对象中,在页面上<from:error中进行回显
<form:errors path="templatename"cssStyle="display:inline;"></form:errors>
另外我们可以在在反射调用setTemplatename(String templatename)方法中 根据templatename 的情况进行逻辑操作,比如可以对 templatename进行trim()去空后再手动进行赋值为null,那么人为的让@NotEmpty标签返回false,从而在页面上
<form:errors path="templatename"cssStyle="display:inline;"></form:errors>
进行信息回显
@Valid在要进行验证的表单对象的前面声明
BindingResult br一定要跟在所验证的表单对象的后面声明,不然会报错
@RequestMapping(value="saveOrUpdatePrintTemplate",method=RequestMethod.POST)
public ModelAndView saveOrUpdatePrintTemplate(HttpServletRequestrequest,ModelAndViewmav,@ModelAttribute
("printTemplate")@Validated PrintTemplate printTemplate,BindingResult br) throws IOException {
this.request=request;
//获得userid并set到表映射到数据库中
printTemplate.setUserid(getUserid(userService));
if (br.hasErrors()) {
mav.setViewName("printTemplate/editPrintTemplate");
return mav;
}
String fileName=printTemplate.getFilename();
if(fileName=="" || fileName==null){
fileName=this.getFileName();
}
printTemplate.setFilename(fileName);
printTemplateService.saveOrUpdatePrintTemplate (printTemplate);
mav.setViewName("redirect:/printTemplate/listPrintTemplate");
return mav;
}
POJO层:
public classPrintTemplate implements Serializable {
/**
* 模板名
*/
@NotEmpty(message="模板名称不能为空,亲,请重新输入")
private String templatename;
public StringgetTemplatename() {
return templatename;
}
public void setTemplatename(Stringtemplatename){
//if(templatename==null ||"".equals(templatename.trim()))templatename = null;
this.templatename = templatename;
}
}