0
votes

This is my first time in stackoverflow...nice to meet you! I'm developing a web app that uses jsf 2.0 + primefaces 3.5 and spring 3.1 So...I need to inject spring services into managed bean and I would put in session the BeanUser that represent the logged user. The DI from spring to jsf works fine...but...I don't understand the @SessionScope managed bean I post my code and I hope in your help!

My applicationContext.xml

<!-- To enable the configuration based on annotations -->
<context:annotation-config base-package="it.valentina.eventiCinofili" />

<!-- To load ManagedBean as Component -->
<context:component-scan base-package="it.valentina.eventiCinofili" />   

<!-- To enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- LOB HANDLER -->
<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true"/>

<!-- DATASOURCE -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://xxxxxxxx:xxxx/dbinstance_262_1" />
    <property name="username" value="xxxxxx" />
    <property name="password" value="xxxxx" />
</bean>    

<!-- SESSION FACTORY -->     
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="it.valentina.eventiCinofili" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props> 
    </property>
    <property name="mappingResources">
        <list>
            <value>/it/valentina/eventiCinofili/hbm-mapping/Utente.hbm.xml</value>
        </list>
    </property>
</bean>

<!-- TRANSACTION MANAGER CONFIG -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

My faces-config

<?xml version="1.0"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                              http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
          version="2.0">

<application>
    <resource-bundle>
        <base-name>it.valentina.eventiCinofili.resourceBundles.ApplicationResourceLabel</base-name>
        <var>ApplResLbl</var>
    </resource-bundle>
    <resource-bundle>
        <base-name>it.valentina.eventiCinofili.resourceBundles.ApplicationResourceMsg</base-name>
        <var>ApplResMsg</var>
    </resource-bundle>

    <!-- SPRING CONFIG -->
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>

</application>

My CommonHeader

@Component 
@ManagedBean(name="commonHeader")
@RequestScoped
public class CommonHeader extends BaseBeanManaged implements Serializable {

private static final long serialVersionUID = 1L;
Logger log = Logger.getLogger(this.getClass());

@Autowired
private UtenteMgmtService utenteMgmtService;

@ManagedProperty(value = "#{beanUtente}")
BeanUtente beanUtente;

    ...........

   public BeanUtente getBeanUtente() {log.info("CommonHeader --> getBeanUtente(): "+beanUtente); return beanUtente;}
public void setBeanUtente(BeanUtente beanUtente) {this.beanUtente = beanUtente;}

My BeanUtente

@ManagedBean(name="homepage")
@SessionScoped
public class BeanUtente implements Serializable{
   ......
   ......

}

My jsp

<p:panelGrid columns="2">
     ....
 <p:column>
   <p:inputText id="username" value="#commonHeader.beanUtente.utenteCorrente.username}" required="true" requiredMessage="#{username_required}" feedback="true" />
 </p:column>
     .....
</p:panelGrid>

My stackTrace

An Error Occurred:
/pages/commons/commonHeader.xhtml @30,170 value="# {commonHeader.beanUtente.utenteCorrente.username}": Target Unreachable, 'beanUtente' returned null

Sorry if my post is too long! Thanks in advance for any suggestions!

Vale

1
You first need to verify that BeanUtente was properly instantiated. Then I also wouldn't recommend mixing @Component and @ManagedBean - kolossus

1 Answers

0
votes

Your ManagedBean is named "homepage", yet you refer to it by name as "beanUtente".

Either change @ManagedBean(name="homepage") to @ManagedBean(name="beanUtente")

or change @ManagedProperty(value = "#{beanUtente}") to @ManagedProperty(value = "#{homepage}").