我想向我的项目添加文档。通过单击F1,我在某个位置打开了文档(对于文档,我有1个文件(index.htm))。但是,我无法使用锚点打开URL。我已经形成了正确的URL,但是.browse()在没有锚的情况下打开了文档(开头)。
public void openHtmlDocument() throws IOException, URISyntaxException {
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
File file = new File(servletContext.getRealPath("/documentation/index.htm"));
URL url = new URL(file.toURI().toURL(), "#_Toc502051959");
Desktop.getDesktop().browse(url.toURI());
}
我该如何解决?其他的其他答案对我而言并不实际,因为用户使用Windows或Linux。 形成的URI:
文件:/ D:/app/wildfly-13.0.0.Final/standalone/tmp/vfs/deployment/deployment545477ea955f6f3d/mainUI-1.2.14.0.war-7f1f239336b4e258/documentation/index.htm#_Toc502051959 < / em>
打开后我的浏览器URL:
答案 0 :(得分:0)
这真的很痛苦。如此处所述,Desktop.browse无法使用锚点: How to launch a file protocol URL with an anchor from Java? 该链接为Windows提供了可能的解决方法。
在Linux中,您可以通过执行以下命令来打开网址:
Runtime.exec("open file:/D:/app/wildfly-13.0.0.Final/standalone/tmp/vfs/deployment/deployment545477ea955f6f3d/mainUI-1.2.14.0.war-7f1f239336b4e258/documentation/index.htm#_Toc502051959");
答案 1 :(得分:0)
解决我的问题的方法,如果突然有人遇到相同的问题:
String menuNameNotBlanked = menuName == null ? "" : menuName.replace(" ","_");
String formPathNotBlanked = formPath == null ? "" : formPath.replace(".xhtml","").replace("/","_");
String helpPath = ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getRealPath(HELP_FILE_PATH);
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
URL contextURL = new URL(request.getScheme(),request.getServerName(),request.getServerPort(),request.getContextPath());
URL helpURL = new URL(contextURL.toString()+ "/" + HELP_FILE_PATH + AddLeadString(menuNameNotBlanked+formPathNotBlanked,"#"));
RequestContext.getCurrentInstance().execute("window.open('" + helpURL + "')");