我正在尝试使用 Spring 的“forward:”前缀将从一个 Controller 收到的请求转发到第二个 Controller 。但是,该请求并没有像我期望的那样将请求转发到第二个 Controller ,而是由第一个 Controller 一遍又一遍地处理(直到我收到 StackOverflowError)。这让我认为它可能将“/app/pong”视为某种相对路径,但我不确定为什么会出现这种情况。我是否误解了“forward:”应该如何工作?我在这里缺少什么吗?
Controller :
@Controller
public class ControllerOne {
@RequestMapping(value = "/ping", method = RequestMethod.GET)
public String doPing(HttpServletRequest request) {
log.debug("Ping?");
return "forward:/app/pong";
}
}
@Controller
public class ControllerTwo {
@RequestMapping(value = "/pong", method = RequestMethod.GET)
public String doPong(HttpServletRequest request) {
log.debug("Pong!");
return "pong";
}
}
servlet 映射:
<servlet-mapping>
<servlet-name>test-servlet</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
查看解析器:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
最佳答案
我认为问题出在您配置的 InternalResourceViewResolver
上。因为这个viewResolver的内部逻辑,它会自动进行转发,你可以配置另一种viewResolver: UrlBasedViewResolver 。
以下是我在官方文档中找到的信息:
It is also possible to use a special forward: prefix for view names that are ultimately resolved by UrlBasedViewResolver and subclasses. This creates an InternalResourceView (which ultimately does a RequestDispatcher.forward()) around the rest of the view name, which is considered a URL. Therefore, this prefix is not useful with InternalResourceViewResolver and InternalResourceView (for JSPs for example). But the prefix can be helpful when you are primarily using another view technology, but still want to force a forward of a resource to be handled by the Servlet/JSP engine. (Note that you may also chain multiple view resolvers, instead.)
关于java - 使用 Spring "forward:"前缀导致 StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15627444/