HTTP 500无法使用primeface创建bean

时间:2014-11-07 08:46:40

标签: spring jsf-2 primefaces

我有一个带@controller的列表并且它可以工作,但是当我想使用primefaces并通过其他EmpresaBean更改EmpresaController时,我有下一个错误,我不知道解决方案。可以帮帮我吗?对不起我的英语,我正在学习它。 THX!

javax.servlet.ServletException: No se puede crear el bean administrado empresaMB.  Se han encontrado los problemas siguientes:
     - No existe la propiedad empresasService para el bean administrado empresaMB.
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

的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"
        version="2.5">
        <!-- JSF mapping -->
        <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>
        <!-- welcome page -->
        <welcome-file-list>
            <welcome-file>index.xhtml</welcome-file>
        </welcome-file-list>

            <!-- Add Support for Spring -->
    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener>
    <listener>
    <listener-class>
    org.springframework.web.context.request.RequestContextListener
    </listener-class>
    </listener>
    </web-app>
**faces-config.xml**
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <!-- JSF and Spring are integrated -->
    <application>
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>

    <!-- configuration of navigation rules -->
 <navigation-rule>
     <from-view-id>index.xhtml</from-view-id>
 </navigation-rule>

</faces-config>

应用-config.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:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Enable autowire -->
    <context:annotation-config />

    <!-- Enable component scanning -->
    <context:component-scan base-package="com.atorres.springapp" />



    <!-- Data Source Declaration -->
  <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

  <!-- JDBC Data Source. It is assumed you have MySQL running on localhost port 3306 with
       username root and blank password. Change below if it's not the case -->

       <bean id="myDataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" 
        p:username="${jdbc.username}"
        p:password="${jdbc.password}" 
        p:validationQuery="${jdbc.validationQuery}" />

 <!-- Hibernate Session Factory -->
  <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="packagesToScan">
      <array>
        <value>com.atorresbr.springapp</value>
      </array>
    </property>
    <property name="hibernateProperties">
    <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop> 
            </props>

    </property>
  </bean>

  <!-- Hibernate Transaction Manager -->
  <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory"/>
  </bean>

  <!-- Activates annotation based transaction management -->
  <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

的index.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>My Team</title>
    </h:head>

    <h:body>
        <h2>My Team</h2>
        <hr/>
        <h:outputText value="Hello JSF"/>

 <p:dataTable var="empresa" value="#{empresaMB.empresas}">
    <p:column headerText="Clave">
        <h:outputText value="#{empresa.clave}" />
    </p:column>


    <p:column headerText="Nombre">
        <h:outputText value="#{empresa.nombre}" />
    </p:column>


</p:dataTable>


    </h:body>
</html>

EmpresaManagedBean.java

package com.atorres.springapp;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;

import org.springframework.dao.DataAccessException;

import com.atorres.springapp.Empresa;
import com.atorres.springapp.EmpresasServiceImpl;

@ManagedBean(name="empresaMB")
@ViewScoped
public class EmpresasManagedBean implements Serializable {

    private static final long serialVersionUID = 1L;

    //Spring User Service is injected...
    @ManagedProperty(value="#{empresasService}")
    EmpresasServiceImpl empresasService;

    List<Empresa> empresaList;

    public List<Empresa> getUserList() {
              empresaList = new ArrayList<Empresa>();
              empresaList.addAll(empresasService.findAll());
              return empresaList;
         }

}

1 个答案:

答案 0 :(得分:1)

@ManagedProperty(value="#{empresasService}")指示框架查找名为empresasService的JSF工件,而EmpresasService可能不是JSF工件。最简单的解决方案是删除注释并自己实例化服务类。

EmpresasServiceImpl empresasService = new EmpresasServiceImpl();

或者,以其他方式注入它。如果您仍在使用Spring,可以将它与JSF结合使用,并使用@Autowired进行服务注入(有关于如何配置Spring以使用JSF的在线文章),或仅使用{注释EmpresasServiceImpl {1}}并查看是否有效(我认为在这种情况下你必须为@ManagedBean字段设置一个setter。)