自定义FacesServlet< url-pattern>摆脱.xhtml扩展名 [英] Customize FacesServlet <url-pattern> to get rid of .xhtml extension

查看:114
本文介绍了自定义FacesServlet< url-pattern>摆脱.xhtml扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Login.xhtmlHome.xhtml.我在web.xml中按如下方式配置了网址格式

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.

我该如何实现?是否可以为每个页面自定义<url-pattern>来摆脱.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来收集所有视图: 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))
        );
    }
}

实际上,这是一个单行.来源:《 JSF权威指南》 .

Effectively, this is an oneliner. Source: The Definitive Guide to JSF.

使用 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,而是想自己种植,请查看

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&lt; url-pattern&gt;摆脱.xhtml扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆