0

这是我第一次使用stackoverflow...很高兴认识你!我正在开发一个使用 jsf 2.0 + primefaces 3.5 和 spring 3.1 的 Web 应用程序所以......我需要将 spring 服务注入托管 bean,并且我会将代表登录用户的 BeanUser 放入会话中。从 spring 到 jsf 的 DI 工作正常......但是......我不明白 @SessionScope 托管 bean 我发布了我的代码,希望能得到你的帮助!

我的 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>

我的面孔配置

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

我的共同标头

@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;}

我的 BeanUtente

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

}

我的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>

我的堆栈跟踪

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

对不起,如果我的帖子太长了!在此先感谢您的任何建议!

4

1 回答 1

0

您的 ManagedBean 被命名为“主页”,但您将其称为“beanUtente”。

要么更改@ManagedBean(name="homepage")@ManagedBean(name="beanUtente")

或更改@ManagedProperty(value = "#{beanUtente}")@ManagedProperty(value = "#{homepage}").

于 2013-06-13T19:45:56.033 回答