Spring Web MVC Tutorial : Part 1

In this tutorial, I will show step by step how to develop basic Spring MVC web application (using JSTL on presentation layer and integrated with Hibernate as ORM). Part 1 will cover the back-end part for this application.

As an example, we will make a basic administration system that will have a module to manage students. First, we need to prepare its’ table in the database. We will use only 1 table for this. After the table is created, we need to generate the persistent object and mapping file for it.(because we will use Hibernate to handle the database operations).

CREATE TABLE `student` (
  `student_id` int(11) NOT NULL auto_increment,
  `student_name` varchar(250) default NULL,
  `address` varchar(500) default NULL,
  `gender` int(11) default NULL,
  PRIMARY KEY  (`student_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Sorry for this. I try to make this as simple as I can, easy for you to understand it quickly. Let’s move on the next step. We need to have a DAO for this. Spring provides a set of abstract DAO classes that one can extend. These abstract classes have methods for providing the data source and any other configuration settings that are specific to the technology one currently is using.

I’m using HibernateDaoSupport class. It’s a super class for Hibernate data access objects. Requires a SessionFactory to be provided; in turn, this class provides a HibernateTemplate instance initialized from the supplied SessionFactory to subclasses. Take a look on this DAO implementation.

public class StudentDAOImpl extends HibernateDaoSupport implements StudentDAO{

    public void add(Student student) {
        getHibernateTemplate().save(student);
    }

    public void update(Student student) {
        getHibernateTemplate().update(student);
    }

    public void delete(Student student) {
        getHibernateTemplate().delete(student);
    }

    public Student get(Integer id) {
        return (Student)getHibernateTemplate().load(Student.class,id);
    }

    public List getAll() {
        return getHibernateTemplate().loadAll(Student.class);
    }
}

If we structured our application properly, we should only have to change the service layer classes to take advantage of the database persistence. The view and controller classes should not have to be modified, since they should be unaware of any implementation details of the service layer. So, we will create a simple service class for this application.

public class AdministrationServiceImpl implements AdministrationService {

    private StudentDAO studentDAO;

    public StudentDAO getStudentDAO() {
        return studentDAO;
    }

    public void setStudentDAO(StudentDAO studentDAO) {
        this.studentDAO = studentDAO;
    }

    public List getAllStudent() {
        return studentDAO.getAll();
    }

    public Student getStudent(Integer studentId) {
        return studentDAO.get(studentId);
    }

    public void registerStudent(Student student) {
        studentDAO.add(student);
    }

    public void updateStudent(Student student) {
        studentDAO.update(student);
    }

    public void unregisterStudent(Student student) {
        studentDAO.delete(student);
    }
}

For this service, we need to add a reference to a StudentDao interface plus a setter method for this reference. We will inject the instance of this DAO from Spring.

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
          "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://localhost/training"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingDirectoryLocations">
	<list>
                <value>/WEB-INF/classes</value>
            </list>
        </property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            </props>
        </property>
    </bean>
    <bean id="administrationService" class="com.training.service.AdministrationServiceImpl">
<property name="studentDAO" ref="studentDAO"></property>
    </bean>
    <bean id="studentDAO" class="com.training.StudentDAOImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

</beans>

Now, we are done for the back-end part.

Continue to Part 2

Respond to this post