1、在一般情況下,actionForm是被存儲(chǔ)在一定的scope中(request或session,通過(guò)action的scope屬性來(lái)配置),當(dāng)我們?cè)谂渲脮r(shí),指定name而不指定attribute,那么指定的name值就作為actionForm存儲(chǔ)在scope中的key值,我們可以在action中通過(guò)httpServletRequest.getAttribute("指定的name屬性值")來(lái)獲得這個(gè)actionForm; 當(dāng)我們既配置了name又配置了attribute,那么actionForm存儲(chǔ)在scope中的key值就采用attribute屬性指定的值了,這時(shí)要通過(guò)httpServletRequest.getAttribute("指定的attribute屬性值")來(lái)獲得actionForm,此時(shí)通過(guò)httpServletRequest.getAttribute("指定的name屬性值")是不能獲得actionForm的。
所以,是否配置attribute屬性就決定了actionForm存儲(chǔ)在scope中的key值是采用name,還是采用attribute
2、 在《Programming Jakarta Struts》這本書(shū)中的第四章“Configuring the Struts Application”中這樣一段說(shuō)明來(lái)分別闡述這兩
個(gè)屬性:(102頁(yè))
++++++++
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.
最初看這些真的還是不好區(qū)分這兩者。不過(guò)在仔細(xì)看過(guò)struts的源代碼以后,豁然開(kāi)朗。。。
下面主要對(duì)attribute進(jìn)行解釋,應(yīng)為沒(méi)有人會(huì)對(duì)name屬性不了解的(呵呵。。。)
解釋:在struts實(shí)例化actionform的時(shí)候,有兩種情況:如果已經(jīng)存在,那么從內(nèi)存中取回;如果第一次實(shí)例化,那么創(chuàng)建,并放入內(nèi)存。
這樣就有一個(gè)問(wèn)題了,struts是根據(jù)什么來(lái)取回并創(chuàng)建actionform的呢,答案就是attribute的值。讓我們進(jìn)入struts的源代碼:
/**
*創(chuàng)建或者取回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!!就在這里了,把創(chuàng)建以后的actionform放在request或者session里,看到放入的名字了么,就是mapping.getAttribute();
if ("request".equals(mapping.getScope())) {
instance = (Actionform) request.getAttribute(attribute);
} else {
session = request.getSession();
instance = (Actionform) session.getAttribute(attribute);
}
。。。
。。。
}
下面又有一個(gè)問(wèn)題浮出水面:如果我沒(méi)有在action mapping中指定attribute呢,那struts 是如何解決的?
答案很簡(jiǎn)單,如果單從結(jié)果上看,此時(shí)struts使用的name的值,為什么呢,看struts源代碼:
/**
* 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>.
*該代碼在:org.apache.struts.config.ActionConfig中
*/
protected String attribute = null;
public String getAttribute() {
//yes!!!!就在這里,看到了吧,如果你沒(méi)有設(shè)定attribute,那么struts 會(huì)把name的值拿過(guò)來(lái)用。呵呵。。。
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;
}
文章出處:http://www.diybl.com/course/3_program/java/javashl/2008131/98144.html