如何将请求从 servlet 转发到 Struts 2 中的操作?

问题描述 投票:0回答:2

我想像这样使用

RequestDispacher
将请求从 servlet 转发到操作

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

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

java servlets struts2 dispatcher forward
2个回答
11
投票

为了做到这一点,您可能还需要将过滤器设置为在 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>

10
投票

使用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>
© www.soinside.com 2019 - 2024. All rights reserved.