Simple Struts 2 and hibernate integration
Concepts you need to be familiar with before continuing
- If you are unfamiliar with namespaces in Struts 2 then you may want to check out the Namespaces in Struts 2 tutorial.
- If you are unfamiliar with wildcard mappings in struts 2 then you may want to check out the Wilcard mappings in Struts 2 tutorial.
Database
The database is very simple and consists of only a single table with three columns (user_id, username and password). You can either create your own and then edit the source code to suit your database or download the sample source code and install the database which is in /WEB-INF/src/database/db.sql.
Integratng Hibernate and Struts 2
When I integrate Hibernate and Struts 2 I add another filter for Hibernate so that there are now two filters in web.xml:
<!--
This must come before the struts2 filter or else Hibernate will not be
initialized when the struts action is called.
-->
<filter>
<filter-name>hibernate</filter-name>
<filter-class>struts2you.HibernateFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernate</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
The code for the Hibernate filter is taken directly from the Hibernate documentation which you can find here. The section on sessions and transactions also makes good reading if you are having difficulty with the open session in view documentation in the first link.
The Hibernate filter needs to come first in this setup and to understand why you need to understand a little about how filters work. When a request is received by the servlet container it looks in the web.xml file and looks for any filters. When it finds one it first executes that filter before continuing on with the request. The Hibernate filter init() method:
@Override
public void init(FilterConfig filterConfig) throws ServletException {
sessionFactory = HibernateUtil.getSessionFactory();
}
sets up Hibernate in our web application (with the call to HibernateUtil) and then the HibernateFilter.doFilter(…) method (which is called by the servlet container for every request via the mapping in web.xml) starts a Hibernate transaction for every request. Once the Hibernate transaction is open then the filter calls the chain.doFilter(…) method as you can see below:
try {
sessionFactory.getCurrentSession().beginTransaction();
// Call the next filter (continue request processing)
chain.doFilter(request, response);
// Commit and cleanup
sessionFactory.getCurrentSession().getTransaction().commit();
}
The above code is part of our HibernateFilter.doFilter(…) method.
The chain.doFilter(…) method call continues on with the request in the usual way but in ours case we have a second filter defined in the web.xml. So the request jumps to that filter (because of the call to chain.doFilter(…)) which in our case is the Struts 2 filter. The Struts 2 then takes over and handles the processing of our request, maybe calling the action we wrote to handle this request. Once our action has finished then processing goes back to the Struts 2 filter and when that is finished, processing goes back to our Hibernate filter, continuing on from line 8 above. The hibernate filter then commits the Hibernate transaction and closes the Hibernate session. This means we have wrapped our request in our Hibernate filter which opens the Hibernate transaction and then closes it when our struts action has finished. In our action code we will never have to open the Hibernate transaction ourselves as our filter has already done that for us. Notice that the Hibernate filter needs to come first or else the servlet container will allow Struts 2 to handle the request before Hibernate, calling our action which needs Hibernate, and as the Hibernate transaction would not be open our action will throw an error.
That’s it, Hibernate is now working with Struts 2. If you want to see the fully working example which includes database reads and displays the output on a jsp, then download the war file at the top of this page and drop it into your servler container.
22.02.2009 01:11 - Posted by doahh - Comments: 0 - Java

Comments: