12

我有Login.xhtmlHome.xhtmlweb.xml我按如下方式配置了 url 模式

<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扩展程序?

4

3 回答 3

15

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

JSF 2.3+

JSF 2.3 提供了一个新的 API 来收集所有视图:ViewHandler#getViews(). ServletRegistration#addMapping()将其与a结合起来,ServletContextListener如下所示。

@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 权威指南

如果您使用 MyFaces 作为 JSF 2.3 实现,则可以仅通过以下web.xml上下文参数透明地激活它:

<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,那么只需添加以下上下文参数:

<context-param>
    <param-name>org.omnifaces.FACES_VIEWS_SCAN_PATHS</param-name>
    <param-value>/*.xhtml</param-value>
</context-param>

如果您不想使用 OmniFaces,而是想自己开发,只需查看 OmniFaces 的源代码。它在 Apache 2.0 许可下开源。它只是不是单线器。

于 2013-08-30T16:41:01.390 回答
3

看看prettyfaces:Pretty URLs for JavaServer Faces

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

并查看第 2 章。开始使用

于 2013-08-30T08:00:14.483 回答
0

只是对先生的一点补充。@balusc 关于 JSF 2.3 上 MyFaces 的出色回答...(编辑:并不是真正的补充,因为它不能补充完整的内容,而只是 tomcat/tomee 用户处理此 tomcat 错误的一种解决方法)。

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

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

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

at org.apache.myfaces.webapp.StartupServletContextListener.contextInitialized(StartupServletContextListener.java:103)

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

<listener>
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
于 2020-12-27T04:13:32.570 回答