title: OpenSessionInView Pattern
body: <p>I have a web application that uses Spring 3.0.1, Struts 2.0, Hibernate 3.3.2 and Apache Tiles and I want to introduce the OpenSessionInView Pattern. I already added the filter in my web.xml file, but I don't know what to do next. </p>
  
  <p>Web.xml file </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;display-name&gt;
      Struts2Hibernate3Spring3Tile2Integration
  &lt;/display-name&gt;
  &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;articles.jsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;
          org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
      &lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/tiles-def.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;
          org.springframework.web.context.ContextLoaderListener
      &lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;
          org.apache.struts2.tiles.StrutsTilesListener
      &lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;struts2&lt;/filter-name&gt;
      &lt;filter-class&gt;
          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      &lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;struts2&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;!-- Filtre pour OpenSessionInView --&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p></p>
  
  <p>ApplicationContext.xml file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;!-- On demande à Spring de gérer l’ensemble des classes package ma.ecommerce --&gt;
  &lt;context:component-scan base-package="ma.ecommerce" /&gt;
  &lt;!-- Pour accéder aux paramètres définis dans le fichier database.properties --&gt;
  &lt;context:property-placeholder location="classpath:database.properties" /&gt;
  &lt;tx:annotation-driven transaction-manager="hibernateTransactionManager" /&gt;
  
  &lt;!-- La définition du bean sessionFactory en utilisant Spring ORM et Hibernate --&gt;
  &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="annotatedClasses"&gt;
          &lt;list&gt;
              &lt;value&gt;ma.ecommerce.service.modele.Article&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt;
              &lt;prop key="hibernate.hbm2ddl.auto"&gt;${hibernate.hbm2ddl.auto}&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="${database.driver}"&gt;&lt;/property&gt;
      &lt;property name="url" value="${database.url}"&gt;&lt;/property&gt;
      &lt;property name="username" value="${database.user}"&gt;&lt;/property&gt;
      &lt;property name="password" value="${database.password}"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  
  
  &lt;bean id="hibernateTransactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="article" class="ma.ecommerce.presentation.action.ArticleAction" /&gt;
  &lt;bean id="articleBean" class="ma.ecommerce.domaine.ArticleBean" /&gt;
  &lt;bean id="articleDao" class="ma.ecommerce.dao.DaoImpl"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>DaoImpl.java file</p>
  
  <pre><code>@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&lt;Article&gt; getArticleList() 
  {
      return       (List&lt;Article&gt;)sessionFactory.getCurrentSession().createCriteria(Article.class).list();
  }
  </code></pre>