Springmvc Key Issues

Source: Internet
Author: User

Next I will write an article about the two issues that may concern you.

1. How does the Controller encapsulate data?

To answer this question, I have to say simpleformcontroller.

Simpleformcontroller is the specific implementation of abstractformcontroller. It allows you to configure the successful view through the successview and formview attributes in the configuration file (the page to be switched after the form is successfully submitted) and form view (display the page of the form); if the submission is invalid (there are three possibilities: 1. validator error. 2. BIND error. That is to say, a type conversion error occurs during the process of extracting parameters from the request and encapsulating them to the command. For example, a string containing letters is converted to an integer. 3. onbindandvalidate () method error), it is returned to the form view again. If the submission is valid, the onsubmit () method defaults to the successful page, of course, you can override this method to fill in the information you want to return before turning.

The workflow of simpleformcontroller is similar to that of abstractformcontroller. The only difference is that you do not have to implement showform () and processformsubmission () on your own (). The showform () method has been implemented by the simpleformcontroller class and is limited to final. You cannot override this class in the subclass that inherits simpleformcontroller. Although the processformsubmission () method can be overwritten, it can meet almost all requirements, so generally no one will rewrite it.
The process is as follows:
When the GET request comes, it will be processed as follows:
1) The request is passed to a controller object
2) Call the formbackingobject () method to create an instance of the command object.
3) Call initbinder () to register the required type converter
4) Call the showform () method and return the view to be presented to the user. If the "bindonnewform" attribute is set to true, servletrequestdatabinder will fill in the initial request parameter with a new form object, and run the onbindonnewform () method.
5) Call the referencedata () method to display the relevant data to the user. Select annual information for User Login
6) return the view specified by formview.

When the POST request comes, it will be processed as follows:
1) if the sessionform attribute is not set, call the formbackingobject () method to create an instance of the command object. Otherwise, the form object is obtained from the session.
2) write the parameters sent from the request to the command object. Check its source code and you will find that it is done in this way:

Servletrequestdatabinder binder = createbinder (request, command );
Binder. BIND (request );

3) execute the onbind () method. After binding the data, perform some self-made modification actions on the form data before verifying the data.
4) if it is set to require verification (the validateonbinding attribute is set), the validator class is called for data verification.
5) Call the onbindandvalidate () method, which allows custom data binding and verification.
6) execute processformsubmission () to verify that the errors object contains no errors. If an error exists, execute showform () to return to the fill in form page; otherwise, execute the onsubmit () method to submit the form, go to the success page.

2. Usage of <spring: kind>

In the Spring framework system, we can say that the Protocol is the least, and the least restrictive is the performance Layer Technology. Unlike struts, many labels have been changed, and some functions are bound to tags. Sping also defines some labels, but these labels only provide convenience for users and do not provide additional functions or effects.

The number of sping tags is relatively small and there are no more than 10. Here we only introduce the most commonly used <sping: bind>, also known as spring binding.

The path attribute of <sping: bind> is bound to the attribute in the form. In this way, $ {status. expression} represents the name of the attribute, $ {status. expression} indicates the attribute value. If the path attribute is "XXX. *", it is bound to all attributes of the form.

The preceding example form has two attributes: username and password. There are actually two binding methods. The first one is like the one described in the previous article http://javacrazyer.iteye.com/blog/790834. the second one is as follows:

......
<Spring: bind Path = "loginform">
Username: <input name = "username" type = "text" value = "$ {command. Username}"/> <br>
Password: <input name = "password" type = "password" value = "$ {command. Password}"/>
</Spring: bind>

In this case, the error message for the user name and password is not displayed as it was previously.


 

So verify what I want to say below:

The <sping: bind> label does not play any role when you enter the form page for the first time. Instead, if a bindexception error occurs after the form is submitted, you can display previous input in input.

 

When it comes to error information processing, I have to talk a lot more about it. Let's take a look at the controllerin my previous article http://javacrazyer.iteye.com/blog/790834.

Errors. Reject ("CCC", "incorrect user name or password! ");
Return new modelandview (getformview (), errors. GetModel ());

It calls the reject method of bindexception, and then calls the GetModel () method of bindexception to return the error along with the form information to the form page for display.

The first parameter of the reject method is the error code. If an international resource is set, the error entries corresponding to the error code in the resource file are displayed. If no international resource is set, the second parameter of the reject method is displayed.

The disadvantage of the reject method is that in the presentation layer, the error message does not belong to the field, that is, it cannot be determined whether the username is incorrect or the password is incorrect. To solve this problem, you can use the rejectvalue method, which is also a more general method. The rejectvalue method is defined as follows:

Rejectvalue (string field, string errorcode, object [] errorargs, string defaultmessage)


The first parameter specifies the form field, that is, username or password, so that you can identify the problem. The second parameter is the same as the first parameter of the reject method, and the error code is formulated; the third candidate number defines placeholders in the resource file. The fourth parameter is the same as the second parameter of the reject method. The rejectvalue method also has a simplified definition.

Rejectvalue (string field, string errorcode, string defaultmessage)


The preceding method is used in the Controller. If a bindexception error occurs after the preceding method is used, the error message is displayed when the form page is returned. How can this problem be solved?

In the above/WEB-INF/JSP/login. jsp, because the reject method is used in the controller, it can only be displayed, if we use the rejectvalue method, such as modifying logincontroller:

Errors. rejectvalue ("username", "nameerr", null, "username error ");
Errors. rejectvalue ("password", "passerr", null, "Incorrect password ");

In this way, you can change the page to the last form.

<Spring: bind Path = "command. username">
Name <input type = "text" name = "$ {status. Expression}" value = "$ {status. Value}"/>
<Font color = "red"> <C: Out value = "$ {status. errormessage}"/> </font> <br/>
</Spring: bind>
<Spring: bind Path = "command. Password">
Password <input type = "password" name = "$ {status. Expression}" value = "$ {status. Value}"/>
<Font color = "red"> <C: Out value = "$ {status. errormessage}"/> </font> <br/>
</Spring: bind>

In this way, the error message is bound to the corresponding field. Of course, you can also output a single brain without specifying a field.

At this point, we still don't see how I explain the value of command. As to why it is not a command rather than other values
This is because the setcommandclass method is a method in abstractcontroller, and this
The default value public static final java. Lang. String default_command_name = "command"; public static final java. Lang. String default_command_name = "command ";
No, it is called command, so you can use it boldly in the tag.

 

3. There are several small issues at last

(1) A common error:
The following error occurs when you directly access the JSP page containing the spring: bind tag without using the controller:
Javax. servlet. servletexception: Neither errors instance nor plain target object for bean name 'person 'available as request attribute
Solution:
Http://spring.jactiongroup.net/viewtopic.php? P = 5482

(2) Meaning of asterisk (*)
Global and All field errors,
# Use wildcard (*) in place of the property name
<Spring: bind Path = "company. *">
<C: foreach items = "$ {status. errormessages}" Var = "error">
C: Out value = "$ {error}"/> <br/>
</C: foreach>
</Spring: bind>

(3) Meaning of command
3-1 commandclass
It is equivalent to the actionform in struts, which is used to encapsulate data in V and facilitate use in C.

3-2 commandname
Used to specify the object to which the JSP data is bound. The default value is command.
For example, in the following configuration, commandname is command
<Spring: bind Path = 'COMMAND. Email '>
<TD> <input type = 'text' name = '$ {status. Expression }'
Value = '$ {status. Value}' size = '30'
Maxlength = '000000'> </TD> </tr>
</Spring: bind>
Because it is the default value, it does not need to display the declaration in the controller.

If setcommandname ("me") is set in controller, the preceding configuration file needs to be changed:
<Spring: bind Path = 'me. Email '>
<TD> <input type = 'text' name = '$ {status. Expression }'
Value = '$ {status. Value}' size = '30'
Maxlength = '000000'> </TD> </tr>
</Spring: bind>
Simple.


(4) An issue worth attention (original article link)


The usage of a common <spring. Bind> is similar:

<Spring: bind Path = "user. Age">
<Input type = "text" name = "Age" value = "$ {status. Value}">
<Font color = "red" >$ {status. errormessage} </font>
</Spring: bind>

Note that the name attribute value of <input> must match the path attribute of <spring: bind>. Otherwise, it cannot be bound!

For example, the following code cannot be bound.

<Spring: bind Path = "user. Age">
<Input type = "text" name = "theage" value = "$ {status. Value}">
<Font color = "red" >$ {status. errormessage} </font>
</Spring: bind>

To avoid mistakes, we strongly recommend that you bind the following methods:

<Spring: bind Path = "user. Age">
<Input type = "text" name = "$ {status. Expression}" value = "$ {status. Value}">
<Font color = "red" >$ {status. errormessage} </font>
</Spring: bind>

 

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.