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 |