开发者

Get the request URL in a JSF bean?

开发者 https://www.devze.com 2023-01-08 20:11 出处:网络
How do you get the request URL in a bean backing a JSF page? I\'ve been looking through the FacesContext docs and the best way I could find seems terribly long:

How do you get the request URL in a bean backing a JSF page? I've been looking through the FacesContext docs and the best way I could find seems terribly long:

public String getRequestURL()
{
    Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
    if(request instanceof HttpServletRequest)
    {
            return ((HttpServletRequest) request).getRequestURL().toString();
    }else
    {
        return "";
    }
}

Edit: Functional Requirements The requirement here is that we need the full URL for a third party javascript utility. The use or architecture of the utility doesn't fit well with JSF, but everything short of this call does. The method I found will work, but it felt wrong to be digging so deep into the FacesContext. Additionally I was hoping there would be a way that could be called with JSF Expression Language since this is going to be use开发者_如何学Cd in a "view" related way.


If you ever need to haul the raw Servlet API from under the JSF hoods by the FacesContext, then chances are that you're doing the job at the wrong place. What is it, the functional requirement for which you thought that this is the right solution? Should you maybe not be using a real Servlet class or a Filter instead of a JSF managed bean? Or maybe don't you need the request URL at all because there are better "jsfish" ways to achieve the same?

If you update your question to include the detail about the functional requirement, then we may be able to suggest the right solution.


Update as per your edit: so, you just need it after all in the view side? E.g. #{bean.requestURL}? You can also just grab it from the HttpServletRequest object which is already implicitly available in EL.

When you're on JSP:

${pageContext.request.requestURL}

Or when you're on Facelets:

#{request.requestURL}

And now, how do you need it for JavaScript? Printing as a JavaScript variable? Like so?

var url = '#{request.requestURL}';

If so, you could also just grab window.location for that.

var url = window.location;

No need to clutter the JSF bean with the view specific details.


You already have you answer :) I'm afraid you'll have to use it that way, since every HTTP request will have to be retrieve from the faces context. You could, of course, simplify some part of this method, like the faces context retrieval, but its definitively the way to go, unless you use some kind of framework that abstracts away those details and let you get to the HttpRequest Object directly.

Regards.


you can get url with all parameters so:

HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
                    .getRequest();
String url = req.getHeader("referer");
0

精彩评论

暂无评论...
验证码 换一张
取 消