如何将请求从servlet转发到struts2的动作?

时间:2013-04-20 11:42:02

标签: java servlets struts2 dispatcher

我想使用RequestDispacher像这样将 Servlet 的请求转发给操作

RequestDispatcher dispatcher=request.getRequestDispatcher("hello.action");
dispatcher.include(request, response);

它不起作用。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:9)

为了做到这一点,您可能还需要将过滤器设置为在FORWARD上运行(并且如您的代码所示,包括INCLUDE,尽管您声明您想要FORWARD):

<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher> 
  <dispatcher>FORWARD</dispatcher>
  <dispatcher>INCLUDE</dispatcher> <!-- If you want includes as well -->
</filter-mapping>

答案 1 :(得分:8)

使用servlet中的代码

getServletContext().getRequestDispatcher("/hello.action").forward(request, response);

您还配置了struts2过滤器以接受转发请求

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
</filter>
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher>
  <dispatcher>FORWARD</dispatcher>
</filter-mapping>