“SpringMVC:高级应用”的版本间差异
		
		
		
		
		
		跳到导航
		跳到搜索
		
				
		
		
	
 (→捕获错误)  | 
				|||
| 第123行: | 第123行: | ||
<syntaxhighlight lang="java">  | <syntaxhighlight lang="java">  | ||
// 商品信息修改提交  | |||
// 在需要校验的pojo前边添加@Validated,在需要校验的pojo后边添加BindingResult  | |||
// bindingResult接收校验出错信息  | |||
// 注意:@Validated和BindingResult bindingResult是配对出现,并且形参顺序是固定的(一前一后)。  | |||
// value={ValidGroup1.class}指定使用ValidGroup1分组的 校验  | |||
// @ModelAttribute可以指定pojo回显到页面在request中的key  | |||
@RequestMapping("/editItemsSubmit")  | |||
public String editItemsSubmit(  | |||
		Model model,  | |||
		HttpServletRequest request,  | |||
		Integer id,  | |||
		@ModelAttribute("items") @Validated(value = { ValidGroup1.class}) ItemsCustom itemsCustom,  | |||
		BindingResult bindingResult,  | |||
		MultipartFile items_pic//接收商品图片  | |||
		) throws Exception {  | |||
	// 获取校验错误信息  | |||
	if (bindingResult.hasErrors()) {  | |||
		// 输出错误信息  | |||
		List<ObjectError> allErrors = bindingResult.getAllErrors();  | |||
		for (ObjectError objectError : allErrors) {  | |||
			// 输出错误信息  | 			// 输出错误信息  | ||
			System.out.println(objectError.getDefaultMessage());  | |||
		}  | 		}  | ||
		// 将错误信息传到页面  | |||
		model.addAttribute("allErrors", allErrors);  | |||
		//  | 		// 可以直接使用model将提交pojo回显到页面  | ||
		model.addAttribute("items", itemsCustom);  | |||
		// 调用service更新商品信息,页面需要将商品信息传到此方法  | 		// 出错重新到商品修改页面  | ||
		return "items/editItems";  | |||
	}  | |||
	//原始名称  | |||
	String originalFilename = items_pic.getOriginalFilename();  | |||
	//上传图片  | |||
	if(items_pic!=null && originalFilename!=null && originalFilename.length()>0){  | |||
		//存储图片的物理路径  | |||
		String pic_path = "F:\\develop\\upload\\temp\\";  | |||
		//新的图片名称  | |||
		String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));  | |||
		//新图片  | |||
		File newFile = new File(pic_path+newFileName);  | |||
		//将内存中的数据写入磁盘  | |||
		items_pic.transferTo(newFile);  | |||
		//将新图片名称写到itemsCustom中  | |||
		itemsCustom.setPic(newFileName);  | |||
	}  | |||
	// 调用service更新商品信息,页面需要将商品信息传到此方法  | |||
	itemsService.updateItems(id, itemsCustom);  | |||
	return "success";  | |||
}  | |||
</syntaxhighlight>  | </syntaxhighlight>  | ||
2020年10月3日 (六) 23:40的版本
Validation
对提交的请求数据进行检验。
依赖包
- hibernate-validator-4.3.0.Final.jar
 - jboss-logging-3.1.0.CR2.jar
 - validation-api-1.0.0.GA.jar
 
配置validator
springmvc.xml
| 使用<mvc:annotation-driven> | 使用HandlerAdapter | 
|---|---|
<mvc:annotation-driven validator="validator"></mvc:annotation-driven>
 | 
<!-- 注解适配器 -->
<bean
	class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
	<property name="webBindingInitializer" ref="customBinder"></property>
</bean>
<!-- 自定义webBinder -->
<bean id="customBinder"
	class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
	<property name="validator" ref="validator" />
</bean>
 | 
<!-- 校验器 -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
	<!-- hibernate校验器-->
	<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
	<!-- 指定校验使用的资源文件,在文件中配置校验错误信息,如果不指定则默认使用classpath下的ValidationMessages.properties -->
	<property name="validationMessageSource" ref="messageSource" />
</bean>
<!-- 校验错误信息配置文件 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
	<!-- 资源文件名-->
	<property name="basenames">   
		<list>    
			<value>classpath:CustomValidationMessages</value> 
		</list>   
	</property>
	<!-- 资源文件编码格式 -->
	<!-- <property name="defaultEncoding" value="UTF-8"></property> -->
	<property name="fileEncodings" value="UTF-8" />
	<!-- 对资源文件内容缓存时间,单位秒 -->
	<property name="cacheSeconds" value="120" />
</bean>
 | |
CustomValidationMessages.properties
位置参考<value>classpath:CustomValidationMessages</value>,内容如:
#添加校验错误提交信息
items.name.length.error=请输入1到30个字符的商品名称
items.createtime.isNUll=请输入商品的生产日期
item.price.isNull=test,价格不能为空
items.message.test=校验信息配置测试文字
使用validator
添加验证规则
在pojo的属性之上添加验证规则:@NotNull(message="{item.price.isNull}",groups= {ValidGroup1.class}),其中:
@NotNull:校验名称;message="{item.price.isNull}":错误信息(配置于“CustomValidationMessages.properties”);groups= {ValidGroup1.class}:此校验所属的分组;@NotNull:;
public class Items {
    private Integer id;
    //校验名称在1到30字符中间
    //message是提示校验出错显示的信息
    //groups:此校验属于哪个分组,groups可以定义多个分组
    @Size(min=1,max=30,message="{items.message.test}",groups={ValidGroup1.class})
    private String name;
    @NotNull(message="{item.price.isNull}",groups= {ValidGroup1.class})
    private Float price;
    private String pic;
    //非空校验
    @NotNull(message="{items.createtime.isNUll}",groups= {ValidGroup1.class})
    private Date createtime;
    private String detail;
    //get、set、toString...
}
错误消息文件
验证规则中配置的message="{item.price.isNull}",对应于“CustomValidationMessages.properties”中的信息:
#添加校验错误提交信息
items.name.length.error=请输入1到30个字符的商品名称
items.createtime.isNUll=请输入商品的生产日期
item.price.isNull=test,价格不能为空
items.message.test=校验信息配置测试文字
- 如果在eclipse中编辑properties文件无法看到中文则参考“Eclipse开发环境配置-indigo.docx”添加propedit插件。【???】
 
捕获错误
在Controller的方法形参前添加@Validated来在参数绑定时进行校验,并将校验信息写入BindingResult中:
- 在要校验的pojo后边添加
BingdingResult; - 一个
BindingResult对应一个pojo; 
// 商品信息修改提交
// 在需要校验的pojo前边添加@Validated,在需要校验的pojo后边添加BindingResult
// bindingResult接收校验出错信息
// 注意:@Validated和BindingResult bindingResult是配对出现,并且形参顺序是固定的(一前一后)。
// value={ValidGroup1.class}指定使用ValidGroup1分组的 校验
// @ModelAttribute可以指定pojo回显到页面在request中的key
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(
		Model model,
		HttpServletRequest request,
		Integer id,
		@ModelAttribute("items") @Validated(value = { ValidGroup1.class}) ItemsCustom itemsCustom,
		BindingResult bindingResult,
		MultipartFile items_pic//接收商品图片
		) throws Exception {
	// 获取校验错误信息
	if (bindingResult.hasErrors()) {
		// 输出错误信息
		List<ObjectError> allErrors = bindingResult.getAllErrors();
		for (ObjectError objectError : allErrors) {
			// 输出错误信息
			System.out.println(objectError.getDefaultMessage());
		}
		// 将错误信息传到页面
		model.addAttribute("allErrors", allErrors);
		
		// 可以直接使用model将提交pojo回显到页面
		model.addAttribute("items", itemsCustom);
		
		// 出错重新到商品修改页面
		return "items/editItems";
	}
	
	//原始名称
	String originalFilename = items_pic.getOriginalFilename();
	//上传图片
	if(items_pic!=null && originalFilename!=null && originalFilename.length()>0){
		//存储图片的物理路径
		String pic_path = "F:\\develop\\upload\\temp\\";
		//新的图片名称
		String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
		//新图片
		File newFile = new File(pic_path+newFileName);
		//将内存中的数据写入磁盘
		items_pic.transferTo(newFile);
		//将新图片名称写到itemsCustom中
		itemsCustom.setPic(newFileName);
	}
	
	// 调用service更新商品信息,页面需要将商品信息传到此方法
	itemsService.updateItems(id, itemsCustom);
	return "success";
}
错误回显
在捕获错误后,页面需要显示错误信息。
如:
- 页头:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
 - 在需要显示错误信息地方:
<spring:hasBindErrors name="item"> <c:forEach items="${errors.allErrors}" var="error"> ${error.defaultMessage }<br/> </c:forEach> </spring:hasBindErrors>
<spring:hasBindErrors name="item">表示如果item参数绑定校验错误下边显示错误信息。
 
或,如:【???????????】
- Controller:
if(bindingResult.hasErrors()){ model.addAttribute("errors", bindingResult); }
 - 在需要显示错误信息地方:
<c:forEach items="${errors.allErrors}" var="error"> ${error.defaultMessage }<br/> </c:forEach>