<form-beans>
   <form-bean name="stuForm" type="my.form.StuForm"></form-bean>
  </form-beans>
  <global-exceptions />
  <global-forwards>
   <forward name="myForward" path="/MyJsp.jsp"/>
  </global-forwards>
  <action-mappings >
    <action
      name="stuForm"
      path="/myAction"
      input="index.jsp"
      scope="request"
      type="my.action.MyAction" />  </action-mappings>action里也没有任何setAttribute之类的代码,
为啥我在MyJsp.jsp里面用
<%out.print(((StuForm)request.getAttribute("stuForm")).getName()); %>
可以正常输出呢?

解决方案 »

  1.   

    开始第一次表单提交的时候根据这个cope="request",会把这个stuForm存储在这个范围内,这里你存储在request内,这不是你存储的,而是struts存储的!所以你可以取出来使用!
      

  2.   

    那attribute这个属性有啥用呢?
      

  3.   

    这个属性的意思是:设置和Action关联的ActionForm Bean在request或session范围内的属性key。
    例如,假定Form Bean存在与request范围内,并且此项设为“myBean”,那么request.getAttribute("myBean")
    就可以返回该Bean的实例。此项为可选项。
      

  4.   

    可以得到保存在request对象中的变量
      

  5.   


    楼上的直接抄书上的吧
    这个我也看到了,不过不是我关心的,我没用attribute照样可以getAttribute,那这个属性不是多余的吗
      

  6.   


    当你没有设置attribute属性的时候,request.getAttribute("myBean") 的myBean是你在<form-bean />里定义的name,当你设置attribute属性的时候是,应该用attribute的属性来调用request.getAttribute("myBean") 方法,这时候myBean是attribute的属性!
      

  7.   

    下面又有一个问题浮出水面:如果我没有在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;
    }