如何获取最初请求的页面的URL?

时间:2012-10-12 23:40:26

标签: jsf jsf-2

我使用以下web.xml设置将未登录用户定向到/faces/loginPage.xhtml。
在/faces/loginPage.xhtml中,我将对用户进行身份验证并将用户重定向到主页。

现在我想将用户重定向到她最初请求的页面,而不是主页。我怎么做?具体来说,如何获取最初请求的页面的URL?

<security-constraint>
    <display-name>MyConstraint</display-name>
    <web-resource-collection>
        <web-resource-name>wrcoll</web-resource-name>
        <description />
        <url-pattern>/faces/secured/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <description />
        <role-name>myUser</role-name>
    </auth-constraint>
</security-constraint>

<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>my_ldap_domain</realm-name>
    <form-login-config>
        <form-login-page>/faces/loginPage.xhtml</form-login-page>
        <form-error-page>/error.xhtml</form-error-page>
    </form-login-config>
</login-config>

1 个答案:

答案 0 :(得分:2)

您似乎通过JSF托管bean而不是j_security_check执行登录。因为如果你使用后者,这已经被自动考虑在内了。

基于FORM的身份验证登录页面以RequestDispatcher#forward()常用的Servlet API方式显示。因此,最初请求的页面的请求URI可用作请求属性,其名称由RequestDispatcher.FORWARD_REQUEST_URI指定,其值为"javax.servlet.forward.request_uri"

因此,在EL上下文中,它可以作为

使用
#{requestScope['javax.servlet.forward.request_uri']}

在JSF上下文中,它可以作为

使用
String originalURL = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("javax.servlet.forward.request_uri");

这需要在初始请求中收集,而不是在表单提交上收集。最简单的方法是在附加到页面的@ViewScoped托管bean的构造函数中抓取它。使用@RequestScoped bean的替代方法是在登录表单中使用该值包含纯HTML <input type="hidden">,并将其设置为@ManagedProperty

相关问题