Spring frame--day04 parameter checksum file upload

Source: Internet
Author: User

One, the parameter check

Springmvc We can use a third-party calibration framework to achieve the validation of request parameters, commonly used hibernate check framework Validation1.pom import required Dependency Hibernate-validator-4.3.2.final.jar Jboss-logging-3.1.0.cr2.jar validation-api-1.0.0.ga.jar2. Configure Authenticator <!--configuration Validator-<bean id= "Myvalidator" CLA ss= "Org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" > <property name= " Providerclass "value=" Org.hibernate.validator.HibernateValidator "></property> </bean> &LT;MVC: Annotation-driven validator= "Myvalidator"/>3. Create an entity class, configure the validation rule with annotations public class Phone {private Integer ID; @NotBlank (message = "Brand? ") Private String brand; @NotBlank (message = "no model?") ") Private String model; @Max (value = 99999, message = "too expensive?" ") 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"; Note: In the parameter list, the checked parameters (phone) and Bindingresult must be adjacent and guaranteed in order. 5.JSP Display error message product?? Card: <input type= "text" Name= "brand" ><span style= "color:red;" >${brandmsg}</span><br> type?? Number: <input type= "text" name= "model" ><span style= "color:red;" >${modelmsg}</span><br> price?? Lattice: <iNput type= "text" name= "price" ><span style= "color:red;" >${pricemsg}</span><br>
two, file upload in 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-data4.完成以上设置,就可以在controller方法中接收文件了: @RequestMapping("/upload") public String upload(@RequestParam("fileName") CommonsMultipartFile file) throws Exception { } 注意该参数需要通过@RequestParam指定参数名

常用校验注解
@Null The annotated element must be null
@NotNull The annotated element must not be null
@NotEmpty The annotated string must be non-empty
@NotBlank The validation string is non-null and must be longer than 0
@Min (value) The annotated element must be a number whose value must be greater than or equal to the specified minimum value
@Max (value) The annotated element must be a number whose value must be less than or equal to the specified maximum value
@Size (max=, min=) The size of the annotated element must be within the specified range
@Pattern (regex=) The annotated element must conform to the specified regular expression
@Email The annotated element must be an e-mail address
@DecimalMin (value) The annotated element must be a number whose value must be greater than or equal to the specified minimum value
@DecimalMax (value) The annotated element must be a number whose value must be less than or equal to the specified maximum value
@Digits (integer, fraction) The annotated element must be a number whose value must be within an acceptable range
@Past The annotated element must be a past date
@Future The annotated element must be a future date
@Length (min=,max=) The size of the annotated string must be within the specified range
@Range (min=,max=) The annotated element must be within the appropriate range
@AssertTrue The annotated element must be true
@AssertFalse

The annotated element must be false

Spring frame--day04 parameter checksum file upload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.