Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF annotations don't work with Spring-boot

I had been tried to use info from Spring Boot and JSF/Primefaces/Richfaces, but for me it doesn't work.

I use Java 8, maven, Spring-boot and JSF with PrimeFaces. I would like to have executable jar and run my application via main method or from command line java -jar myApp.jar.

The problem - JSF-annotations (@ManagedBean, @ManagedProperty) are ignored.

Pom file:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.3.RELEASE</version>
</parent>

<dependencies>
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>7.0.54</version>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-logging-juli</artifactId>
        <version>7.0.54</version>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <version>7.0.54</version>
    </dependency>

    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>5.0</version>
    </dependency>

    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.7</version>
    </dependency>

    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.7</version>
    </dependency>
    ...
</dependencies>

I also have tried to add/remove javax.el-api/javax.el/jstl - the same result. For bean initialization I have added section to faces-config.xml

When I change spring-boot-starter-web to spring-boot-starter and have spring-web (according to solution from mentioned post from Herick) I got

java.io.FileNotFoundException: class path resource [org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.class] cannot be opened because it does not exist

My config class:

@Configuration
@EnableAutoConfiguration//(exclude = {WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class})
@ComponentScan("hello")
public class Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class);
    }

    @Bean
    public FacesServlet facesServlet() {
        return new FacesServlet();
    }

    @Bean
    public ServletRegistrationBean facesServletRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(facesServlet(), "*.xhtml");
        registration.setName("facesServlet");
        return registration;
    }

    @Bean
      public ServletListenerRegistrationBean<ConfigureListener> jsfConfigureListener()         {
          return new ServletListenerRegistrationBean<ConfigureListener>(new ConfigureListener());
      }

}

With (exclude = {WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class}) web.xml configuration doesn't work. In mentioned post was:

@Bean
public ListenerRegistationBean jsfConfigureListener() {
    return new ListenerRegistrationBean(new ConfigureListener());           
}     

ListenerRegistationBean is absent in my spring-boot and I have used ServletListenerRegistrationBean instead.

My web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"       
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"     
    version="3.1">
    <display-name>Test</display-name>
    <servlet>
        <servlet-name>facesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>facesServlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    <error-page>
        <location>/error.xhtml</location>
    </error-page>
</web-app>

And faces-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"              
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"  
    version="2.2">
    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>

    <managed-bean>
      <managed-bean-name>managedBeann</managed-bean-name>
      <managed-bean-class>hello.ManagedBeann</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
</faces-config>

Because nonworking annotations is used. By the way PrimeFaces is working.

My purpose is force JSF-annotation to work, because in real project without them it is impossible.

like image 678
Olga Avatar asked Jul 10 '14 08:07

Olga


1 Answers

Disclaimer

I am answering this based on what I think you were trying to acheive even though my answer does not match the question title.

You said "My purpose is force JSF-annotation to work, because in real project without them it is impossible." I'm guessing you mean "impossible" because putting managed beans in the faces-config.xml is cumbersome. So to this end I am going to not use the faces-config.xml to manage beans.

I'm going to show you an alternative that uses Spring annotations which is very non-cumbersome and I feel accomplishes your original goal.

Answer

Example -- https://github.com/Zergleb/Spring-Boot-JSF-Example

I looked over your question the other day and decided to try and make this work and I put my results on github (Link above). This example should allow you to write a JSF application using Spring annotations instead of JSF annotations for example you'll say

@Component
@Scope("view")
//The example above contains an implementation of the View Scope in Spring.

instead of

@ManagedBean
@ViewScope

and you'll then be able to use Spring for all of your dependency injection.

I used gradle instead of maven so this means your dependencies are in the build.gradle instead of the pom.xml I had to add these in order to make everything work. Those should be easy enough to translate to a pom.xml I imagine.

compile group: 'javax.el', name: 'el-api', version: '1.0'
compile group: 'com.sun.el', name: 'el-ri', version: '1.0'
compile group: "javax.servlet.jsp" name: "jsp-api" version: "2.1"

My web.xml only has one servlet now and I removed the servlet-mapping and all of the other attributes of the web.xml

(I'm still working on how to remove this web.xml altogether check the example for any updates on whether I figured it out or not)

<web-app ... same as before>
    <servlet>
        <servlet-name>facesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>
</web-app>

faces-config.xml now has no managed beans

<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"  version="2.2">
     <application>
         <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
     </application>
</faces-config>

I do not have this right now but we might want to consider having an empty in the web.xml I haven't researched this a ton but one of the spring-project examples on github contains this code

https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-traditional/src/main/webapp/WEB-INF/web.xml

<!-- Disables Servlet Container welcome file handling. Needed for compatibility with Servlet 3.0 and Tomcat 7.0 -->
<welcome-file-list>
    <welcome-file></welcome-file>
</welcome-file-list>

I hope that answers your question. If I left something out try and reference the example code.

Example

https://github.com/Zergleb/Spring-Boot-JSF-Example

Runs a spring boot application that should both run Spring MVC and JSF in one application sharing a common context.(I included this in the answer because you referenced this link in your question Spring Boot and JSF/Primefaces/Richfaces which says that mixing Spring MVC and JSF is impossible but I have working in my example code.

like image 184
Zergleb Avatar answered Oct 13 '22 02:10

Zergleb