How to get the URL of current page in JSF?

妖精的绣舞 提交于 2020-03-13 07:15:19

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!