JSF:指向 /webapp 子目录中网页的超链接
我有一个 .xhtml 页面列表,保存在 /src/main/webapp/pages/ 文件夹中。 现在我想创建它们的超链接。目前唯一有效的是默认主页:/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>
对于其他人,如果我有一个链接,例如:
<a href="/pages/page1.xhtml">Page 1</a>
我收到以下错误:
/page1.xhtml 未在以下位置找到 作为资源的外部上下文
我的问题是:如何在相对于web应用程序根目录的href 中指定我想要的页面。
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>
I get the following error:
/page1.xhtml Not Found in
ExternalContext as a Resource
My question is: How do I specify the page I want in a href relative to the webapp root.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于相对链接(即不以 http:// 开头的链接),您需要了解两件主要事情:
/
开头的相对链接是相对于域根的。如果当前 URL 是 http://example.com/app 并且页面包含链接
,那么它将指向到 http://example.com/pages/page1.xhtml (失败)。
如果当前 URL 是 http://example.com/app 并且页面包含链接
,那么它将指向到 http://example.com/app/pages/page1.xhtml (有效)。
如果当前 URL 为 http://example.com/app/pages/default.xhtml 并且页面包含一个链接
,那么它将指向 http://example.com /app/pages/pages/page1.xhtml(失败)。
您的问题是欢迎页面是通过转发而不是重定向打开的。这样,浏览器地址栏中的请求 URL 就会保留在 http://example.com/app 上它实际上显示 http://example.com/app/pages/default.xhtml 的内容。为了让链接在所有情况下都能工作,您需要一个像
“Thus”这样的链接,包括上下文路径,即 webapp 根目录。如果您唯一的问题是您想要动态包含上下文路径,那么只需打印
HttpServletRequest#getContextPath()
另请参阅:
Two major things you need to know about relative links (i.e. the ones not starting with http://):
/
are relative to the domain root.If the current URL is http://example.com/app and the page contains a link
then it will point to http://example.com/pages/page1.xhtml (fails).
If the current URL is http://example.com/app and the page contains a link
then it will point to http://example.com/app/pages/page1.xhtml (works).
If the current URL is http://example.com/app/pages/default.xhtml and the page contains a link
then it will point to http://example.com/app/pages/pages/page1.xhtml (fails).
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
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()
See also: