Spring MVC 3: Interceptor return view on false

有些话、适合烂在心里 提交于 2019-12-08 01:56:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!