如何在没有请求的servlet的情况下在jsf托管bean中获取请求URL? [英] How do I get request url in jsf managed bean without the requested servlet?

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

问题描述

假设网址为 http:// localhost:8080 / project-name / resource.xhtml

我想获得以下 http: // localhost:8080 / project-name
在JSF托管bean中。

I want to obtain the following http://localhost:8080/project-name in a JSF managed bean.

推荐答案

我将假设你使用JSF 2和Java EE 6来解决这个问题。

实际机制的实现将根据程度而有所不同您将需要原始URL。

The implementation of the actual mechanism will vary depending on the extent to which you'll need the original URL.

您首先需要访问底层servlet容器(假定为一个,而不是一个portlet容器) HttpServletRequest对象。使用 FacesContext 以下列方式访问HttpServletRequest对象:

You'll first need to get access to the underlying servlet container (assumed to one, instead of a portlet container) produced HttpServletRequest object. Use the FacesContext object to access the HttpServletRequest object in the following manner:

HttpServletRequest origRequest = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();

HttpServletRequest 类提供了几种实用程序方法来获取原始请求的近似表示:

The HttpServletRequest class provides several utility methods to obtain a near representation of the original request:


  • getRequestURL() ,它提供的原始请求没有查询字符串

  • getScheme getServerName getServerPort getContextPath getServletPath getPathInfo getQueryString 所有输出都可以按顺序组合以获得原始请求。如果你想要一个较小的URL片段,你可能不得不省略后面的调用。

  • getRequestURL(), which provides the original request sans the query string
  • getScheme, getServerName, getServerPort, getContextPath, getServletPath, getPathInfo and getQueryString all of whose outputs can be combined in sequence to obtain the original request. You may have to omit the latter invocations if you want a lesser fragment of the URL.

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

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