JSF中的Java Web服务URL

时间:2011-02-14 13:51:22

标签: java web-services jsf

我创建了一个JSF应用程序,它也提供了一些Web服务。 Web服务是通过注释创建的。

现在我想创建一个webserviceInfo.xhtml页面,在那里我可以获得所有需要的webservice信息。

当我访问地址 http://our.server.com/application/OurWebserviceName 时,我会获得访问网络服务所需的所有信息(此信息页面由Glassfish自动生成)

要包含此页面,我在webserviceInfo.xhtml中执行了以下操作:

 <iframe scrolling="automatic"  width="971" height="1000" src="#{myBean.generateUrlToWebservice()}/OurWebserviceName"/>

其中:

public String generateUrlToWebservice(){
    FacesContext fc = FacesContext.getCurrentInstance();
    String servername = fc.getExternalContext().getRequestServerName();
    String port = String.valueOf(fc.getExternalContext().getRequestServerPort());
    String appname = fc.getExternalContext().getRequestContextPath();
    return "http://"+servername+":"+port+appname;
}

对此有更优雅的解决方案吗?

BR,Rene

2 个答案:

答案 0 :(得分:1)

如果你像协议(http,https ..)和页面(在应用程序名称之后)动态获取所有参数,那就更好了

public String generateUrlToWebservice(){
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext exContext = fc.getExternalContext();
    String servername = exContext.getRequestServerName();
    String port = String.valueOf(exContext.getRequestServerPort());
    String appname = exContext.getRequestContextPath();
    String protocol = exContext.getRequestScheme();
    String pagePath = exContext.getInitParameter("pagePath"); //read it from web.xml
    return protocol +"://"+servername+":"+port+appname+pagePath;
}

答案 1 :(得分:1)

使用页面相对URL。

<iframe src="OurWebserviceName"></iframe>

或者在JSTL <base> taglib的帮助下使用functions标记。

<html xmlns:fn="http://java.sun.com/jsp/jstl/functions">
...
<base href="#{fn:replace(request.requestURL, request.requestURI, '')}#{request.contextPath}"></base>

这样,任何不以方案或/开头的网址始终都与此网址相关。

或者,如果真的需要在JSF中执行此操作,则以下方案可以减少对方案和端口的影响。

HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
return request.getRequestURL().toString().replace(request.getRequestURI, "") + request.getContextPath();