一,参数校验git
springmvc中咱们能够使用第三方的校验框架来实现请求参数的校验,经常使用hibernate的校验框架validation 1.pom中导入所需依赖 hibernate-validator-4.3.2.Final.jar jboss-logging-3.1.0.CR2.jar validation-api-1.0.0.GA.jar 2.配置验证器 <!-- 配置验证器 --> <bean id="myvalidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property> </bean> <mvc:annotation-driven validator="myvalidator"/> 3.建立实体类,用注解配置校验规则 public class Phone { private Integer id; @NotBlank(message = "品牌呢?") private String brand; @NotBlank(message = "没型号?") private String model; @Max(value = 99999, message = "太贵了吧?") private Integer price; @DateTimeFormat(pattern = "yyyy-MM-dd") private Date launchDate; //GETTERS AND SETTERS... } 4.contorller: @RequestMapping("add") public String add(Model md,@Valid Phone phone, BindingResult br) throws Exception { if (br.hasErrors()) { FieldError brand = br.getFieldError("brand"); FieldError model = br.getFieldError("model"); FieldError price = br.getFieldError("price"); if (brand != null) { md.addAttribute("brandmsg", brand.getDefaultMessage()); } if (model != null) { md.addAttribute("modelmsg", model.getDefaultMessage()); } if (price != null) { md.addAttribute("pricemsg", price.getDefaultMessage()); } return "add"; } else { phoneService.add(phone); return "redirect:list"; } } 注意:参数列表中,被校验的参数(phone)和BindingResult必须相邻并保证顺序。 5.jsp显示错误信息 品 牌:<input type="text" name="brand"><span style="color: red;">${ brandmsg }</span><br> 型 号:<input type="text" name="model"><span style="color: red;">${ modelmsg }</span><br> 价 格:<input type="text" name="price"><span style="color: red;">${ pricemsg }</span><br>
二,SpringMVC中的文件上传
1.springmvc对fileupload进行了封装,使用上传须要先导入fileupload的依赖: <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> 2.还须要在springmvc的配置文件中配置文件解析器: <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10240000"/> </bean> 3.表单中须要指定提交方式为post,并将enctype改成multipart/form-data 4.完成以上设置,就能够在controller方法中接收文件了: @RequestMapping("/upload") public String upload(@RequestParam("fileName") CommonsMultipartFile file) throws Exception { } 注意该参数须要经过@RequestParam指定参数名
经常使用校验注解
@Null | 被注释的元素必须为 null |
---|---|
@NotNull | 被注释的元素必须不为 null |
@NotEmpty | 被注释的字符串的必须非空 |
@NotBlank | 验证字符串非null,且长度必须大于0 |
@Min(value) | 被注释的元素必须是一个数字,其值必须大于等于指定的最小值 |
@Max(value) | 被注释的元素必须是一个数字,其值必须小于等于指定的最大值 |
@Size(max=, min=) | 被注释的元素的大小必须在指定的范围内 |
@Pattern(regex=) | 被注释的元素必须符合指定的正则表达式 |
被注释的元素必须是电子邮箱地址 | |
@DecimalMin(value) | 被注释的元素必须是一个数字,其值必须大于等于指定的最小值 |
@DecimalMax(value) | 被注释的元素必须是一个数字,其值必须小于等于指定的最大值 |
@Digits(integer, fraction) | 被注释的元素必须是一个数字,其值必须在可接受的范围内 |
@Past | 被注释的元素必须是一个过去的日期 |
@Future | 被注释的元素必须是一个未来的日期 |
@Length(min=,max=) | 被注释的字符串的大小必须在指定的范围内 |
@Range(min=,max=) | 被注释的元素必须在合适的范围内 |
@AssertTrue | 被注释的元素必须为 true |
@AssertFalse | 被注释的元素必须为 falseweb |