2008年9月20日

struts-config.xml中 action 的attribute属性与name属性

1、在一般情况下,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的源代码:

/**
*创建或者取回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;
}

posted @ 2008-09-20 13:37 chenkai 阅读(683) | 评论 (1)编辑 收藏

Request的getParameter和getAttribute方法的区别

当两个Web组件之间为转发关系时,转发源会将要共享 request范围内的数据先用setAttribute将数据放入到HttpServletRequest对象中,然后转发目标通过 getAttribute方法来取得要共享的数据。而MVC中用的就是Web组件之间的转发啊!真是笨,怎么当时没有想到呢?

      下面整理一下getParameter和getAttribute的区别和各自的使用范围。

      (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法

      (2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,例如假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:

      <a  href="authenticate.jsp?username=wolf">authenticate.jsp  </a>

      或者:

      <form  name="form1"  method="post"  action="authenticate.jsp">
          请输入用户姓名:<input  type="text"  name="username">
          <input  type="submit"  name="Submit"  value="提交">
      </form>

       在authenticate.jsp中通过request.getParameter("username")方法来获得请求参数username:

       <%  String  username=request.getParameter("username");  %>

       (3)当两个Web组件之间为转发关系时,转发目标组件通过getAttribute()方法来和转发源组件共享request范围内的数据。

        假定  authenticate.jsp和hello.jsp之间为转发关系。authenticate.jsp希望向hello.jsp传递当前的用户名字,  如何传递这一数据呢?先在authenticate.jsp中调用setAttribute()方法:

        <%
        String  username=request.getParameter("username");
        request.setAttribute("username",username);
        %>

        <jsp:forward  page="hello.jsp"  />

        在hello.jsp中通过getAttribute()方法获得用户名字:

        <%  String  username=(String)request.getAttribute("username");  %>
        Hello:  <%=username  %>

        从更深的层次考虑,request.getParameter()方法传递的数据,会从Web客户端传到Web服务器端,代表HTTP请求数据。request.getParameter()方法返回String类型的数据。

        request.setAttribute()和getAttribute()方法传递的数据只会存在于Web容器内部,在具有转发关系的Web组件之间共享。这两个方法能够设置Object类型的共享数据。

        request.getParameter()取得是通过容器的实现来取得通过类似post,get等方式传入的数据。

        request.setAttribute()和getAttribute()只是在web容器内部流转,仅仅是请求处理阶段。

        getAttribute是返回对象,getParameter返回字符串

        总的来说:request.getAttribute()方法返回request范围内存在的对象,而request.getParameter()方法是获取http提交过来的数据。

posted @ 2008-09-20 09:57 chenkai 阅读(2777) | 评论 (0)编辑 收藏

<2008年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(2)

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜