Tuesday, April 30, 2013

Working with Simple Form Controller using Spring & Hibernate

A SimpleFormController is used to display a form to the user (form view) and upon perfomring the appropriate task it will display success form( success view).

In order to integrate spring & hibernate first of all we need to add all the related jar's to our project lib folder and build path.

The deployment descriptor  (web.xml) with appropriate configuration.





Now create the context configuration files applicationContext.xml and spring-servlet.xml inside WEB-INF folder.

As part of the applicationContext.xml , generally we used to do the data access configurations.





Here, We are using HibernateTemplate in our application . So we need to get that object first. As HibernateTemplate depends on SessionFactory object we have configured LocalSessionFactoryBean and it's reference was given to Hibernatetemplate.

As the SessionFactory bean again depends on datasource object , we have configured BasicDataSource class and it's reference was given to LocalSessionFactory bean.

As part of LocalSessionFactory bean we can provide more information about hibernate like mapping files and hibernate properties.

ResourceBundleMessageSource is the bean which is used for displaying valid messages in our form from the property files.

Create a table called 'user' in the mysql database created(mydb here).We have given two columns phone and username where phone is the primary key column.

Now create the hbm file User.hbm.xml in our application which was configured in applicationContext.xml with property mappingResources.





Create a controller SaveDataController which extends SimpleFormController.



create two jsp's userForm.jsp and success.jsp inside WEB-INF/jsp folder. userForm.jsp is the form view with two fileds User Name and Phone


We need to create a command class for the userForm.jsp. User is the command class with two properties userName , phone as the user table have two columns.




To write the DAO logic we have created an interface UserDao.




UserDaoImpl provides implementation to the interface where we have written code to save the data in the database.





save() simply saves the user object into the databse corresponding table (user).

Create a validator class implementing Validator interface. Here we are performing our validations to the form.





Create a property file to display the error messages during validation.




Now finally, configure all the beans in spring-servlet.xml .





When running the application the userForm will be displayed . Without entering the data if we try to submit the form, we can observe the validation messages.



if we enter valid details in the text fields we can observe the success information.



How it's working:

When ever request sent from client to server which is a GET request, SimpleFormController will get the form view which is configured and displays to the client.

Now, the client enters the data(or without entering the data) and submits the form. Now the request is POST request, SimpleFormController will check for any validator configured for this appropriate controller. If exists, it will call the validate() of the validator where we performed our validations. Incase of errors, SimpleFormController will display the same form view again to the client. If there are no errors in the validate() , then it will display success view to the client.

Download Source



3 comments: