|
为actin mpping之attribute属性正身!(1)
在很多时候,action mapping中的attribute 和 name 这两个属性一直困扰我,今天我觉得是该痛下决心拔掉这颗钉子的时候了。 翻看一些资料。。。。。 在《Programming Jakarta Struts》这本书中的第四章“Configuring the Struts Application”中这样一段说明来分别阐述这两 个属性:(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.
最初看这些真的还是不好区分这两者。 不过在仔细看过struts的源代码以后,豁然开朗。。。 下面主要对attribute进行解释,应为没有人会对name属性不了解的(呵呵。。。) 解释:在struts实例化actionform的时候,有两种情况:如果已经存在,那么从内存中取回;如果第一次实例化,那么创建,并放入内存。 这样就有一个问题了,struts是根据什么来取回并创建actionform的呢,答案就是attribute的值。让我们进入struts的源代码:
/** *创建或者取回formbean方法 *该方法在:org.apache.struts.util.RequestUtils中 */ public static Actionform createActionform( HttpServletRequest request, ActionMapping mapping, ModuleConfig moduleConfig, ActionServlet servlet) { 。。。。 。。。 // Is there a form bean associated with this mapping? //得到action mapping中attribute的值 String attribute = mapping.getAttribute(); 。。。。 。。。。 Actionform instance = null; HttpSession session = null; //yes!!就在这里了,把创建以后的actionform放在request或者session里,看到放入的名字了么,就是mapping.getAttribute();
|