JSF 2.0: How to get the URL that is entered in the browser's address bar
My JSF application redirects any user who is not logged in to the login page. When the user logs in, I want the application to redirect to the page the user has initially entered in the browser's adress bar. But开发者_如何学Python I don't know how to access the url the user has initially entered, since he is automatically redirected to the login page which I configured in the web.xml.
The container managed security doesn't have any API-provided facilities for this. Your best bet is to replace the <login-config>
by a Filter
class which does roughly like this:
HttpServletRequest httpreq = (HttpServletRequest) request;
HttpServletResponse httpres = (HttpServletResponse) response;
if (httpreq.getUserPrincipal() == null) {
httpreq.getSession().setAttribute("from", httpreq.getRequestURI());
httpres.sendRedirect("login.jsf");
} else {
chain.doFilter(request, response);
}
And then in your login thing:
request.login(username, password);
externalContext.redirect((String) request.getSession().getAttribute("from"));
精彩评论