Reputation: 1356
I defined view resolver like this:
<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 a interceptor, when some conditions not pass, I want to forward to a jsp page, I implement like this:
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/views/jsp/info.jsp");
requestDispatcher.forward(request, response);
Above, the page that I want to forward is hard code, I didn't want to do like that, is there any approach that I can get the page from the view resolver?
Upvotes: 3
Views: 11383
Reputation: 1165
Please have a look at RedirectAttributes
NOTE: Redirects are NOT Forwards; see (https://www.baeldung.com/servlet-redirect-forward)
You can do something like
public String handle(Account account, BindingResult result, RedirectAttributes redirectAttrs) {
return "redirect:/context/info.jsp";
}
Upvotes: 1
Reputation: 149095
If you wanted to forward to a view from a postHandle
it would have been easier because in postHandle
you have full access to the ModelAndView.
It is also possible in a preHandle
method, thanks to the ModelAndViewDefiningException
, that allows you to ask spring to do itself the forward from anywhere in a handler processing.
You can use it that 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;
}
}
Upvotes: 9
Reputation: 1606
If we consider you're using SpringMVC and using controllers and you want to redirect to a info.jsp the code should looks like this :
@Controller
public class InfoController {
@RequestMapping(value = "/info", method = RequestMethod.GET)
public String info(Model model) {
// TODO your code here
return "info";
}
}
Upvotes: 1