JSF:超链接到/ webapp子目录中的网页 [英] JSF: Hyperlink to webpage in /webapp subdirectory
问题描述
我有一个列在我的/ src / main / webapp / pages /文件夹中的.xhtml页面。
现在我想为它们创建超链接。目前,唯一可用的是默认主页:/src/main/webapp/pages/default.xhtml。
I have a list of .xhtml pages that I keep in my /src/main/webapp/pages/ folder. Now I want to create hyperlinks to them. Currently the only one that works is the default home page: /src/main/webapp/pages/default.xhtml.
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>/pages/default.xhtml</welcome-file>
</welcome-file-list>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
对于其他人,如果我有一个链接,例如:
For the others, if I have a link such as:
<a href="/pages/page1.xhtml">Page 1</a>
我收到以下错误:
/page1.xhtml在
中找不到外部资源作为资源
/page1.xhtml Not Found in ExternalContext as a Resource
我的问题是:我相对于web应用程序指定了一个href 中的页面。
My question is: How do I specify the page I want in a href relative to the webapp root.
推荐答案
你需要知道的关于相对链接的事情(即不是以http://开头的):
Two major things you need to know about relative links (i.e. the ones not starting with http://):
- 相对于域根。
- 没有前导斜杠的相对链接与请求URL相关(因为它在浏览器地址中bar)
如果当前的URL是 http://example.com/app ,该页面包含一个链接
If the current URL is http://example.com/app and the page contains a link
<a href="/pages/page1.xhtml">
那么它将指向 http://example.com/pages/page1.xhtml (failed)。
then it will point to http://example.com/pages/page1.xhtml (fails).
如果当前网址是 http://example.com/app 和页面包含一个链接
If the current URL is http://example.com/app and the page contains a link
<a href="pages/page1.xhtml">
然后它将指向 http://example.com/app/pages/page1.xhtml (作品)。
then it will point to http://example.com/app/pages/page1.xhtml (works).
如果当前网址为 http:// example。 com / app / pages / default.xhtml ,该页面包含一个链接
If the current URL is http://example.com/app/pages/default.xhtml and the page contains a link
<a href="pages/page1.xhtml">
然后它将指向 http://example.com/app/pages/pages/page1.xhtml (failed)。
then it will point to http://example.com/app/pages/pages/page1.xhtml (fails).
您的问题是欢迎页面是由前进而不是重定向打开的。这样,浏览器地址栏中的请求网址就会保留在 http://example.com/app 上它实际上显示了 http://example.com/app/pages/default.xhtml的内容。要在所有情况下使链接都可以工作,您需要像
Your problem is that the welcome page is opened by a forward rather than a redirect. This way the request URL as it is in browser address bar stays on http://example.com/app while it is actually displaying the content of http://example.com/app/pages/default.xhtml. To get the links to work in all circumstances you want a link like
<a href="/app/pages/page1.xhtml">
因此,包括上下文路径,哪个是webapp根。如果您唯一的问题是您想要动态地包含上下文路径,那么只需打印 HttpServletRequest#getContextPath()
Thus, including the context path, which is the webapp root. If your sole problem is that you want to include the context path dynamically, then just print HttpServletRequest#getContextPath()
<a href="#{request.contextPath}/pages/page1.xhtml">
另请参见:
- 如何使用没有包括上下文根名称?
- How to use relative paths without including the context root name?
See also:
这篇关于JSF:超链接到/ webapp子目录中的网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!