问题
I'm using an interceptor to restrict access to certain users in the app. For instance:
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
{
Logger.logRequest(request);
return list.contains(user);
}
If the list contains the user, it completes the request. Otherwise, it does nothing.
How do I display a custom page if the user doesn't have access? Right now, if it's false, it just shows a blank page which is not great for user experience.
回答1:
It looks like you can do a response redirect without hitting the servlet. The following works:
if (list.contains(user))
return true;
else
{
//set up the view
response.sendRedirect("nope_view");
return false;
}
来源:https://stackoverflow.com/questions/15691094/spring-mvc-3-interceptor-return-view-on-false