在JSF bean中获取请求URL? [英] Get the request URL in a JSF bean?

查看:139
本文介绍了在JSF bean中获取请求URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在支持JSF页面的bean中获取请求URL?我一直在浏览FacesContext文档,我找到的最好方法似乎非常长:

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 "";
    }
}

编辑:功能要求这里的要求是我们需要第三方javascript实用程序的完整URL。该实用程序的使用或体系结构不适合JSF,但是这个调用的所有内容都不合适。我发现的方法会起作用,但是如果深入挖掘FacesContext则感觉不对。另外我希望有一种可以用JSF表达式语言调用的方法,因为它将以视图相关的方式使用。

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 used in a "view" related way.

推荐答案

如果您需要通过 FacesContext <从JSF引擎盖下运输原始Servlet API ,那么你很可能在错误的地方做这项工作。这是什么,您认为这是正确的解决方案的功能要求?您是否应该使用真正的 Servlet 类或过滤器而不是JSF托管bean?或者您可能根本不需要请求URL,因为有更好的jsfish方法可以实现相同的目标吗?

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.

更新根据你的编辑:所以,你只是在视图方面需要它吗?例如。 #{bean.requestURL} ?您也可以从 HttpServletRequest 对象中获取它,该对象已在EL中隐式提供。

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.

当您使用JSP时:

${pageContext.request.requestURL}

当你在Facelets上时:

Or when you're on Facelets:

#{request.requestURL}

而现在,你如何需要JavaScript呢?打印为JavaScript变量?喜欢这样吗?

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

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

如果是这样,你也可以抓住 window.location

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

var url = window.location;

不需要使用特定于视图的详细信息来混淆JSF bean。

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

这篇关于在JSF bean中获取请求URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆