struts-config.xml中action配置--> attribute和name的区别(转)
来源:互联网 发布:java中的集合面试题 编辑:程序博客网 时间:2024/04/27 14:38
在实际开发中,我们一般不必去理会attribute属性的设置。
在一般情况下,actionForm是被存储在一定的scope中(request或session,通过act
所以,是否配置attribute属性就决定了actionForm存储在scope中的key值是采用name,还是采用attribute
在《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 act
A value is on
optional and has no default value.
++++++++++++++++++++++++
name:
++++++++++++++++++++++++
The name of the form bean, if any, that is associated with this act
from on
最初看这些真的还是不好区分这两者。不过在仔细看过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?
- //得到act
ion 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);
- }
- }
下面又有一个问题浮出水面:如果我没有在act
- /**
- * 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 <co
de>name</co de>. - * 该代码在:org.apache.struts.config.ActionConfig中
- */
- 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;
- }
举个例子:当我们不再需要存储在request或session中的actionForm时,我们就应该把这个actionForm从request或session中删除掉,而不应该再让actionForm随request或session传递到页面中去,这时就要用到这个key值了。
- //示例代码(act
ion.execute中): - // remove unused form
- if("request".equals(actionMapping.getScope())) {
- httpServletRequest.removeAttribute(actionMapping.getName());
- }
- else if("session".equals(actionMapping.getScope())) {
- httpServletRequest.getSession().removeAttribute(actionMapping.getName());
- }
当然,当你配置了attribute属性时,上面示例代码中的actionMapping.getName()就要用actionMapping.getAttribute()替代了。
- struts-config.xml中action配置--> attribute和name的区别(转)
- struts-config.xml中action配置 --> attribute和name的区别
- struts中action的attribute和name的区别
- 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属性的意义
- 阿翔编程学-struts-config.xml中action元素的attribute属性的意义
- Struts1中web.xml和struts-config.xml的配置
- Struts基础:struts-config.xml中action的scope属性
- struts-config.xml 中action 属性配置中的scope问题
- action attribute和name 属性区别
- action attribute和name 属性区别
- action attribute和name 属性区别
- Struts中web.xml和struts-config.xml配置详解
- struts-config.xml中配置action的input属性的作用
- struts-config.xml中action元素的parameter属性
- Using Android Socket
- The issue of template definition and declaration separation
- orcal 优化
- Windows平台下JNI简单示例
- 设置F7过滤界面为左树右边
- struts-config.xml中action配置--> attribute和name的区别(转)
- 为C# Window Form程序设置热键。
- 数据库树型结构中获取目录串的高性能方法,sys_connect_by_path与自定义函数性能对比
- 79. Bad news has wings. 好事不出门,坏事传千里
- 黑马程序员之移动通信分析学习
- 循序渐进Lu例程
- 在文件每行后加上指定符号的批处理
- 黑马程序员之银行系统的学习分析
- 适易信息管理系统架构、配置实现增删改查思路及总结