Part three: Wiring the backend with Spring Framework and Hibernate

This is part three of the Minesweeper portlet tutorial.

This part of the tutorial describes how we can wire our Struts actions with Spring beans and how we can utilize the Spring dao support with Hibernate.

Saving the highscore to database

The following steps try to illustrate how to combine Struts with Spring and Hibernate for easy data access.
  1. First we create the HighScore class to represent a highscore entry in the database
  2. public class HighScore {
        private Long id;
        private int score;
        private String name;
  3. We map it to database table highscore with HighScore.hbm.xml mapping file
  4. <hibernate-mapping>
        <class name="com.blogspot.pragmatastic.minesweeper.HighScore" table="highscore">
            <id name="id" column="id">
                <generator class="native"/>
            <property name="score" column="score"/>
            <property name="name" column="name"/>
  5. Then we create a HighScoreDao class for saving the data with the spring hibernate support
  6. public class HighScoreDaoImpl extends HibernateDaoSupport implements HighScoreDao {
        public void save(HighScore highScore) {
  7. Now we create a spring application context file minesweeper-applicationContext.xml
  8. First we define the data source for the database which is configured by
  9. <beans>
        <bean id="dbConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location">
            <property name="placeholderPrefix" value="${db."/>
            <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="${db.url}"/>
            <property name="username" value="${db.username}"/>
            <property name="password" value="${db.password}"/>
  10. Then we define the hibernate session factory and list our HighScore.hbm.xml mapping file as resource
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="mappingResources">
            <property name="hibernateProperties">
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="">update</prop>
                    <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
  12. And finally we define our dao
        <bean id="highScoreDao" class="com.blogspot.pragmatastic.minesweeper.HighScoreDaoImpl">
            <property name="sessionFactory" ref="sessionFactory"/>
  14. Now we need to make the beans defined in the minesweeper-context.xml available for our struts action. First we configure spring to load the context file and create a application context from it in web.xml
  15. <web-app>
  16. Next we enable the struts spring integration in the struts.xml
  17. <struts>
        <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>
  18. After that we can refer to the HibernateDao object in HighScoreAction, spring will automatically wire our action with the bean
  19. public class HighScoreAction extends ActionSupport {
        private HighScore highScore;
        private HighScoreDao highScoreDao;
        // Auto wired byName from spring
        public void setHighScoreDao(HighScoreDao highScoreDao) {
            this.highScoreDao = highScoreDao;
        public HighScore getHighScore() {
            return highScore;
        // Saves the form
        public String save() {
            return SUCCESS;
Continue to Part four: Making the UI with jQuery and Freemarker


No comments:

Post a Comment