Unable to create managed bean

3

I have a problem here in java that I do not know how to solve nor what it may be because I am learning to program with java for the web and I do not know the possible causes of the following error javax.servlet.ServletException: Unable to create managed bean testeHibernate .

Used code

testHibernate.java

import java.util.List;

 import javax.faces.application.FacesMessage;
 import javax.faces.bean.ManagedBean;
 import javax.faces.bean.ManagedProperty;
 import javax.faces.bean.SessionScoped;
 import javax.faces.context.FacesContext;
 import Classhiber.Utilizador;
 import DAO.UtilizadorDAO;
 @ManagedBean(name = "testeHibernate" )
 @SessionScoped
 public class testeHibernate {

private UtilizadorDAO utdao;

@ManagedProperty(value = "#{utiliza}")  
private Utilizador utiliza = new Utilizador();

public UtilizadorDAO getUtilizadorDAO() {
    return utdao;
}

public void setutilizador(UtilizadorDAO ut) {
    this.utdao = ut;
}

public Utilizador getutilizador() {
    return utiliza;
}

public void setutilizador(Utilizador utz) {
    this.utiliza = utz;
}

public String register() {
    // Calling Business Service
    utdao.addutilizador(utiliza);
    // Add message
    FacesContext.getCurrentInstance().addMessage(null, 
            new FacesMessage("The Employee "+this.utiliza.getUser()+" Is Registered Successfully"));
    return "";
}
}

Spring.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3307/xp" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>


<!-- Hibernate 4 SessionFactory Bean definition -->
<bean id="hibernate4AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>Classhiber.Utilizador</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
    </bean>


    <bean id="UtilizadorDAOimpl" class="Impl.UtilizadorDAOimpl">
        <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </bean>
</beans>

Face-config

<?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>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5" metadata-complete="true">
    <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>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <context-param>
        <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>
</web-app>

error - > It happens when I run the project on Tomcat-v7

javax.servlet.ServletException: Unable to create managed bean testeHibernate.  The following problems were found:
     - Property utiliza for managed bean testeHibernate does not exist.  Check that appropriate getter and/or setter methods exist.
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Thank you.

    
asked by anonymous 26.08.2015 / 17:47

1 answer

4

The cause of the error is this:

 - Property utiliza for managed bean testeHibernate does not exist. Check that appropriate getter and/or setter methods exist. 

You need to rename the getters/setters of the property you use. You have the methods setutilizador and getutilizador which arrow / returns the value of the property it uses. Rename them to setUtiliza and getUtiliza .

Whenever you make a get or set method ... adopt this pattern: setValueName, starting with a capital letter after the set or get. Another thing is the class name starts with a capital letter.

Try to follow Oracle's naming standards to facilitate the development of your programs as well as the maintenance and evolution thereof.

UPDATE:

This is wrong: @ManagedProperty(value = "#{utiliza}") It should look like this: @ManagedProperty(value = "utiliza")

The value that will be the "name" of the property you should only put its name (which in this case is "utiliza" ). This way #{utiliza} is just the syntax used by JSF to access the property defined in ManagedBean .

Notes

1)

I do not see the need to use @ManagedProperty since you will not "change the property name" from uses to another name. Annotating Bean as @ManagedBean you can already access the properties of that have the get method as #{testeHibernate.nomeDaPropriedade} in JSF. Even if you have a method getSoma( ) but do not have a sum property in your Bean, if you access it by JSF #{testeHibernate.soma} it will call the getSoma() .

2)

@ManagedBean(name = "testeHibernate" ) this attribute name is used to give the name of your Bean that will be accessed in JSF. By default, if you do not put the name parameter, it creates its name by copying the class name and by entering the first lowercase letter . Just put @ManagedBean for it to do this. In your case it would be testeHibernate . So need to put the attribute name, only if you want to make explicit the name it will receive , which I see as good practice too.

    
26.08.2015 / 20:26