Java 在 JSF bean 中获取请求 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3302933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the request URL in a JSF bean?
提问by Adam
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:
如何在支持 JSF 页面的 bean 中获取请求 URL?我一直在浏览 FacesContext 文档,我能找到的最好方法似乎非常长:
public String getRequestURL()
{
Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
if(request instanceof HttpServletRequest)
{
return ((HttpServletRequest) request).getRequestURL().toString();
}else
{
return "";
}
}
Edit: Functional RequirementsThe 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.
编辑:功能要求这里的要求是我们需要第三方 javascript 实用程序的完整 URL。该实用程序的使用或架构与 JSF 不太匹配,但除此调用外的一切都适合。我发现的方法是可行的,但是对 FacesContext 进行如此深入的挖掘感觉是错误的。此外,我希望有一种可以用 JSF 表达式语言调用的方法,因为它将以“视图”相关的方式使用。
采纳答案by BalusC
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?
如果您需要从 JSF 引擎盖下提取原始 Servlet API FacesContext
,那么您很可能在错误的地方完成了这项工作。您认为这是正确解决方案的功能要求是什么?您是否应该不使用真正的Servlet
类或Filter
使用 JSF 托管 bean 来代替?或者您可能根本不需要请求 URL,因为有更好的“jsfish”方法来实现相同的目标?
If you update your question to include the detail about the functional requirement, then we may be able to suggest the right solution.
如果您更新您的问题以包含有关功能要求的详细信息,那么我们可能会建议正确的解决方案。
Updateas 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.
根据您的编辑进行更新:那么,您毕竟只需要在视图方面使用它?例如#{bean.requestURL}
?您也可以从HttpServletRequest
EL 中已经隐式可用的对象中获取它。
When you're on JSP:
当您使用 JSP 时:
${pageContext.request.requestURL}
Or when you're on Facelets:
或者当您使用 Facelets 时:
#{request.requestURL}
And now, how do you need it for JavaScript? Printing as a JavaScript variable? Like so?
现在,您如何为 JavaScript 需要它?作为 JavaScript 变量打印?像这样?
var url = '#{request.requestURL}';
If so, you could also just grab window.location
for that.
如果是这样,你也可以抓住window.location
它。
var url = window.location;
No need to clutter the JSF bean with the view specific details.
无需将 JSF bean 与视图特定的细节混在一起。
回答by Jose Diaz
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.
你已经得到了答案:) 恐怕你必须那样使用它,因为每个 HTTP 请求都必须从 faces 上下文中检索。当然,您可以简化此方法的某些部分,例如面部上下文检索,但它绝对是要走的路,除非您使用某种框架来抽象这些细节并让您直接访问 HttpRequest 对象。
Regards.
问候。