Unable to create Managed bean

Asked

Viewed 486 times

3

I’m having a problem here in java that I don’t know how to solve or what it might be because I’m learning to program with java for the web and I don’t know the possible causes of the following error javax.servlet.ServletException: Unable to create managed bean testeHibernate.

Code used

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 -> Happens when I run the project in 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.

  • At what point does this error occur? Could you pass the whole error?

  • already edited the post .

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 change the name of getters/setters the property uses. You have the methods setutilizador and getutilizador arrow/returns the value of the property used. Change their name to setUtiliza and getUtiliza.

Whenever you are going to do a get or set method... adopt this pattern: setNomeDaVariable, starting with uppercase letter after the set or get. Another thing is the class name start with uppercase letter.

Try to follow the standards of nomenclature of Oracle to facilitate the development of its programs, as well as the maintenance and evolution of their.

UPDATE:

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

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

Remarks

1)

I see no need to use the @ManagedProperty since you will not "rename the property" you use to another name. Note the bean as @ManagedBean you can already access in JSF its properties that have the get method as follows #{testeHibernate.nomeDaPropriedade}. Even if you has a method getSoma( ) but does not have a property sum in your Bean, if you access from JSF #{testeHibernate.soma} he will call the method getSoma().

2)

@ManagedBean(name = "testeHibernate" ) that attribute name serves to name your bean which will be accessed in JSF. By default, if you do not put the name parameter, it creates his name by copying the class name and by lowering the first letter. Just put @ManagedBean for him to do so. In your case would be testeHibernate. Thus, no need of you putting the attribute name, only if you want to make explicit the name he will receive, what I see as a good practice too.

  • Obg. Now give me a mistake like this @14,61 value="#{testeHibernate.utiliza.user}": Target Unreachable, 'utiliza' returned nul . I’m not lucky at all :)

  • @ManagedProperty(value = "#{utiliza}") that wrong syntax, it should be @ManagedProperty(value = "utiliza")

  • thanks if you know of any other code mistakes I am making wrong tell me to learn. Thank you.

  • Thank you all. Sorry to ask again but this from those who do not know and as those who do not see it is true , I’ve searched the net and can not solve this error Ago 27, 2015 8:34:56 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute&#xA;WARNING: #{testeHibernate.save}: java.lang.NullPointerException&#xA;javax.faces.FacesException: #{testeHibernate.save}: java.lang.NullPointerException . I have been told that it was because the user was not initialized but I tried to initialize and always gives the same error. Thank you

  • To investigate a nullPointer Exception you need more details... if applicable, open another topic for this. But the reason for it is always when we try to access a property/method of an object that is null... you need to track the execution of the program with a debug to find out where the problem is

Browser other questions tagged

You are not signed in. Login or sign up in order to post.