JSF Spring 3项目中的事务控制

时间:2014-09-22 13:44:13

标签: spring hibernate jsf transactions

我在使用带有Primefaces,Spring 3和Hibernate的JSF 2.0的项目中遇到了问题。 我能够正确配置整个项目,并且能够使用spring的依赖注入和事务控制,以及JSF视图,服务(接口和impl),DAO(接口和impl)。 但每当我尝试列出或删除某个模型的对象时,它都会复制模型表中的当前对象。 以前有人有这个问题吗? PS:我用@Component(“myBeanName”)注释我的控制器类,所以我的jsf视图可以理解bean名称来调用方法和使用对象。

这是applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<context:component-scan base-package="br.com.dentrio" />
<context:annotation-config />
<context:spring-configured />

<!-- Data Source Declaration -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dentriodb" />
    <property name="user" value="root" />
    <property name="password" value="root" />
    <property name="maxPoolSize" value="10" />
    <property name="maxStatements" value="0" />
    <property name="minPoolSize" value="5" />
</bean>

<!-- Session Factory Declaration -->
<bean id="SessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- <property name="packagesToScan"> <list>     <value>net.javabeat.spring.model</value> 
        </list> </property> -->
    <property name="packagesToScan" value="br.com.dentrio.model"></property>
    <!-- <property name="annotatedClasses"> -->
    <!-- <list> -->
    <!-- <value>net.javabeat.spring.model.*</value> -->
    <!-- </list> -->
    <!-- </property> -->
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.format_sql">true</prop>
        </props>
    </property>
</bean>

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

<!-- Transaction Manager is defined -->
<bean id="txManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="SessionFactory" />
</bean>

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

<application>
    <locale-config>
        <default-locale>pt_BR</default-locale>
    </locale-config>
</application>

<application>
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>

<application>
    <message-bundle>
        messages
    </message-bundle>
 </application>

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

<display-name>PrimeFaces Web Application</display-name>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext.xml
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-       class>
</listener>
<listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
</listener>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>

<!-- Welcome page -->
<welcome-file-list>
    <welcome-file>home.xhtml</welcome-file>
</welcome-file-list>

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

<!-- Map these files with JSF -->
<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>*.jsf</url-pattern>
</servlet-mapping>
<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>

</web-app>

我的Bean控制器

package br.com.managedController;

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

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.view.ViewScoped;

import org.springframework.dao.DataAccessException;

import br.com.dentrio.comum.BaseBean;
import br.com.dentrio.model.Dentista;
import br.com.dentrio.service.DentistaServiceImpl;

@Component("dentistaBean")
public class DentistaBean extends BaseBean implements Serializable {

private static final long serialVersionUID = 1L;

@Autowired
DentistaService dentistaService;

private Date data;
private Dentista dentista = new Dentista();
private Dentista dentistaSelecionado;
List<Dentista> listaDentistas = new ArrayList<Dentista>();

public void prepararPedidos() {
carregarListaDentistas();
}

@PostConstruct
private void carregarListaDentistas() {
listaDentistas = new ArrayList<Dentista>();
listaDentistas.addAll(dentistaService.listDentistas());
}

public String adicionarDentista() {
try {
    dentistaService.addDentista(dentista);
    carregarListaDentistas();
    addMessage("Sucesso", "Dentista adicionado com Sucesso!");
    return "listarDentistas?faces-redirect=true";

} catch (DataAccessException e) {
    e.printStackTrace();
}
return ERROR;
}

public void deletarDentista(Integer dentistaId) {
try {
    Dentista dentista = dentistaService.getDentista(dentistaId);
    dentistaService.deletarDentista(dentista);
    addMessage("Sucesso", "Dentista deletado com Sucesso!");
    carregarListaDentistas();
} catch (Exception e) {
    e.printStackTrace();
}
}

public void resetForm() {
dentista = new Dentista();
}

public Dentista getDentista() {
return dentista;
}

public void setDentista(Dentista dentista) {
this.dentista = dentista;
}

public Date getData() {
return data;
}

public void setData(Date data) {
this.data = data;
}

public List<Dentista> getListaDentistas() {
return listaDentistas;
}

public void setListaDentistas(List<Dentista> listaDentistas) {
this.listaDentistas = listaDentistas;
}

public Dentista getDentistaSelecionado() {
return dentistaSelecionado;
}

public void setDentistaSelecionado(Dentista dentistaSelecionado) {
this.dentistaSelecionado = dentistaSelecionado;
}

public DentistaService getDentistaService() {
return dentistaService;
}

public void setDentistaService(DentistaService dentistaService) {
this.dentistaService = dentistaService;
}
}

1 个答案:

答案 0 :(得分:-2)

我所做的是将Spring服务作为JSF bean中的托管属性注入。您可以毫无问题地使用@ViewScoped。

@ViewScoped
public class bean{

@ManagedProperty(value="#{springService}")
private SpringService springService;
//getter and setter