Friday, May 3, 2013

Resolving Neither BindingResult nor plain target object issue

The common error stack we observe when we are working with spring tag library is 'Neither BindingResult nor plain target object for bean name available as request attribute'. 

The error message itself explaining that we have not mentioned the corresponding command object details (command name, command class).

When we face this problem:

When we are using forms in our application, in general we will use tag library provided by spring

i.e, 

As part of the  spring tag library we have tag's to use different html components as part of our application (like form, textfiled,checkbox .. etc).

Here is the sample code ..


Here we have not mentioned the commandName attribute as part of form tag, which means we have not provided the details of command object.

one more case we may observe .,


In the spring-servlet.xml we have configured our class SaveDataController with all required properties except commandName. This is the other case where we used to get the same error.

How to Resolve this :

The solution to the above mentioned problem is so simple. We need to provide command object details in order to avoid this error.

Here is the solution



We have provided commandName attribute for the  and



here we have provided the commandName property for the SaveDataController class.


That's it. Remember , We need to cofigure everything as part of our xml files and in case of forms we need to provide commandName and commandClass

Special Case:

For e.g i have a form without any fields and only have button (ofcourse, in general we may not face this situation , form without input fileds)


Here we have used only button within the form. In this case we don't need any command class.

As command class is used to get the data from the form and store that collected data from the form into command object and here we don't have any input fileds to collect the data entered by user. That's why we no need to provide command object details.


Thursday, May 2, 2013

Can we change the url-pattern in web.xml ..?

Yes...  

we can change the url-pattern to whatever we wish to see in the browser. Generally we may observe in most of the tutorials/books the common url patterns ..  





where in the url pattern *.htm refers to any name with an extension .htm.

If you want to change it to some other extension like .do, .action ..etc you can do that.





Suppose if we have a requirement such that we don't need any extension at the end of my web application's url. Then you can simply use ' / ' or ' /* ' instead of  ' *. extension'.



How it will work:

When we run the applicationa and send a request to server like this 

http://ip address: port number/ Application Name/ requested resource

Then the server will check for the last part of the url , the request resource 
(/requested resource) and compares with the url pattern configured in web.xml . If it matches then request will be forwarded to the corresponding controller configuration by the DispatcherServlet.



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



Monday, April 29, 2013

Different Controllers in Spring

In Spring mvc, DispatcherServlet acts as controller and it will call the methods of controller classes that we have defined.

Controller classes:

these are the classes whose methods are called by  DispatcherServlet . A controller class is the one which provides the implementation of org.springframework.web.servlet.mvc.Controller interface directly or indirectly.

In spring we have bunch of controller classes implementing org.springframework.web.servlet.mvc.Controller interface. You can go through the controller class hierarchy from the link.

The DispatcherServlet always calls handleRequest() of Controller interface when ever we send request from the browser to the controller.

Commonly used Controllers:

Some of the controllers frequently used are :

AbstractController,AbstractCommandController,AbstractFormController,  MultiActionController,SimpleFormController,AbstractWizardFormController... etc.

All of these classes are implementing Controller interface. If we want to write our own controller we can extend any one of these classes , so that our class will become a controller class.

Illustration:

If our controller class is extending AbstractController then we need to implement  handleRequestInternal() of AbstractController.



However, DispatcherServlet will call handleRequest() of Controller interface when ever we requested the controller. The handleRequest() internal code will call handleRequestInternal() of our AbstractController.

This can be represented as..




Now let us consider our class is extending AbstractCommandController, then we need to implement  handle() of AbstractCommandController.




Similar to above illustration , DispatcherServlet always calls handleRequest() of Controller which will  call handleRequestInternal() and which will further call handle() of our controller.

Suppose we have a form and we need to do some task after submitting the form then we will take the help of AbstractFormController.


showForm() method will be called to show the view(form) and if any errors exists in the validation they will be added to exception object.

Now whenever the form got submitted, DispatcherServlet will call handleRequest() which calls handleRequestInternal() which will further call processFormSubmission() of our controller.


If our form has to do multiple tasks , let's say form has more than one button, then we prefer MultiActionController. 




Here add() and delete() are the custom methods corresponding to multiple buttons (add,delete) in our form. 

If we want to display a form to the user and display a success message after reading the data from the form then we need to take the help of SimpleFormController.




We need to provide some basic information like CommandClass, Command name, Form view and Success view. Ofcourse we can set them in xml file also.

Whenever user submit's the form if no errors exists the controller calls onSubmit() of our controller class and finally it will display the success view to the user.


Now, if we want to get the information from user in multiple forms, then we need to go for AbstractWizardFormController. Incase of multiple form based applications our class should extend AbstractWizardFormController .




Here, we need to provide command class, command name and the pages are the forms to be displayed one by one in order to get multiple form application.


Which Controller is Best:

Based on our need, we can chose any of the controllers mentioned above. If we are performing a task without any form then we can use any of AbstractController,AbstractCommandController. 

If our application have forms, then we can use any of SimpleFormController,AbstractFormController, AbstractWizardFormController based on our requirement.


Here we are using the term command class, command name.

A command class is a noraml java bean with some properties relative to the corresponding form. If the form has three fields then our command class need to have three corresponding properties. command name is the name given to the command class object.

Working with these controllers will be described in later articles.