【问题标题】:Can we write struts dispatchAction method with void return type?我们可以用 void 返回类型编写 struts dispatchAction 方法吗?
【发布时间】:2012-11-15 07:54:06
【问题描述】:

我想使用 Struts DispacthAction 方法执行一些操作,但是我不想转发任何 jsp;

我可以用 void 编写调度方法吗? 例如:

  public void perform(ActionMapping actionMapping, ActionForm actionForm,    HttpServletRequest request,HttpServletResponse response) 
    throws Exception {
    System.out.println();
 }

【问题讨论】:

    标签: java struts struts-1


    【解决方案1】:

    您的方法必须返回 ActionForward,而不是 void这是 Struts 中执行方法的约定,您必须遵守约定,否则您的代码将无法工作。

    可能返回null 而不是ActionForward 对象,但这很棘手,因为请求调度程序只会返回。响应的当前内容将被发送到客户端,如果您不自己管理响应,您可能会得到一个空白页面。

    【讨论】:

      【解决方案2】:

      如文档中所述,您的方法必须具有返回类型 "ActionForward" 而不是 void。
      我不明白您为什么不想将您的回复转发到任何其他页面。
      如果你想将你的响应转发到另一个动作,你可以通过简单地在 struts-config.xml 的转发标签中提供动作路径来做到这一点,比如

      <forward name="success" path="/doAction.do">
      

      【讨论】:

        猜你喜欢
        • 2012-03-19
        • 1970-01-01
        • 1970-01-01
        • 2015-06-13
        • 1970-01-01
        • 2014-08-24
        • 1970-01-01
        • 2012-05-07
        • 1970-01-01
        相关资源
        最近更新 更多