如何在JSF中获取当前页面的URL? [英] How to get the URL of current page in JSF?

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

问题描述

有什么方法可以获取所加载页面的URL?

Is there any way to get the URL of the page which is loaded?

我想要加载的页面的URL,在我的控制器中,我将在init()方法中调用方法getUrlOfPage().

I would like the URL of the page which is loaded, in my controller i will call a method getUrlOfPage() in init() method .

我需要URL源来将其用作在其中导出上下文的输入.

I need the URL source to use it as a input for exporting the context in it.

如何获取页面的URL?

How to get the URL of the page?

推荐答案

HttpServletRequest 本身可通过 ExternalContext#getRequest()通过JSF API获得.

It's available by HttpServletRequest#getRequestURL() (with domain) or getRequestURI() (without domain). The HttpServletRequest itself is in turn available through JSF API via ExternalContext#getRequest().

因此,

public void someMethod() {
    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    String url = request.getRequestURL().toString();
    String uri = request.getRequestURI();
    // ...
}

或者,如果您正在使用CDI @Named来管理bean,并且您使用的是JSF 2.3或更高版本,那么也可以通过

Or, if you're using CDI @Named to manage beans, and you're on JSF 2.3 or newer, then this is also possible through javax.faces.annotation.ManagedProperty:

@Inject @ManagedProperty("#{request.requestURL}")
private StringBuffer url; // +setter

@Inject @ManagedProperty("#{request.requestURI}")
private String uri; // +setter

public void someMethod() {
    // ...
}

或者,如果您正在使用CDI @Named来管理Bean,那么在较旧的JSF版本上也可以这样做:

Or, if you're using CDI @Named to manage beans, then this is also possible, also on older JSF versions:

@Inject
private HttpServletRequest request;

public void someMethod() {
    String url = request.getRequestURL().toString();
    String uri = request.getRequestURI();
    // ...
}

或者,如果您仍在使用自JSF 2.3起已弃用的@ManagedBean,那么也可以通过

Or, if you're still using the since JSF 2.3 deprecated @ManagedBean, then this is also possible through javax.faces.bean.ManagedProperty (note that the bean can only be @RequestScoped!):

@ManagedProperty("#{request.requestURL}")
private StringBuffer url; // +setter

@ManagedProperty("#{request.requestURI}")
private String uri; // +setter

public void someMethod() {
    // ...
}

另请参见

  • 以编程方式获取当前页面
  • See also

    • Get current page programmatically
    • 这篇关于如何在JSF中获取当前页面的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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