`
holmessong
  • 浏览: 14424 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论

action-mappings之attribute属性 [转]

阅读更多

action-mappings之attribute属性(zz)

在很多时候,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();
       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源代码:

   /**
    * 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!!!!就在这里,看到了吧,如果你没有设定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;
   }

分享到:
评论

相关推荐

    struts2的入门开发

    &lt;action attribute="loginForm" input="/login.jsp" name="loginForm" path="/login" scope="request" type="test.action.LoginAction"&gt; &lt;/action&gt; &lt;/action-mappings&gt; &lt;message-resources parameter="test....

    struts配置元素详解

    ”1.0” encoding=”ISO... &lt;action-mappings&gt; &lt;action path="" attribute="" type="" name="" scope="" validate="" input="" / &gt; &lt;/action-mappings&gt; &lt;message-resources / &gt; &lt;plug-in /&gt; &lt;/struts-config&gt;

    SSH开发纪要整合解决四大问题(中文、jar包冲突、延时加载、模块化)文档

    &lt;action-mappings &gt; &lt;action attribute="addForm" input="/add.jsp" --错误时跳转的页面 name="addForm" --Frombean的名字 path="/add" --路径 scope="request" --作用域 type=...

    struts基于mvc的开发代码

    &lt;action-mappings &gt; &lt;action attribute="testForm" input="/test.jsp" name="testForm" path="/test" scope="request" type="com.yourcompany.struts.action.TestAction" /&gt; &lt;action attribute="test1Form...

    Myeclipse开发struts+hibernate+spring新手入门--环境配置---项目开发示例

    14 &lt;action attribute="LoginForm" input="/login.jsp" name="LoginForm" path="LoginAction" scope="request" type="com.test.web.action.LoginAction" validate="true"&gt; 15 16 17 &lt;/action&gt; 18 19 &lt;/action-...

    Authorization in ASP.Net MVC using XML Configuration.

    Asp.net MVC comes with built in Authorization filter attribute that you can use on your Controller and Action to define the role that can access corresponding Controller or Action. This approach will ...

    Struts原理、开发及项目实施

    RegUserForm "/&gt; &lt;/form-beans&gt; &lt;br/&gt;&lt;action-mappings&gt; &lt;action path="/regUserAction" type=" org.cjea.Struts.example.RegUserAction " attribute=" regUserForm " scope="request...

    Struts in Action中文版

    2.6.2. Struts的强项........................................................................................................58 Struts in Action 中文版 Lastest Revised:10/14/2005 10:27:00 AM ...

    struts in Action

    1. 介绍.........................................................................................................18 1.1. 关于本书...........................................................................

    ORACLE OSB开发指南

    ORACLE OSB开发指南,英文版。 目录: Part I IDE Help for Oracle Service Bus 1 Introduction to Oracle Service Bus 2 Tasks Working with Projects, Folders, Resources, and Configurations .....................

Global site tag (gtag.js) - Google Analytics