struts中action的attribute和name的区别
2009-08-19 15:50
239 查看
在一般情况下,actionForm是被存储在一定的scope中(request或session,通过action的scope属性来配置),当我们在配置时,指定name而不指定attribute,那么指定的name值就作为actionForm存储在scope中的key值,我们可以在action中通过httpServletRequest.getAttribute("指定的name属性值")来获得这个actionForm; 当我们既配置了name又配置了attribute,那么actionForm存储在scope中的key值就采用attribute属性指定的值了,这时要通过httpServletRequest.getAttribute("指定的attribute属性值")来获得actionForm,此时通过httpServletRequest.getAttribute("指定的name属性值")是不能获得actionForm的。
所以,是否配置attribute属性就决定了actionForm存储在scope中的key值是采用name,还是采用attribute
2、 在《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的源代码:
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();
if ("request".equals(mapping.getScope())) {
instance = (Actionform) request.getAttribute(attribute);
} else {
session = request.getSession();
instance = (Actionform) session.getAttribute(attribute);
}
。。。
。。。
}
下面又有一个问题浮出水面:如果我没有在action mapping中指定attribute呢,那struts 是如何解决的?
答案很简单,如果单从结果上看,此时struts使用的name的值,为什么呢,看struts源代码:
protected String attribute = null;
public String getAttribute() {
//yes!!!!就在这里,看到了吧,如果你没有设定attribute,那么struts 会把name的值拿过来用。呵呵。。。
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;
所以,是否配置attribute属性就决定了actionForm存储在scope中的key值是采用name,还是采用attribute
2、 在《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的源代码:
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();
if ("request".equals(mapping.getScope())) {
instance = (Actionform) request.getAttribute(attribute);
} else {
session = request.getSession();
instance = (Actionform) session.getAttribute(attribute);
}
。。。
。。。
}
下面又有一个问题浮出水面:如果我没有在action mapping中指定attribute呢,那struts 是如何解决的?
答案很简单,如果单从结果上看,此时struts使用的name的值,为什么呢,看struts源代码:
protected String attribute = null;
public String getAttribute() {
//yes!!!!就在这里,看到了吧,如果你没有设定attribute,那么struts 会把name的值拿过来用。呵呵。。。
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;
相关文章推荐
- struts-config.xml中action配置--> attribute和name的区别(转)
- struts-config.xml中action配置 --> attribute和name的区别
- struts-config中action的attribute属性与name属性的关系
- struts-config中action 的attribute属性与name属性
- struts-config.xml中 action 的attribute属性与name属性
- struts-config.xml中 action 的attribute属性与name属性
- Struts Ongl valueStack与ActionContent.ValueStack的区别
- Struts应用出现的错误及解决方法:There is no Action mapped for namespace / and action name login
- Structs 在Struts.xml中配置action时,action的name属性最好首字母大写
- struts1 中DispatchAction 与action 的区别,以及DispatchAction的用法含义
- Struts错误:There is no Action mapped for namespace / and action name
- Struts Action 与ActionSupport的区别
- Action的mapping.findFoward(forwardName)必须要在struts-config.xml中的对应的action节点配置一个forward节点
- servlet和struts中action的区别
- Struts1.x和Struts2.0的Action的区别
- struts <s:form action=""> 和 <s:submit action=""> 的区别
- struts 配置出现错误 There is no Action mapped for namespace [/] and action name [findPage] associated with
- 部署Struts遇到“There is no Action mapped for namespace [/] and action name [...] associated with .."问题
- struts-config.xml中action元素的attribute属性的意义
- 【Struts1】DynaValidatorForm和DynaValidatorActionForm的区别