我定义了这样的视图解析器:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
我有一个拦截器,当某些条件没有通过时,我想转发到一个jsp页面,我这样实现:
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/views/jsp/info.jsp");
requestDispatcher.forward(request, response);
上面,我要转发的页面是硬代码,我不想这样做,有什么方法可以从视图解析器中获取页面吗?
答案 0 :(得分:8)
如果您希望从postHandle
转发到视图,那会更容易,因为在postHandle
中您可以完全访问ModelAndView。
由于preHandle
,ModelAndViewDefiningException
方法也可以让你在处理程序处理的任何地方让spring自己做前进。
您可以这样使用:
public class ForwarderInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// process data to see whether you want to forward
...
// forward to a view
ModelAndView mav = new ModelAndView("forwarded-view");
// eventually populate the model
...
throw new ModelAndViewDefiningException(mav);
...
// normal processing
return true;
}
}
答案 1 :(得分:1)
您可以执行类似
的操作 public String handle(Account account, BindingResult result, RedirectAttributes redirectAttrs) {
return "redirect:/context/info.jsp";
}
答案 2 :(得分:1)
如果我们认为您正在使用SpringMVC并使用控制器,并且您想要重定向到info.jsp,则代码应如下所示:
@Controller
public class InfoController {
@RequestMapping(value = "/info", method = RequestMethod.GET)
public String info(Model model) {
// TODO your code here
return "info";
}
}