4

I created a Maven Web Application using JSF 2.2 , Primefaces, Tomcat 7 dependencies.

My JSF's implemention is Mojarra 2.2.4, I added this dependecies on my POM:

    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.4</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.4</version>
        <scope>compile</scope>
    </dependency>

But unfortunately, I have problem with tomcat7-maven-plugin and its embedded tomcat. If I use tomcat7:run command, my webapp starts without problem, but when it try to load managed bean I receive this error:

Target Unreachable, identifier 'testBean' resolved to null

This is a sign that the webapp is using JSF 1.x instead of JSF 2.x. The configurator of JSF 1.x does not recognize @ManagedBean annotations which will cause that they're not loaded/initialized automagically without the need for faces-config.xml.

I'm using tomcat embedded 7.0.50, configured using it: http://tomcat.apache.org/maven-plugin-trunk/tomcat7-maven-plugin/adjust-embedded-tomcat-version.html

But despite I doesn't recognize @ManagedBean annotaions, it works only using managedbean tag on faces-config.xml.

Is there a way to include JSF 2.x support to Tomcat Embedded for maven7-tomcat-plugin?

PS: tomcat7:run-war works, but I don't like because obviously it a static run, without any possibility to change xhtml code (e.g.) on fly.

giaffa86
  • 708
  • 1
  • 9
  • 22
  • I don't understand the problem. JSF is deployed with your application isn't it? – Gimby Feb 10 '14 at 09:51
  • It is, I have JSF dependencies on my POM, but the problem is Maven Tomcat Embedded. It doesn't recognizes JSF 2.X. In fact, if I build WAR and deploy it on external Tomcat, webapp works. – giaffa86 Feb 10 '14 at 10:09
  • Interesting cause of the eternal *"Target Unreachable, identifier [beanname] resolved to null"* by the way. I've previously seen questions about this problem coming along without an obvious cause. I wonder what in earth Maven+Embedded-Tomcat is doing here. – BalusC Feb 10 '14 at 10:11
  • BalusC doesn't get me scared. Is there a way to configure maven-embedded-tomcat? I don't want every time deploy my war on server. – giaffa86 Feb 10 '14 at 10:19
  • My point is that the webapp CANNOT be using JSF 1.x, because you are deploying JSF 2.x. It doesn't magically change to a different set of jars that are not even there. My current guess is that the annotations are in fact not getting processed, perhaps in general and not specifically only JSF 2.x annotations. – Gimby Feb 10 '14 at 10:51
  • Ok, should be as you said. In that case, how could I resolve it? I remberer you that I have problem only if I use maven-embedded-tomcat. – giaffa86 Feb 10 '14 at 11:12
  • First of all prove that what I say is in fact true. If I might remind you, you need to solve this. Don't start making assumptions that the answer MUST come from this thread. The only thing I can do is help you to find topics to further research. – Gimby Feb 10 '14 at 15:01
  • Thank you for helping me, but I need to figure out what I should search. I can't do further research if I am not focused on problem. Maybe Do You mean something like that? http://stackoverflow.com/questions/4941355/maven-annotation-processing-with-maven-compiler-plugin – giaffa86 Feb 10 '14 at 15:57
  • 1
    The problem is by effect of the plugin (it is not running a webapp in a WAR or a directory), the annotation scanning algorithm does not behave properly. If you are using MyFaces try set org.apache.myfaces.annotation.SCAN_PACKAGES param, to indicate the algorithm to scan the selected packages. – lu4242 Feb 10 '14 at 16:01
  • I am using Mojarra 2.2. Where do you put this property? In faces-config file? Is there a similar rule for Mojarra? – giaffa86 Feb 10 '14 at 16:34
  • I've better specified my JSF's implementation on the question. – giaffa86 Feb 10 '14 at 16:48
  • 1
    It is a param that you should put on web.xml. I don't know the details behind Mojarra, but I suppose it exists a similar parameter but I don't know if it will work, because we had to do some special stuff to make it work long time ago, different to the default algorithm proposed by the spec. What I can tell you is the param was added in MyFaces precisely to fix these situations. Switch to MyFaces is easy and will make your life more easier. Try the 2.2.0 artifacts. – lu4242 Feb 11 '14 at 05:31
  • Ok I switched to MyFaces and added annotation scanning algorithm into my web.xml. Now it works great! Thanks lu4242! – giaffa86 Feb 11 '14 at 09:24

1 Answers1

3

I resolved switched from Mojarra 2.2.4 to MyFaces 2.2.0 following these steps:

  1. remove Mojarra dependecies in my POM
  2. add MyFaces dependecies in my POM:

    <dependency>
        <groupId>org.apache.myfaces.core</groupId>
        <artifactId>myfaces-api</artifactId>
        <version>2.2.0</version>
        <scope>compile</scope>
    </dependency>
    
    <dependency>
        <groupId>org.apache.myfaces.core</groupId>
        <artifactId>myfaces-impl</artifactId>
        <version>2.2.0</version>
        <scope>compile</scope>
    </dependency>
    
  3. change listener-class on my web.xml

    <listener>
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
    

  4. add context-param on my web.xml

    <context-param>
         <description>Defines which packages to scan for beans, separated by commas.
             Useful for when using maven and jetty:run (version 6) or tomcat:run
        </description>
        <param-name>org.apache.myfaces.annotation.SCAN_PACKAGES</param-name>
        <param-value>eu.dedalus</param-value>
    

.5 remove jsf-api e jsf-impl from WEB-INF/lib.

.6 [ECLIPSE ONLY] add myfaces.api and myfaces-impl libraries in Build Path using M2REPO variable and remove jsf-api and jsf-impl libraries.

Thanks to https://stackoverflow.com/users/814956/lu4242!

Community
  • 1
  • 1
giaffa86
  • 708
  • 1
  • 9
  • 22
  • 1
    I know that you have long resolved the problem, but I found a quick solution by using Jetty plugin instead of Tomcat. – Leonardo Nov 06 '14 at 20:47