我创建了一个 JSF 应用程序,它也提供了一些 Web 服务。 Web 服务是通过注释创建的。
现在我想创建一个 webserviceInfo.xhtml 页面,从中获取所有需要的网络服务信息。
当我转到地址 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,雷内
最佳答案
使用页面相关 URL。
<iframe src="OurWebserviceName"></iframe>
或使用 <base>
在 JSTL 的帮助下标记 functions
标签库。
<html xmlns:fn="http://java.sun.com/jsp/jstl/functions">
...
<base href="#{fn:replace(request.requestURL, request.requestURI, '')}#{request.contextPath}"></base>
这样任何不以 scheme 或 /
开头的 URL始终与此 URL 相关。
或者,如果您真的需要在 JSF 中执行此操作,则以下方案和端口可以减少麻烦。
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
return request.getRequestURL().toString().replace(request.getRequestURI, "") + request.getContextPath();
关于JSF 中的 Java Web 服务 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4992928/