首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义FacesServlet <url-pattern>以摆脱.xhtml扩展

自定义FacesServlet <url-pattern>以摆脱.xhtml扩展
EN

Stack Overflow用户
提问于 2013-08-29 18:21:50
回答 3查看 13.8K关注 0票数 12

我有Login.xhtmlHome.xhtml。我在web.xml中配置了url模式,如下所示

代码语言:javascript
复制
<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是我的项目名称。

但是,我希望它是http://localhost:8080/fran/Login/而不是http://localhost:8080/fran/Login.xhtml

我如何才能做到这一点?是否可以为每个页面定制<url-pattern>以摆脱.xhtml扩展?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-08-31 00:41:01

如果您的唯一原因是要摆脱.xhtml扩展,那么有多种方法可供选择,具体取决于您所使用的JSF版本。

JSF 2.3+

JSF2.3提供了一个新的API来收集所有视图:ViewHandler#getViews()。在ServletContextListener中将其与ServletRegistration#addMapping()结合使用,如下所示。

代码语言:javascript
复制
@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' BlogThe Definitive Guide to JSF

如果您使用MyFaces作为JSF2.3实现,那么仅通过以下web.xml上下文参数就可以透明地激活它:

代码语言:javascript
复制
<context-param>
    <param-name>org.apache.myfaces.AUTOMATIC_EXTENSIONLESS_MAPPING</param-name>
    <param-value>true</param-value>
</context-param>

Mojarra还没有类似的东西。

JSF 2.2-

使用OmniFaces FacesViews。通过将视图文件放在/WEB-INF/faces-views/文件夹中,它提供了一种零配置方式来实现这一点。否则,如果您不打算修改您的项目结构,并且希望将视图文件保留在通常的位置,并且仍然受益于无扩展URL,那么只需添加以下上下文参数:

代码语言:javascript
复制
<context-param>
    <param-name>org.omnifaces.FACES_VIEWS_SCAN_PATHS</param-name>
    <param-value>/*.xhtml</param-value>
</context-param>

如果你不想使用OmniFaces,而是想自己开发,只要看看source code of OmniFaces就行了。它在Apache2.0许可下是开源的。只是它不是一个线条。

票数 15
EN

Stack Overflow用户

发布于 2013-08-30 16:00:14

看看prettyfaces: Pretty URLs for JavaServer Faces

查看主页中的2. Create pretty-config.xml示例

然后看一下Chapter 2. Get Started

票数 3
EN

Stack Overflow用户

发布于 2020-12-27 12:13:33

只是对先生的一点补充。@balusc关于JSF2.3上的MyFaces的出色回答...(编辑:不是真正的补充,因为不能补充完整的内容,而只是tomcat/tomee用户处理此tomcat bug的变通方法)。

使用MyFaces 2.3.6时,我收到一个关于servlet规范和ServletContextListener的异常:

代码语言:javascript
复制
java.lang.UnsupportedOperationException: Section 4.4 of the Servlet 3.0 specification does not permit this method to be called from a ServletContextListener that was not defined in web.xml, a web-fragment.xml file nor annotated with @WebListener

在堆栈后面,我看到了这一行:

代码语言:javascript
复制
at org.apache.myfaces.webapp.StartupServletContextListener.contextInitialized(StartupServletContextListener.java:103)

在将该侦听器添加到web.xml之后,一切工作正常:

代码语言:javascript
复制
<listener>
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18508329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档