struts-config.xml中action配置 --> attribute和name的区别

在实际开发中,我们一般不必去理会attribute属性的设置。  

在一般情况下,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

在《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的源代码:

  1.     /**
  2.     *创建或者取回formbean方法
  3.     *该方法在:org.apache.struts.util.RequestUtils中
  4.     */
  5.     public static Actionform createActionform(HttpServletRequest request, ActionMapping mapping, ModuleConfig moduleConfig, ActionServlet servlet) {
  6.         //Is there a form bean associated with this mapping?
  7.         //得到action mapping中attribute的值
  8.         String attribute = mapping.getAttribute();
  9.         Actionform instance = null;
  10.         HttpSession session = null;
  11.         //yes!!就在这里了,把创建以后的actionform放在request或者session里,看到放入的名字了么,就是mapping.getAttribute();
  12.         if ("request".equals(mapping.getScope())) {
  13.             instance = (Actionform) request.getAttribute(attribute);
  14.         } else {
  15.             session = request.getSession();
  16.             instance = (Actionform) session.getAttribute(attribute);
  17.         }
  18.     }

下面又有一个问题浮出水面:如果我没有在action mapping中指定attribute呢,那struts 是如何解决的?答案很简单,如果单从结果上看,此时struts使用的name的值,为什么呢,看struts源代码:

  1.     /**
  2.     * The request-scope or session-scope attribute name under which our
  3.     * form bean is accessed, if it is different from the form bean's
  4.     * specified <code>name</code>.
  5.     * 该代码在:org.apache.struts.config.ActionConfig中
  6.     */
  7.     protected String attribute = null;
  8.     public String getAttribute() {
  9.         //yes!!!!就在这里,看到了吧,如果你没有设定attribute,那么struts 会把name的值拿过来用。呵呵。。。
  10.         if (this.attribute == null) {
  11.             return (this.name);
  12.         } else {
  13.             return (this.attribute);
  14.         }
  15.         public void setAttribute(String attribute) {
  16.             if (configured) {
  17.                 throw new IllegalStateException("Configuration is frozen");
  18.             }
  19.         this.attribute = attribute;
  20.     }

举个例子:当我们不再需要存储在request或session中的actionForm时,我们就应该把这个actionForm从request或session中删除掉,而不应该再让actionForm随request或session传递到页面中去,这时就要用到这个key值了。

  1.     //示例代码(action.execute中):   
  2.     // remove   unused   form   
  3.     if("request".equals(actionMapping.getScope())) {   
  4.         httpServletRequest.removeAttribute(actionMapping.getName());   
  5.     }   
  6.     else if("session".equals(actionMapping.getScope())) {   
  7.         httpServletRequest.getSession().removeAttribute(actionMapping.getName());   
  8.     }

当然,当你配置了attribute属性时,上面示例代码中的actionMapping.getName()就要用actionMapping.getAttribute()替代了。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值