Is the positive body of the attribute of maid mpping

Source: Internet
Author: User
This is the correct attribute of the maid mpping!

In many cases, attribute and name attributes in action mapping have been bothering me. Today I think it is time to make up my mind to unmount this nail.
Read some documents .....

In Chapter 4 "inserting the struts application" in "programming Jakarta Struts ",
Attributes: (page 102)
++
Atribute:
++
The name of the request or session scope attribute under which the form bean for this action can be accessed.
A value is only allowed here if there is a form bean specified in the name attribute. This attribute is
Optional and has no default value.

++
Name:
++
The name of the form bean, if any, that is associated with this action. This value must be the name attribute
From one of the form-bean elements defined earlier. This attribute is optional and has no default value.

At first, it was hard to distinguish the two. However, I have carefully read the strutsSource codeIn the future, we will be enlightened...

The following mainly explains the attribute. It should be because no one knows the name attribute (Haha ...)

Explanation: When struts instantiates an actionform, there are two situations: if it already exists, it will be retrieved from the memory; if it is instantiated for the first time, it will be created and put into the memory.
In this case, there is a question: what does struts use to retrieve and create an actionform? The answer is the value of attribute. Let's go to the struts SourceCode:

/**
* Create or retrieve the formbean Method
* This method is in org. Apache. Struts. util. requestutils.
*/
Public static actionform createactionform (
Httpservletrequest request,
Actionmapping mapping,
Moduleconfig,
Actionservlet servlet ){
....
...
// Is there a form bean associated with this mapping?
// Obtain the attribute value in action mapping.
String attribute = mapping. getattribute ();
....
....
Actionform instance = NULL;
Httpsession session = NULL;
// Yes !! Here, put the created actionform in the request or session. Can you see the entered name, that is, mapping. getattribute ();
If ("request". Equals (mapping. getscope ())){
Instance = (actionform) request. getattribute (attribute );
} Else {
Session = request. getsession ();
Instance = (actionform) Session. getattribute (attribute );
}
...
...

}

Another problem arises: If I didn't specify attribute in action mapping, how does struts solve it?
The answer is very simple. If we look at the results, the name value of Struts is used at this time. Why? Look at the struts source code:

/**
* The request-scope or session-scope attribute name under which our
* Form bean is accessed, if it is different from the form Bean's
* Specified <code> name </code>.
* The code is in org. Apache. Struts. config. actionconfig.
*/
Protected string attribute = NULL;

Public String getattribute (){
// Yes !!!! Here we can see that, if you do not set the attribute, Struts will take the name value for use. Haha...
If (this. Attribute = NULL ){
Return (this. Name );
} Else {
Return (this. Attribute );
}
}

Public void setattribute (string attribute ){
If (configured ){
Throw new illegalstateexception ("configuration is frozen ");
}
This. Attribute = attribute;
}

What if someone asks if I have no name in action mapping ?.... Fainting ~~~~ '''

Everyone has a good time learning!

This post was originally posted in the struts area of cjw (http://www.chinajavaworld.net)

Jplateau

copyright jplateau, all rights reserved

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.