Welcome to the world of Spring MVC. We are going to learn some very basic Spring MVC terms. You can see the final output of this program on the image left. We will learn these terms. (1)What are configuration Files? (2)Why we use @Controller & @RequestMapping Annotations? (3)What are Models? (4)What are ViewResolver? (5)How we send data from Controller to view(jsp) (6)What is new in web.xml file and many other things?
In this tutorial, we will learn about some commons tags of spring MVC. Some of common tags are given below:- (1)<s:form /> (2)<s:input /> (3)<s:checkbox /> (4)<s:checkboxes /> (5)<s:radiobutton /> (6)<s:hidden /> (7)<s:select /> we will learn all these tags working In detail, as how to bind model’s properties in these or how to retrieve those values from server side etc. You will also learn working of @ModelAttribute annotations. Lets start...
Till now, we have learnt that when we type http://http://localhost:8080/myurl,then control will move to the controller’s method which is mapped with this url. Now have a look at this url: http://http://localhost:8080/myurl/ProgramSji.Here Programsji is a pathvariable. You can get this value in any variable. And use that value in your code. Let’s start with this program…..
In this program, we have two dropdowns. In first dropdown, we have a list of countries,when we change first dropdown’s value, then it will send an ajax request and retrieve states from server side according to country selection. Here you will learn some new things, like, because we are sending here country name In json format (in ajax command),and receiving response in json format, so for json support we have to add “org.codehaus.jackson” dependency in pom.xml file, so please check pom.xml file also. And have to create a bean of type MappingJacksonHttpMessageConverter.So that whatever json data is coming to action method, that can be converted into requestparam. And whatever data we are sending from server to client, it can be converted into json. so please check configuration file also We will also learn @RequestParam, and @ResponseBody annotations. Let’s start the program….
Converters are used to Convert http messages into some usable form, for example,to convert json message into a bean (or bean’s properties into json), we use MappingJacksonHttpMessageConverter.
we can also create our own converters as per our specific need.
In this program, we are going to create a class Person, then we’ll send “Bill;Jobs;55;1000” from client side to server, Using our converter, these values will automatically create an object of Person class and put these values in person’s fname, lname,age and salary properties. and send that to action method. Lets start….
Here, we will learn some new things, like to create our own conveter, we have to implement Converter interface. and then we have to register this converter in configuration file. So please have a look at that file too.
In this example, we are going to implement HandlerInterceptor, So whenever Program will get some request from client side and if that request url is configured with
Interceptor, then these methods will be executed in a sequence
(2)Action method, that is mapped with url
(4)Then it will render view (jsp)
(5) At the end, Interceptor’s afterCompletion will be executed.
Any of the above interceptor we will use here in this program, that must be configured into configuration file. So please have a look also in configuration file.
Suppose we fill a form online, to validate all values on the form ,we use some kind of validation on server side. Here in spring, we can use validation annotations on bean’s property. For example, if we need person bean’s name field’s length must be between 5 to 20, we can use this annotation on name property @Length(min=5 ,max=20,message=”please check length”) Spring will only read these validations annotations, when we use @valid annotation with @ModelAttribute on post method. Whenever we have to add this kind of validation , we need to add dependency for these annotations. So please have a look at pom.xml for those dependencies. Let’s have a look at this program to see @valid annotation and other validation annotations working.
In our application, if we have multiple pages those share a common header/footer,then it is a good idea to use tiles. Tiles offers us functionality to use same header/footer
for multiple pages.
When we need to use tiles, in our application, (1)we have to include its dependencies into pom.xml (2) have to create beans for class TilesConfigurer and TilesViewResolver. (3)one new file will be created tiles.xml.
Lets see how spring helps us to integrate tiles with it.
It is very common in application to add functionality to upload a file. Here we will learn how we can upload a file using Spring MVC framework and how to save that
file on server.
If we need to add functionality to upload a file, we need to register a Class CommonsMultipartResolverin configuration file.
Let’s start with this program…
If your application is multilingual, you may need to add functionality to change all messages or labels according to locale. For that you have to create different properties files, for each locale, where you can put your messages. For example, if you want to add functionality of three locales(English, Chinese, Italy), we have to add three files. Messages_en.properties,Messages_ch.properties,Messages_it.properties Here Messages is just a name, that can be changed. Lets have a look at the program, then only we can understand