问题
Is there any way to get the URL of the page which is loaded?
I would like the URL of the page which is loaded, in my controller i will call a method getUrlOfPage() in init() method .
I need the URL source to use it as a input for exporting the context in it.
How to get the URL of the page?
回答1:
It's available by HttpServletRequest#getRequestURL() (with domain) or getRequestURI() (without domain). The HttpServletRequest itself is in turn available by ExternalContext#getRequest().
Thus, so:
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
String url = request.getRequestURL().toString();
String uri = request.getRequestURI();
Or, if your bean is request scoped, then this is also possible:
@ManagedProperty("#{request.requestURL}")
private StringBuffer url; // +setter
@ManagedProperty("#{request.requestURI}")
private String uri; // +setter
See also
- Get current page programmatically
来源:https://stackoverflow.com/questions/20403569/how-to-get-the-url-of-current-page-in-jsf