我有一个使用Spring 3.0.1,Struts 2.0,Hibernate 3.3.2和Apache Tiles的Web应用程序,我想介绍OpenSessionInView模式。我已经在我的web.xml文件中添加了过滤器,但我不知道接下来该做什么。
Web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<display-name>
Struts2Hibernate3Spring3Tile2Integration
</display-name>
<welcome-file-list>
<welcome-file>articles.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>
org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
</param-name>
<param-value>/WEB-INF/tiles-def.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.apache.struts2.tiles.StrutsTilesListener
</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Filtre pour OpenSessionInView -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
ApplicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- On demande à Spring de gérer l’ensemble des classes package ma.ecommerce -->
<context:component-scan base-package="ma.ecommerce" />
<!-- Pour accéder aux paramètres définis dans le fichier database.properties -->
<context:property-placeholder location="classpath:database.properties" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<!-- La définition du bean sessionFactory en utilisant Spring ORM et Hibernate -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>ma.ecommerce.service.modele.Article</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}"></property>
<property name="url" value="${database.url}"></property>
<property name="username" value="${database.user}"></property>
<property name="password" value="${database.password}"></property>
</bean>
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="article" class="ma.ecommerce.presentation.action.ArticleAction" />
<bean id="articleBean" class="ma.ecommerce.domaine.ArticleBean" />
<bean id="articleDao" class="ma.ecommerce.dao.DaoImpl"/>
DaoImpl.java文件
@Repository("articleDao")
public class DaoImpl implements IDao
{
@Autowired
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public void saveArticle(Article article)
{
sessionFactory.getCurrentSession().saveOrUpdate(article);
}
@SuppressWarnings("unchecked")
@Override
public List<Article> getArticleList()
{
return (List<Article>)sessionFactory.getCurrentSession().createCriteria(Article.class).list();
}
答案 0 :(得分:1)
似乎唯一缺少的是在渲染视图后提交和关闭会话。也许你可以用过滤器来做到这一点:
https://developer.jboss.org/wiki/OpenSessionInView
但是如果你真的想要使用“开放式会议视图”(反)模式,你应该三思而后行。起初,它听起来像是一个避免LazyLoadingExceptions的简单解决方案,但你应该知道它带来的惩罚:
也许您可以确定哪些字段确实需要,并在服务层内准备投影/ DTO,而不是传播到视图。这将释放数据库并封装数据更改。
问题在于,您真的需要访问视图中的数据库吗?