【问题标题】:Get beautified URL from HttpServletRequest从 HttpServletRequest 获取美化 URL
【发布时间】:2020-02-11 04:46:55
【问题描述】:

当没有人登录时,我正在使用org.omnifaces.filter.HttpFilter 在登录页面上重定向访问者。

@Override
public void doFilter(HttpServletRequest req, HttpServletResponse res, HttpSession session, FilterChain chain) throws ServletException, IOException {
    String loginUrl = "/myapp/login?redirect_url=" + req.getRequestURL();
    boolean loggedIn = (req.getRemoteUser() != null);
    if (loggedIn) {
        chain.doFilter(req, res); // So, just continue request.
    } else {
        Servlets.facesRedirect(req, res, loginUrl);
    }
}  

我想将未登录用户重定向到/login?redirect_url=previous_page_url

问题是我所有的 URL 都被 pretty-faces 美化了,当我尝试使用 HttpServletRequest.getRequestURI() 获取以前的 URL 时,它给了我丑陋的 URL。

例如,我配置了一个 url /myapp/my-page-3 显示/views/module1/page3.xhtml

但是HttpServletRequest.getRequestURI() 给了我/views/module1/page3.xhtml不是 /myapp/my-page-3

有什么想法吗?

【问题讨论】:

  • 因为您已经在使用 OmniFaces .. Servlets.getRequestURI(req) 是否返回预期的 URI?

标签: servlets servlet-filters omnifaces prettyfaces


【解决方案1】:

当基于 servlet 的 URL 重写引擎在后台使用 RequestDispatcher#forward() 将传入的友好 URL 请求转发到所需资源时,您可以使用 request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI) 找出原始请求 URI。

String originalRequestURI = request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);

由于您已经在使用 OmniFaces,您可以使用 Servlets#getRequestURI() 自动检测它并在存在时返回它,否则回退到默认的 HttpServletRequest#getRequestURI()

String requestURI = Servlets.getRequestURI(request);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-21
    • 2020-08-30
    • 2018-02-07
    • 2011-02-08
    • 2012-11-10
    • 2014-07-04
    • 2010-11-18
    • 1970-01-01
    相关资源
    最近更新 更多