How to forward to Spring MVC interceptor
I have defined the permissive view as follows:
<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>
and I had an interceptor when some conditions did not pass, I want to forward the jsp page, I am implementing like this:
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/views/jsp/info.jsp");
requestDispatcher.forward(request, response);
Above, the page I want to forward is hardcoded, I didn't mean to do this, is there any approach I can get for the page from the view?
source to share
If you want to forward the view from postHandle
, it would be easier, because in postHandle
you you have full access to the ModelAndView.
This is also possible in a preHandle
method, thanks ModelAndViewDefiningException
to which allows you to ask spring to do the redirection itself from anywhere in the handler processing.
You can use it this way:
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;
}
}
source to share
Please have a look at RedirectAttributes
You can do something like
public String handle(Account account, BindingResult result, RedirectAttributes redirectAttrs) {
return "redirect:/context/info.jsp";
}
source to share
If we believe you are using SpringMVC and using controllers and you want to redirect to info.jsp, the code should look like this:
@Controller
public class InfoController {
@RequestMapping(value = "/info", method = RequestMethod.GET)
public String info(Model model) {
// TODO your code here
return "info";
}
}
source to share