在 JSF 中,我可以在 URL 中隐藏 XHTML 文件的部分目录层次结构吗?
在 JSF 应用程序中,我们有目录层次结构:
webapp
xhtml
login.xhtml
main.xhtml
search.xhtml
css
main.css
extra.css
js
jquery.js
等等。servlet 映射是:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
这工作正常,但我们的 Web 应用程序的 URL 如下所示:
http://localhost/myapp/xhtml/login.xhtml
http://localhost/myapp/xhtml/search.xhtml
我们希望通过删除 /xhtml部分,即 http://localhost/myapp/login.xhtml
我找不到任何方法来完成此操作。有没有办法在
中做到这一点?我需要一些额外的框架吗?
In a JSF application, we have the directory hierarchy:
webapp
xhtml
login.xhtml
main.xhtml
search.xhtml
css
main.css
extra.css
js
jquery.js
etc. The servlet mapping is:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
This works fine, but the URLs of our web app look like this:
http://localhost/myapp/xhtml/login.xhtml
http://localhost/myapp/xhtml/search.xhtml
We would like to have simpler URLs by dropping the /xhtml
part, i.e.
http://localhost/myapp/login.xhtml
I could not find any way to accomplish this. Is there some way to do this in the <servlet-mapping>
? Do I need some additional framework?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
过滤器
来做到这一点。自行开发的过滤器或第三方过滤器,例如 URLRewriteFilter。只需将其映射到*.xhtml
上,然后转发到/xhtml/*
即可。类似:
但更简单的方法是更改目录层次结构以删除
/xhtml
子文件夹。这些 CSS/JS(和图像)文件最好放置在/resources
子文件夹中,以便您可以利用
、的功能以正确的方式
和
。另请参阅:
You could do it with a
Filter
. Either homegrown or a 3rd party one like URLRewriteFilter. Just map it on*.xhtml
and then forward to/xhtml/*
.Something like:
But simpler would be to just change the directory hierarchy to get rid of
/xhtml
subfolder. Those CSS/JS (and image) files should preferably be placed in a/resources
subfolder so that you can utilize the powers of<h:outputStylesheet>
,<h:outputScript>
and<h:graphicImage>
in a proper way.See also: