0

I use JSF with Spring Boot. My problem is, that if I use the @javax.faces.bean.ManagedBean annotation I receive

javax.el.PropertyNotFoundException: Target Unreachable, identifier [numberTest] resolved to null

I resolved the issue with put also the @Component annotation on the managed bean. The other way was to put the Java classes in the WEB-INF.

My question is: both above-mentioned way seems to be bad and I think that they will cause trouble. What would be the solution to this problem?

I Googled a lot and tried every solution found.

(An additional fact, is that with @javax.annotation.ManagedBean the core JSP functions work (I didn't receive the error) but for example EL doesn't.)

Here is the code, in which with I receive the PropertyNotFoundException:

Configuration.java

import javax.faces.webapp.FacesServlet;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableWebMvc
@Configuration
@EnableJpaRepositories(basePackageClasses = {})
@ComponentScan(basePackageClasses = { CLASSES_ARE_HERE })
public class Configuration {

    public static void main(String[] args) {    
        SpringApplication.run(Configuration.class, args);
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        FacesServlet servlet = new FacesServlet();            
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet, "*.xhtml");
        return servletRegistrationBean;
    }

}

The managed bean:

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

@ManagedBean
@ApplicationScoped
public class NumberTest {


    private int theNumber=0;
    
    
    public NumberTest() {
        
    }

    public void addOne() {
        theNumber++;
    }
    
    public void addN(int n) {
        theNumber+=n;
    }
    
    public int getTheNumber() {
        return theNumber;
    }

    public void setTheNumber(int theNumber) {
        this.theNumber = theNumber;
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="3.1">
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>
    <!-- Time in seconds that facelets should be checked for changes since last 
        request. A value of -1 disables refresh checking. -->
    <context-param>
        <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>1</param-value>
    </context-param>

    <!-- Set the project stage to "Development", "UnitTest", "SystemTest", or 
        "Production". -->
    <!-- An optional parameter that makes troubleshooting errors much easier. -->
    <!-- You should remove this context parameter before deploying to production! -->
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
</web-app>

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>
</faces-config>

The .xhtml

<!DOCTYPE html>
<f:view xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui" encoding="UTF-8">

<html>
    <h:head>
    </h:head>

    <h:body>
        <h1>Szám</h1>
        <h3>#{numberTest.theNumber}</h3>
        <h:form>
            <br></br>
            <h:commandButton action="#{numberTest.addOne()}" value="Add one"></h:commandButton>         
        </h:form>
        <h:form>
            <br></br>
            <h:inputText binding="#{input1}" />
            <h:commandButton value="Add N" action="#{numberTest.addN(input1.value)}" />          
        </h:form>
    </h:body>
</html>
</f:view>
SiGe
  • 341
  • 6
  • 18
  • PrimeFaces **never** manages beans. Spring can, JSF can, CDI can, PrimeFaces can't and java-se can't either. Tried posting the error in google and maybe adding 'stackoverflow' in the searchquery – Kukeltje Sep 20 '17 at 14:55
  • I recently wrote an answer in order to integrate Spring Boot and JSF, you should consider reading it: https://stackoverflow.com/a/46190826/1199132 (First of all, get rid of JSF annotations and web.xml). – Aritz Sep 20 '17 at 15:38
  • Thank you to help me to understand better the JSF. @XtremeBiker This is a good description. However I'm unable to get rid of web.xml. I followed your steps, but the application doesn't start without settings in web.xml. – SiGe Sep 21 '17 at 12:27

0 Answers0