论坛首页 Java企业应用论坛

Struts中怎样动态的跳传到一个页面

浏览 13192 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-07-31  
在webwork中,可以在 webwork.xml中定义动态的Action,类似:
<action ...... 
<forward path="path{1}"/>
</action>

这个path可以在 action中动态的指定,但是在struts中怎样在Action中动态的指定path呢?
   发表时间:2005-07-31  
没用过WebWork2,不是很明白你的意思.

你是想不在struts-config.xml中配置这个路径,之间在Action的代码中添加路径,你这个路径是不是动态的生成的,所以不能配置?
0 请登录后投票
   发表时间:2005-07-31  
我觉得不能在action里面配置. 我也想这么配置来,结果不行

struts在这方面很差,tag也很差. (对比而言)
0 请登录后投票
   发表时间:2005-07-31  
Morgan0916 写道
没用过WebWork2,不是很明白你的意思.

你是想不在struts-config.xml中配置这个路径,之间在Action的代码中添加路径,你这个路径是不是动态的生成的,所以不能配置?


我的意思是说 :比如有个 Action 和它的配置:
<action path="/edit"  name="lyo.hotmail.test.TestAction">
    <forward name="success" path="/myapp/{url}"/>
</action>

我在 这个 TestAction中可以这样 :

public String url="";
.... execute(.....);{
 url="nextStep.do?id=5&app=now";
}

这样 url就在 Action中被动态的赋值了,我可以动态的决定跳转到哪个页面以及追加参数
0 请登录后投票
   发表时间:2005-08-01  
比较笨的解决办法:
/**
     *  在需要为ActionForward设置参数时得到一个用于更改的ActionForward。
     *  @param mapping
     *  @param forward forward的name。
     *  @return
     *  @author chinakite
     *  @version 1.0
     *  &lt;pre&gt;
     *  Created on :2005-8-1 9:25:51
     *  LastModified:
     *  History:
     *  &lt;/pre&gt;
     */
    public ActionForward turnForward(ActionMapping mapping, String forward) {
       ActionForward af = mapping.findForward(forward);
       ActionForward naf = new ActionForward();
       naf.setContextRelative(af.getContextRelative());
       naf.setModule(af.getModule());
       naf.setName(af.getName());
       naf.setPath(af.getPath());
       naf.setRedirect(af.getRedirect());
       return naf;
    }

然后可以写相应的方法设参数,例如:
/**
     *  此方法是用来给ActionForward设置action配置中path的参数用的。
     *  比如我们在写Action的过程中想forward到a.do并且想构造这样的url:
     *  /a.do?key1=value1&key2=value2
     *  那么我们可以在Action中这样写:
     * public ActionForward execute(ActionMapping mapping,
     *      ActionForm form,
     * HttpServletRequest request,
     * HttpServletResponse response)
     * throws Exception {
     * String forwardKey = "success";
     * //do something;
     *
     * ActionForward af = turnForward(mapping, forwardKey);
     * af = setForwardParam(af, "key1", "value1");
     * af = setForwardParam(af, "key2", "value2");
     * return af;
     * }
     *
     *  @param af 想要加参数的ActionForward;
     *  @param name 参数名;
     *  @param value 参数值;
     *  @author chinakite
     *  @version 1.0
     *  &lt;pre&gt;
     *  Created on :2005-8-1 9:36:16
     *  LastModified:
     *  History:
     *  &lt;/pre&gt;
     */
    protected void setForwardParam(ActionForward af,
                String name,
                String value) {
       String path = af.getPath();
       StringBuffer sb = new StringBuffer(path);
       if(name != null && !name.equals("")){
       if (path.indexOf("?") &gt; 0) {
           sb.append("&");
           sb.append(name);
           sb.append("=");
           sb.append(value);
       } else {
           sb.append("?");
           sb.append(name);
           sb.append("=");
           sb.append(value);
       }
       af.setPath(sb.toString());
       }    
    }

代码较丑,临时写的,见笑
0 请登录后投票
   发表时间:2005-08-05  
&lt;form-bean name="xxForm" type="xxx.xxx.XxForm"/&gt;

    &lt;action name="xxForm" path="/xxAction"  type="xxx.xxx.XxAction" parameter="method" scope="request"&gt;
      &lt;forward name="go" path="/aa/bb.jsp"&gt;
      &lt;/forward&gt;
    &lt;/action&gt;

    public class XxAction extends XXXAction {   

    public ActionForward query(ActionMapping actionMapping, ActionForm actionForm,
                              HttpServletRequest request,
                              HttpServletResponse response) {

   ActionForward af = actionMapping.findForward("go");
   return af;
   }

这样子可以吧。还有很多。
0 请登录后投票
   发表时间:2005-08-06  
是我没看明白还是怎么的?你这样不能动态跳啊,只能跳到/aa/bb.jsp,楼主要的是动态跳。
0 请登录后投票
   发表时间:2005-08-29  
据我所知,在struts里是不能这样做的,其实,感觉这倒没什么大的必要,你就在配置文件里多加几个跳转连接就行。因为你的页面跳转,只要你写在action里,肯定也变成静态的了,只是你不希望写那么多的配置罢了,如果又出现了新的连接情况,你还是要修改action的,即还要修改源代码,感觉不太好。action里功能最好不要写得太集中,尽量做到单个方法仅实现单个功能较好(一家之言)。
0 请登录后投票
   发表时间:2005-08-29  
感觉楼主的想法不是很好........
一个LINK就应该是一个action 不应该动态的决定action 这样的业务是很乱的
应该做成一个action处理你想用两个action处理的问题 然后在这个action里决定要跳转的页面 
至于参数 自己控制就好了 这个容易的很
0 请登录后投票
   发表时间:2005-08-30  
引用

我的意思是说 :比如有个 Action 和它的配置:
java代码: 


&lt;action path="/edit"  name="lyo.hotmail.test.TestAction"&gt;
    &lt;forward name="success" path="/myapp/{url}"/&gt;
&lt;/action&gt;



我在 这个 TestAction中可以这样 :

java代码: 


public String url="";
.... execute(.....){
url="nextStep.do?id=5&app=now";
}



这样 url就在 Action中被动态的赋值了,我可以动态的决定跳转到哪个页面以及追加参数


完全可以的
url="nextStep.do?id=5&app=now";
return new ActionForward(url);
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics