问题描述
我有 Login.xhtml
和 Home.xhtml
.我在 web.xml
中配置了 url 模式如下
I have Login.xhtml
and Home.xhtml
. I configured the url pattern in web.xml
as follows
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Login.xhtml</welcome-file>
</welcome-file-list>
当我运行整个项目时,登录页面 URL
是这样的 http://localhost:8080/fran/Login.xhtml
,这里是 fran
是我的项目名称..
When I run the whole project, the login page URL
is like this http://localhost:8080/fran/Login.xhtml
, here fran
is my project name..
但是,我希望它是 http://localhost:8080/fran/Login/
而不是 http://localhost:8080/fran/Login.xhtml
代码>.
However, I would like it to be http://localhost:8080/fran/Login/
instead of http://localhost:8080/fran/Login.xhtml
.
我怎样才能做到这一点?是否可以为每个页面自定义 以摆脱
.xhtml
扩展名?
How can I achieve this? Is it possible to customize the <url-pattern>
for every page to get rid of the .xhtml
extension?
推荐答案
如果您的唯一原因是摆脱 .xhtml
扩展名,那么根据您的 JSF 版本,有多种方法重新使用.
If your sole reason is to get rid of the .xhtml
extension, then there are various ways depending on the JSF version you're using.
JSF 2.3 提供了一个新的 API 来收集所有视图:ViewHandler#getViews()
.将此与 ServletRegistration#addMapping()
在 ServletContextListener
中,如下所示.
JSF 2.3 offers a new API to collect all views: the ViewHandler#getViews()
. Combine this with ServletRegistration#addMapping()
in a ServletContextListener
as below.
@FacesConfig
@WebListener
public class ApplicationConfig implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
addExtensionLessMappings(event.getServletContext(), FacesContext.getCurrentInstance());
}
private void addExtensionLessMappings(ServletContext servletContext, FacesContext facesContext) {
servletContext
.getServletRegistrations().values().stream()
.filter(servlet -> servlet.getClassName().equals(FacesServlet.class.getName()))
.findAny()
.ifPresent(facesServlet -> facesContext
.getApplication()
.getViewHandler()
.getViews(facesContext, "/", ViewVisitOption.RETURN_AS_MINIMAL_IMPLICIT_OUTCOME)
.forEach(view -> facesServlet.addMapping(view))
);
}
}
实际上,这是一个单行.来源:Arjan Tijms 的博客 和 JSF 权威指南.
Effectively, this is an oneliner. Source: Arjan Tijms' Blog and The Definitive Guide to JSF.
如果您使用 MyFaces 作为 JSF 2.3 实现,则可以仅通过以下 web.xml
上下文参数透明地激活它:
If you're using MyFaces as JSF 2.3 implementation, then this can be transparently activated by solely the following web.xml
context parameter:
<context-param>
<param-name>org.apache.myfaces.AUTOMATIC_EXTENSIONLESS_MAPPING</param-name>
<param-value>true</param-value>
</context-param>
Mojarra 还没有类似的产品.
Mojarra does not have an equivalent yet.
使用 OmniFaces FacesViews.它通过将视图文件放在 /WEB-INF/faces-views/
文件夹中,提供了一种零配置的方式来实现这一点.否则,如果您不打算修改您的项目结构并希望将您的视图文件保留在通常的位置并仍然受益于无扩展名的 URL,那么只需添加以下上下文参数:
Use OmniFaces FacesViews. It offers a zero-configuration way to achieve that by placing the view files in /WEB-INF/faces-views/
folder. Otherwise, if you intend to not modify your project structure and want to keep your view files at the usual place and still benefit of extensionless URLs, then it's a matter of adding the following context parameter:
<context-param>
<param-name>org.omnifaces.FACES_VIEWS_SCAN_PATHS</param-name>
<param-value>/*.xhtml</param-value>
</context-param>
如果您不想使用 OmniFaces,而是想自己开发,只需查看 源代码.它是在 Apache 2.0 许可下开源的.它只是不是单线.
In case you don't want to use OmniFaces, but rather want to homegrow your own, just look at source code of OmniFaces. It's open source under Apache 2.0 License. It's only not an oneliner.
这篇关于自定义 FacesServlet <url-pattern>摆脱 .xhtml 扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!