Use the validator framework and file upload in spring MVC in November 30

Source: Internet
Author: User

First, we reviewed the Form Verification and business logic verification failures in spring MVC, and then returned to the form page to display the entire internal operation process of the error message.
When an error occurs in form verification, the process returned to the form registration page is automatically completed by the processformsubmission method of the default simpleformcontroller. Wang Tao forgets this and covers the processformsubmission method, the business logic verification is directly executed in the processformsubmission method. As a result, the form verification fails and cannot return to the form registration page.
View the help documentation to learn the detailed workflow of simpleformcontroller, especially the onsubmit (Object command, bindexception errors) method to learn how to write the code that failed the business logic verification.
Protected modelandview onsubmit (Object command,
Bindexception errors)
{
Errors. adderror (New fielderror ("name", "Duplicate username "));
Return new modelandview (this. getformview (), errors. GetModel ());
}
The differences between the onsubmit method and the dosubmitaction method are analyzed.

It explains how to integrate the validator framework into the design idea of spring:
Class myvalidator implements validator
{
Boolean supports (class clazz)
{
Why can't I use: usercommand. Class. Equals (clazz );?
This is mainly because the class does not overwrite the equals method.
}

Void validate (Object OBJ, errors)
{
}
}
The written verification method should be defined as follows:
Public static Boolean validatefloatrange (Java. Lang. Object bean,
Validatemedition va,
Field field,
Errors errors)
{
......
}
Therefore, the validate (Object OBJ, errors) method of the myvalidator class should be written as follows:
Void validate (Object OBJ, errors)
{
Validateresources resources = new validateresources (inputstream []);
Validator = new validator (resources, "/logon ");
Valdiator. setparameter (validator. param_bean, OBJ );
Validator. setparameter ("org. springframework. validation. Errors", errors );
Validator. Validate ();
}
I asked Wei yidong, Which of the following is more complex than the code for verifying the form information written without the validator framework and the code written using the validator framework, and which one is he willing to use? Obviously, the code written using the validator framework is more complex and he is more willing to perform form verification using the previous method. Why should we use the validator framework? This is used to encapsulate and configure code reuse and validation logic. If he wants to have a higher development position in the company and he wants to get more salary, he should use the validator framework so that other low-level programmers can not write verification code, the verification method written by Wei yidong is directly inserted in the validator framework, and the boss will put part of the Work money saved by others into Wei yidong's pocket!

Wei yidong, don't be too happy! Someone has long done the job you want. This is the spring module. It imitates the validator framework implemented by struts and implements validator of sring MVC that calls the validator framework. Now, let's take a look at how to use the validtor provided by the spring module!

Next, I will explain how to use the validtor provided by spring module to perform form verification. However, since spring does not support dynamic command objects, we are still unable to play a complete view layer without writing a line of code like struts.

Yang song found that the spring module 1.0 in appfuse does not have the validatewhen verification rule. However, we currently use spring module 6.0, so don't worry about this issue.

Next, let's talk about the file upload in struts and the file upload in spring MVC. The file upload in strtus is very simple, while the file upload in spring MVC is slightly more complicated, first, you must configure commonsmultipartresolver in the configuration file. Spring MVC will process multipart data requests. Then, you can use commonsmultipartfile to process large upload files without worrying about array out-of-bounds issues. Wang Tao can go on to explain how bytearraymultipartfileeditor calls the getbyte method of commonsmultipartfile to convert uploaded files into byte arrays, it seems that he has his own programming experience.

 

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.