【问题标题】:Transaction control in a JSF Spring 3 projectJSF Spring 3 项目中的事务控制
【发布时间】:2014-09-22 13:44:12
【问题描述】:

我在使用 JSF 2.0 和 Primefaces、Spring 3 和 Hibernate 的项目中遇到了问题。 我能够正确配置整个项目,并且能够很好地使用 spring 的依赖注入和事务控制以及 JSF 视图、服务(接口和实现)、DAO(接口和实现)。 但是,每当我尝试列出或删除某个模型的对象时,它都会复制模型表中的当前对象。 以前有人遇到过这个问题吗? 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;
}
}

【问题讨论】:

  • 请提供您的源代码
  • 这里更新了代码...如果您需要其他文件,我可以在这里发布它们,例如服务接口和 impl 类或 DAO
  • 有人吗???我认为这可能是春季交易错误或 idk。

标签: spring hibernate jsf transactions


【解决方案1】:

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

@ViewScoped
public class bean{

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

【讨论】:

  • 问题是我的@Transactional 注释,它在错误的地方。无论如何感谢您的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-15
  • 2019-04-11
  • 2013-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多