在没用struts1.2 的时候可以用request.setAttribute()  给servlet 传值,servlet用request.getAttribute() 接收该值。在运用了struts1.2 后request.setAttribute()  给一个action ,就无法用request.getAttribute() 取得该值,请问在struts1.2中要达到此目的一般这样处理?假设我传递一个bean的类型过去,我试着在页面用一个隐藏空间来设置这个bean值,但是actionForm无法接受bean类型的数据,大家帮帮忙

解决方案 »

  1.   

    不可能的.完全可以用struts1.2来setAttribute()的. 
    你在org.apache.struts.action.Action 的方法execute 设定 
    request.setAttribute, 在你所要提交的jsp页面上就可以用
    request.getAttribute的.
      

  2.   

    ActionFrom:
    public class UserForm extends ActionForm {
    /*
     * Generated fields
     */ /** password property */
    private String password; /** username property */
    private String username;****get   set 就不贴了
    }-----------------------------------------------------------------------------------------------
    Action:
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
    String username = userForm.getUsername();
    String password = userForm.getPassword();
    String abc = (String) request.getAttribute("abc");
    String bcd = (String) request.getAttribute("bcd");
    ProBean proBean = (ProBean) request.getAttribute("proBean");
    System.out.println(username);
    System.out.println(password);
    System.out.println(abc);                   //null
    System.out.println(bcd);                   //null
    //System.out.println(proBean.getName());  //这两句空指针了。取不到值
    //System.out.println(proBean.getId());
    return null;
    }----------------------------------------------------------------------------------------------------------------
    struts-config.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
      <data-sources />
      <form-beans >
        <form-bean name="userForm" type="com.liang.struts.form.UserForm" />  </form-beans>  <global-exceptions />
      <global-forwards />
      <action-mappings >
        <action
          attribute="userForm"
          name="userForm"
          path="/user"
          scope="request"
          type="com.liang.struts.action.UserAction" />  </action-mappings>  <message-resources parameter="com.liang.struts.ApplicationResources" />
    </struts-config>-------------------------------------------------------------------------------------jsp:<body>
        <html:form action="user.do" method="post" focus="login">
          <table border="0">
            <tr>
              <td>Login:</td>
              <td><html:text property="username" /></td>
            </tr>
            <tr>
              <td>Password:</td>
              <td><html:password property="password" /></td>
            </tr>
            <tr>
              <td colspan="2" align="center"><html:submit /></td>
            </tr>
            <%
           request.setAttribute("abc","abc");
           request.setAttribute("bcd","bcd");
          
          
           ProBean pro = new ProBean();
           pro.setName("proname");
           pro.setId("proid");
           request.setAttribute("proBean",pro);
           %>
          </table>
         
        </html:form>
      </body>-------------------------------------------------------------------------setAttribute  的东西在action里都收不到哦。结果:
    asdf
    asdf
    null
    null
      

  3.   

    是不是由于没有写这句?
    request.getRequestDispatcher("user.do").forward(request,response);
    转发
    但是这样页面都打不开了。直接转发,
    然后控制台也没有打印东西我还试了一个servlet也不行,是不是jsp页面不能setAttribute 给 .class的啊?
      

  4.   

    试了一下ActionForm也接收不了自己定义的类型
    2009-6-5 11:33:51 org.apache.commons.beanutils.PropertyUtilsBean invokeMethod
    严重: Method invocation failed.
    java.lang.IllegalArgumentException: argument type mismatch要setAttribute的话可以用session实现。具体不大清楚什么原因。网上查了说是页面到类的request属于不可见,已不属于同一个请求范围那位朋友能帮忙解释一下啊?
      

  5.   

    实在不行就用JS控制转发试试吧request.getRequestDispatcher("user.do").forward(request,response); 提交按钮的时候再执行
      

  6.   


    这位朋友的方法可行,在struts1.2中这样可以让页面收到。但是这个用脚本怎么写不知道怎么写。直接写里面,直接就转发了。
    还有如果用request.getRequestDispatcher("servlet").forward(request,response);  提交给一个servlet(不用struts),在servlet里一样取不到
    getAttribute();如果request.getRequestDispatcher("somejsp.jsp").forward(request,response); 给一个jsp页面又可以 
      

  7.   

    jsp->action 是一个请求。应该可以取到request.getAttribute("")的值
    之所以取不到有可能是重定向。分为两个请求了。 
    找不到原因的话改用页面隐藏域提交 action里request.getParameter("")吧。 
      

  8.   

    刚刚写错了。
    用request.getRequestDispatcher("servlet/或jsp").forward(request,response);   在 servlet 或 struts 的 action 或 jsp 都可以用getAttribute()  得到值。不好意思
      

  9.   


    恩。之前收不到的原因就是因为没有转发过去。但是转发语句写在<%  %>里,页面就直接跳转了。得写js来控制
      

  10.   

    请问rrong_m要js实现request.getRequestDispatcher("user.do").forward(request,response);如何写啊?window.location.href="user.do";我这样写只是单传的url访问user.do  不会带数据过去
      

  11.   

    用session吧 取到以后 马上remove
      

  12.   

    肯定啊这样等于FORM没有提交。。你不是为了传值吗?为什么不考虑用隐藏表单呢?
      

  13.   

    恩是为了传值,有的时候想传一个bean。。隐藏域只能传string类型的。。
    window.location.href="user.do";写我试过会跳转,可是不会把attribute带过去