使用JSF 2.3的无扩展URL

时间:2017-10-18 13:14:38

标签: jsf jsf-2.3

JSF 2.3无扩展URL,如here所述,不适用于我的企业应用。导航到没有.xhtml扩展名的网址时,会抛出FileNotFoundException异常:

Warning:   JSF1091: No mime type could be found for file /pages/najmovi.  To resolve this, add a mime-type mapping to the applications web.xml.
FATAL:   JSF1073: javax.faces.FacesException caught during processing of RESTORE_VIEW 1 : UIComponent-ClientId=, Message=java.io.FileNotFoundException
FATAL:   java.io.FileNotFoundException
javax.faces.FacesException: java.io.FileNotFoundException
    at com.sun.faces.application.view.ViewMetadataImpl.createMetadataView(ViewMetadataImpl.java:158)
    at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:247)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:133)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:201)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:670)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1580)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:338)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.glassfish.tyrus.servlet.TyrusServletFilter.doFilter(TyrusServletFilter.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:250)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at com.edu.iznajmljivanje.servlet.AuthenticationFilter.doFilter(AuthenticationFilter.java:30)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:250)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:652)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:591)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:463)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:168)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException
    at org.apache.naming.resources.DirContextURLConnection.getInputStream(DirContextURLConnection.java:403)
    at com.sun.faces.util.Util.getLastModified(Util.java:1278)
    at com.sun.faces.facelets.impl.DefaultFaceletCache$2.newInstance(DefaultFaceletCache.java:98)
    at com.sun.faces.facelets.impl.DefaultFaceletCache$2.newInstance(DefaultFaceletCache.java:93)
    at com.sun.faces.util.ExpiringConcurrentCache$1.call(ExpiringConcurrentCache.java:100)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at com.sun.faces.util.ExpiringConcurrentCache.get(ExpiringConcurrentCache.java:115)
    at com.sun.faces.facelets.impl.DefaultFaceletCache.getViewMetadataFacelet(DefaultFaceletCache.java:148)
    at com.sun.faces.facelets.impl.DefaultFaceletCache.getViewMetadataFacelet(DefaultFaceletCache.java:63)
    at com.sun.faces.facelets.impl.DefaultFaceletFactory.getMetadataFacelet(DefaultFaceletFactory.java:310)
    at com.sun.faces.facelets.impl.DefaultFaceletFactory.getMetadataFacelet(DefaultFaceletFactory.java:240)
    at com.sun.faces.application.view.ViewMetadataImpl.createMetadataView(ViewMetadataImpl.java:146)
    ... 41 more

配置web.xml中的Servlet-mappings:前缀/app/*和后缀*.xhtml
此外,还有针对模式@WebFilter的身份验证过滤器/app/pages/*(类似于this

服务器:GlassFish ver-5.0;其他一些JSF 2.3功能正常运行。

1 个答案:

答案 0 :(得分:0)

我使用JBoss EAP 7.0解决了同样的问题。虽然声称你可以使用无扩展名的URL,但它不是开箱即用的,只有JSF 2.3代码(就像你提到的博客一样)。我还没有看到一个可以在没有其他框架的情况下适用于所有应用程序服务器的工作示例。

我使用Rewrite,因为配置很简单,开箱即用。另一种选择可能是Omnifaces,但因为我们使用JBoss EAP 7.0和Omnifaces需要一个(脏!)解决方法来使它在JBoss EAP 7.0(Why is WildFly 10 + JSF 2.3 not working with Omnifaces 2.6.1?)上运行,我们放弃了Omnifaces。该链接提到了Wildfly 10,但它也适用于EAP 7.0.x版本。

这是我的maven依赖:

    <dependency>
        <groupId>org.ocpsoft.rewrite</groupId>
        <artifactId>rewrite-servlet</artifactId>
        <version>3.4.2.Final</version>
    </dependency>

这是我的重写配置:

import javax.servlet.ServletContext;

import org.ocpsoft.rewrite.annotation.RewriteConfiguration;
import org.ocpsoft.rewrite.config.Configuration;
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
import org.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;
import org.ocpsoft.rewrite.servlet.config.rule.Join;

/**
 * Configuration file to ensure extensionless url's with Rewrite framework from ocpsoft
 * 
 * @author Fabrizio Usai
 * 
 * @since 1.0.0
 *
 */
@RewriteConfiguration
public class ExtensionlessConfiguration extends HttpConfigurationProvider {

    /*
     * (non-Javadoc)
     * 
     * @see org.ocpsoft.common.pattern.Weighted#priority()
     */
    @Override
    public int priority() {
        return 10;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.ocpsoft.rewrite.config.ConfigurationProvider#getConfiguration(java.lang.Object)
     */
    @Override
    public Configuration getConfiguration(final ServletContext context) {
        return ConfigurationBuilder.begin().addRule(Join.path("/beheer").to("/beheren/welkom.xhtml")).addRule(Join.path("/{param}").to("/{param}.xhtml"))
                .addRule(Join.path("/").to("/welkom.xhtml")).addRule(Join.path("/beheren/{param}").to("/beheren/{param}.xhtml"));
    }

}

这会将以下页面映射到无扩展名网址:

  1. 基本应用程序URL /将在内部映射到welkom.xhtml,就像在web.xml欢迎列表中一样,由于第2点,它将在没有扩展名的情况下进行映射。因此,例如www.inter.it将在内部映射到www.inter.it/welkom.xhtml。
  2. 根应用程序中的所有页面都没有.xhtml。例如www.inter.it/welkom.xhtml将映射到www.inter.it/welkom
  3. / beheren文件夹下的所有页面都没有.xhtml。因此,例如www.inter.it/beheren/team.xhtml将映射到www.inter.it/beheren/team
  4. SAML入口点URL / beheer将内部映射到/beheren/welkom.xhtml,由于第3点,它将在没有扩展名的情况下进行映射。因此,例如www.inter.it/beheer将映射到www.inter 。它/ beheren / welkom.xhtml
  5. 祝你好运; - )