kar*_*ark 11 jsf friendly-url jsf-2
我有Login.xhtml和Home.xhtml.我web.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扩展?
Bal*_*usC 14
如果您唯一的理由是摆脱.xhtml扩展,那么取决于您正在使用的JSF版本有多种方式.
JSF 2.3提供了一个新的API来收集所有视图:ViewHandler#getViews().与此相结合ServletRegistration#addMapping()的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))
        );
    }
}
实际上,这是一个oneliner.资料来源:JSF权威指南.
使用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许可下的开源软件.它不仅仅是一个oneliner.
| 归档时间: | 
 | 
| 查看次数: | 13098 次 | 
| 最近记录: |