来自HttpServletResponse的java Spring MVC相对重定向
鉴于这种相对重定向到另一个控制器的方法:
@Controller
@RequestMapping("/someController")
public class MyController {
@RequestMapping("/redirme")
public String processForm(ModelMap model) {
return "redirect:/someController/somePage";
}
}
如果我在一个拦截器中,我如何模拟相同的相对重定向
public class MyInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
{
response.sendRedirect("/someController/somePage");
return false;
}
}
现在有了拦截器,我将在应用程序中结束。com/someController/somePage时,我真的想在应用程序中。com/deployment/someController/somePage。肯定有“春天”的解决方案吗
# 1 楼答案
在参考了redirect documentation、查看了UrlBasedViewResolver的源代码以及gotuskar的评论之后,我觉得自己很傻,因为我不知道这会起作用,也不知道有一个解决方案:
# 2 楼答案
将我的评论转换为回答-
尝试使用
response.sendRedirect(request.getContextPath() + uri);