SpringMVC框架(2)之(2.3 Validation校验器)

Validation校验器

SpringMVC使用 JSR-303 校验规范,使用是 Hibernate Validator

【1】设置环境:
1.导jar包;
2.在处理器适配器中配置校验器;
3.创建资源文件(eg:CustomValiationMessages.properties);

【2】校验规则:(POJO类对应的字段上)
需求:商品提交时校验
1.商品名称长度1~30个字符;
2.商品生产日期不能为空;

【3】捕获错误:
需要修改 Controller:
1.在需要校验的 POJO类前添加 @Validated注解;
2.在需要校验的 POJO类之后(紧跟)添加参数 BindingResult bindingResult;

【4】在页面中显示错误信息:
jsp页面中遍历错误信息;

 
1. springmvc.xml中处理器适配器中配置校验器; 根据<value>classpath:CustomValiationMessages</value>在 config文件夹下创建 CustomValiationMessages.properties资源文件; 3.Items.java中分别在字段上添加 @Size (min=1,max=30,message="{items.name.length.error}")、 @NotNull (message="{items.createtime.is.notnul}")注解,message的值根据 properties文件中的 key来得到; 在页面中显示错误信息:需要在4.ItemsController.java 中将结果添加到 model中:model.addAttribute("errors",errors);;)

1. springmvc.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 
	
	<!-- (开启spring注解扫描)3.注解开发的Handler -->
	<context:component-scan base-package="com.asd"></context:component-scan>

	<!-- 通过annotation-driven可以替代处理器映射和处理器适配器-->
	<mvc:annotation-driven></mvc:annotation-driven> 
	
	<!-- 2 自定义WebBinder--> 
	<bean id="customBinder" name="" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
        <!-- 配置validator -->
        <property name="validator" ref="validator"></property>
    </bean>
	
	<!-- 3.校验器 -->
	<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
		<!-- 校验器 -->
		<property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>
		<!-- 指定校验器使用的资源文件,如果不指定,则默认使用classpath下ValidationMessage.properties -->
		<property name="validationMessageSource" ref="messageSource"></property>
	</bean>
	<!-- 错误信息配置文件 -->
	<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <!-- 资源文件名 -->
        <property name="basenames">
        	<list>
        		<value>classpath:CustomValiationMessages</value>
        	</list>
        </property>
        <!-- 文件编码 -->
        <property name="fileEncodings" value="UTF-8"></property>
        <!-- 缓存时间 -->
        <property name="cacheSeconds" value="120"></property>
	</bean>

	<!-- 1.注解适配器 --> 
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <!-- 注解适配器中引用--> 
        <property name="webBindingInitializer" ref="customBinder"></property>
    </bean>

	<!-- 4.视图解析器 --> 
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

2.CustomValiationMessages.properties

items.name.length.error=商品名称长度限制在1~30个字符
items.createtime.is.notnull=商品生产日期不可为空

3.Items.java

public class Items{
     ...

	//商品名称长度限制在1~30个字符
	@Size(min=1,max=30,message="{items.name.length.error}")
	private String name;

    @NotNull(message="{items.createtime.is.notnul}")
	private Date createtime;

	...
}

4.ItemsController.java
(参数POJO类前添加 @Validated 注解,后面添加参数 (BindingResult bindingResult); itemsCustom@Validated @ModelAttribute(value=“itemsCustom”) ItemsCustom itemsCustom,BindingResult bindingResult,)

@Controller
@RequestMapping("/items")
public class ItemsController{
	@Autowired
	private ItemsService itemsService;

	@RequestMapping("/editItemsSubmit") 
	public String editItemsSubmit(Model model,Integer id,HttpServlerRequest request,
		@Validated @ModelAttribute(value="itemsCustom") ItemsCustom itemsCustom,BindingResult bindingResult,
		MultipartFile picFile) throws Exception{ 
		model.addAttribute("id",id);
		if (picFile!=null&&picFile.getOriginalFilename()!=null&&picFile.getOriginalFilename().length()>0) {
			//进行文件上传
			String fielPath=request.getRealPath("/");
			//上传的原始文件名
		    String oldFileName=picFile.getOriginalFilename();
		    //新文件名
		    String newFileName=UUID.randomUUID()+oldFileName.subString(oldFileName.lastIndexOf("."));
		    //新文件
		    File file=new File(fielPath+"images/"+newFileName);
		    //写入文件
		    picFile.transferTo(file);
		    //新图片写入到数据库中
		    itemsForm.setPic(newFileName);
	    }
	    if (bindingResult.hasErrors()) {
	    	List<ObjectError> errors=bindingResult.getAllErrors();
	    	for (ObjectError error:errors) {
	    		System.out.pringln(error.getDefaultMessage());
	    	}
	    }
		itemsService.updateItems(id,itemsCustom); 
		return "redirect:queryItems.action"; 
	}
}

5. editItems.jsp
4.ItemsController.java中打印输出信息时,有 getDefaultMessage() 方法:System.out.pringln(error.getDefaultMessage());,所以 5. editItems.jsp页面中显示错误信息时:${error.defaultMessage},get方法去掉 get,再首字母小写;)

<body>
  <!-- 显示错误信息 -->
  <c:forEach items="${errors}" var="error">
    ${error.defaultMessage}<br/>
  </c:forEach>

  <form action="${pageContext.request.contextPath}/items/editItemsSubmit.action" method="post" enctype="multipart/form-data">
  <input type="hidden" name="id" value="${item.id}"/>
  <table>
     <tr><td>商品名称:</td>
         <td><input type="text" name="name" value="${itemsCustom.name}"></td></tr>
     <tr><td>商品价格:</td>
         <td><input type="text" name="price" value="${itemsCustom.price}"></td></tr>
     
     <tr><td>商品图片</td>
         <c:if test="${itemsCustom.pic!=null}">
             <img src="images/${itemsCustom.pic}" width="100" height="100"/><br/>
         </c:if>
         <input type="file" name="picFile"/>
         </tr>

     <tr><td>订购日期:</td>
         <td><input type="text" name="price" value="<fmt:formatDate value='${itemsCustom.createtime}' pattern="yyyy-MM-dd"/>"></td></tr>
     <tr><td>商品描述:</td>
         <td><input type="text" name="detail" value="${itemsCustom.detail}"></td></tr>
     <tr><td colspan="2" align="center"><input type="submit" value="提交"/></td></tr>
  </table>
  </form>
</body>

运行结果页面:
在这里插入图片描述

Validation分组校验

需求:
针对不同的 Controller方法达到个性化验证;比如:修改商品信息,只校验日期不为空;
1. 创建分组接口;(接口不定义方法,只作为一个标识)
2. 定义校验规则属于哪分组;(POJO类对应的字段上相应的注解如 @Size、@NotNull 中添加 groups属性,如:groups={ValidateGroup1.class},指明属于哪个组; )
3. 在Controller方法中定义使用校验组;(使用 @Validated 注解的 value 属性指明分组:
@Validated(value={ValidateGroup1.class}));

eg:
1.ValidateGroup1.java

public class ValidateGroup1{
	//接口不定义方法,只作为一个标识
}

1.’ValidateGroup2.java

public class ValidateGroup2{
	//接口不定义方法,只作为一个标识
}

2.Items.java
(对应的字段上相应的注解如 @Size、@NotNull 中添加 groups属性,如:groups={ValidateGroup1.class},指明属于哪个组; )

public class Items{
     ...

	//商品名称长度限制在1~30个字符
	@Size(min=1,max=30,message="{items.name.length.error}",groups={ValidateGroup2.class})
	private String name;

    @NotNull(message="{items.createtime.is.notnul}",groups={ValidateGroup1.class})
	private Date createtime;

	...
}

3.ItemsController.java
((参数POJO类前添加 @Validated 注解,@Validated(value={ValidateGroup1.class})value 指明哪个分组,后面添加参数 (BindingResult bindingResult);)

@Controller
@RequestMapping("/items")
public class ItemsController{
	@Autowired
	private ItemsService itemsService;

	@RequestMapping("/editItemsSubmit") 
	public String editItemsSubmit(Model model,Integer id,HttpServlerRequest request,
		@Validated(value={ValidateGroup1.class}) @ModelAttribute(value="itemsCustom") ItemsCustom itemsCustom,BindingResult bindingResult,
		MultipartFile picFile) throws Exception{ 
		model.addAttribute("id",id);
		if (picFile!=null&&picFile.getOriginalFilename()!=null&&picFile.getOriginalFilename().length()>0) {
			//进行文件上传
			String fielPath=request.getRealPath("/");
			//上传的原始文件名
		    String oldFileName=picFile.getOriginalFilename();
		    //新文件名
		    String newFileName=UUID.randomUUID()+oldFileName.subString(oldFileName.lastIndexOf("."));
		    //新文件
		    File file=new File(fielPath+"images/"+newFileName);
		    //写入文件
		    picFile.transferTo(file);
		    //新图片写入到数据库中
		    itemsForm.setPic(newFileName);
	    }
	    if (bindingResult.hasErrors()) {
	    	List<ObjectError> errors=bindingResult.getAllErrors();
	    	for (ObjectError error:errors) {
	    		System.out.pringln(error.getDefaultMessage());
	    	}
	    }
		itemsService.updateItems(id,itemsCustom); 
		return "redirect:queryItems.action"; 
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41029923/article/details/84874358