How to properly install and configure JSF libraries via Maven?

前端 未结 1 470
南旧
南旧 2020-11-21 23:35

I\'m trying to deploy a JSF based application to Tomcat 6. The way my build system is setup, the WAR itself doesn\'t have any libraries in it, because this server is serving

相关标签:
1条回答
  • 2020-11-22 00:07

    When you're facing a "weird" exception suggesting that classes/methods/files/components/tags are absent or different while they are seemingly explicitly included in the web application such as the ones below,

    java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/faces/webapp/FacesServlet

    java.util.MissingResourceException: Can't find javax.faces.LogStrings bundle

    com.sun.faces.vendor.WebContainerInjectionProvider cannot be cast to com.sun.faces.spi.InjectionProvider

    com.sun.faces.config.ConfigurationException: CONFIGURATION FAILED

    The tag named inputFile from namespace http://xmlns.jcp.org/jsf/html has a null handler-class defined.

    java.lang.NullPointerException at javax.faces.CurrentThreadToServletContext.getFallbackFactory

    java.lang.AbstractMethodError at javax.faces.application.ViewHandlerWrapper.getWebsocketURL

    or when you're facing "weird" runtime behavior such as broken HTTP sessions (jsessionid appears in link URLs over all place), and/or broken JSF view scope (it behaves as request scoped), and/or broken CSS/JS/image resources, then the chance is big that the webapp's runtime classpath is polluted with duplicate different versioned JAR files.

    In your specific case with the ClassFormatError on the FacesServlet, it means that the JAR file containing the mentioned class has been found for the first time is actually a "blueprint" API JAR file, intented for implementation vendors (such as developers working for Mojarra and MyFaces). It contains class files with only class and method signatures, without any code bodies and resource files. That's exactly what "absent code attribute" means. It's purely intented for javadocs and compilation.

    Always mark server-provided libraries as provided

    All dependencies marked "Java Specifications" in Maven and having -api suffix in the artifact ID are those blueprint APIs. You should absolutely not have them in the runtime classpath. You should always mark them <scope>provided</scope> if you really need to have it in your pom. A well known example is the Java EE (Web) API:

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version><!-- 7.0 or 8.0 or newer --></version>
        <scope>provided</scope>
    </dependency>
    

    If the provided scope is absent, then this JAR will end up in webapp's /WEB-INF/lib, causing all trouble you're facing now. This JAR also contains the blueprint class of FacesServlet.

    In your specific case, you have an unnecessary JSF API dependency:

    <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>javax.faces-api</artifactId>
    </dependency>
    

    This is causing trouble because this contains the blueprint class of FacesServlet. Removing it and relying on a provided Java EE (Web) API as shown above should solve it.

    Tomcat as being a barebones JSP/Servlet container already provides JSP, Servlet and EL (and since 8 also WebSocket) out the box. So you should mark at least jsp-api, servlet-api, and el-api as provided. Tomcat only doesn't provide JSF (and JSTL) out the box. So you'd need to install it via the webapp.

    Full fledged Java EE servers such as WildFly, TomEE, GlassFish, Payara, WebSphere, etc already provide the entire Java EE API out the box, including JSF. So you do absolutely not need to install JSF via the webapp. It would only result in conflicts if the server already provides a different implementation and/or version out the box. The only dependency you need is the javaee-web-api exactly as shown here above.

    Installing JSF on Tomcat

    The proper way to install JSF in Tomcat is mentioned in our JSF wiki - Installing JSF. There are 2 JSF implementations, Mojarra and MyFaces. You should choose to install one of them and thus not both.

    Installing Mojarra on Tomcat:

    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>jakarta.faces</artifactId>
        <version><!-- Check https://eclipse-ee4j.github.io/mojarra --></version>
    </dependency>
    

    You can also check org.glassfish:jakarta.faces repository for current latest release version (which is currently 2.3.14). See also Mojarra installation instructions

    Installing MyFaces on Tomcat:

    <dependency>
        <groupId>org.apache.myfaces.core</groupId>
        <artifactId>myfaces-bundle</artifactId>
        <version><!-- Check http://myfaces.apache.org --></version>
    </dependency>
    

    You can also check org.apache.myfaces.core:myfaces-bundle repository for current latest release version (which is currently 2.3.5).

    Note that Tomcat 6 as being Servlet 2.5 container supports max JSF 2.1.

    Don't forget to install JSTL API along, by the way. This is also absent in Tomcat.

    <dependency>
        <groupId>jakarta.servlet.jsp.jstl</groupId>
        <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
        <version><!-- Check https://mvnrepository.com/artifact/jakarta.servlet.jsp.jstl/jakarta.servlet.jsp.jstl-api --></version>
    </dependency>
    

    Also note that since JSF 2.3, CDI has become a required dependency. This is available out the box in normal Java EE servers but not on servletcontainers such as Tomcat. In this case head to How to install and use CDI on Tomcat?

    See also:

    • What exactly is Java EE?
    • java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
    • How to install and use CDI on Tomcat?
    • JSF returns blank/unparsed page with plain/raw XHTML/XML/EL source instead of rendered HTML output
    0 讨论(0)
提交回复
热议问题