in Spring 3 MVC

Spring 3 MVC Annotations

Spring 3 MVC – Annotations

Introduction:

Spring 3 MVC framework is a very flexible web based MVC framework which is less intrusive. Spring 3 has also come up with the Annotation based configuration which reduces the amount of XML that needs to be declared in the MVC modules.

In this article we will understand what are the annotations that are available in spring framework and also some of the important objects that we will be using when working with the Spring 3 MVC.


Annotations in Spring 3 MVC:

  1. @Controller
  2. @RequestMapping
  3. @PathVariable
  4. @RequestParam
  5. @RequestHeader
  6. @ModelAttribute

@Controller:

  • This annotation is used at the class level.
  • Denotes that the marked class acts as a controller.

Eg:


import org.springframework.stereotype.Controller;

@Controller
public class StudentController {

}


@RequestMapping:

 

  • This annotation can be used at the class level and method level.
  • This annotation accepts the following as its parameters.Please note that all the arguments are optional.
  1. String[ ] values – Represents an array of URLs which tells that the controller should be invoked for the request which arrive with the list of URLs mentioned.
    Eg: @RequestMapping(value={"/welcome", "/test"}.
  2. RequestMethod[ ] methods – An array of HTTP methods. This is optional parameter and when not mentioned, the default method is HTTP GET. If specifically mentioned like POST, TRACE etc., then the method or controller will be invoked only when the request comes with the mentioned method. Eg: @RequestMapping(method=RequestMethod.GET).
  3. String[] Params – An array of Request Parameters and their values which can be used to validate if a particular request parameter has a value.
    Eg. @RequestMapping(params="myParam=myValue")
  4. String[] headers – An array of headers which can be used to validate the values that the headers carry in request object. This is used for validation purposes same as the Params[] discussed earlier.
     Eg.@RequestMapping(headers = "content-type=text/*")

Eg:

 

@PathVariable

Srinivas Reddy

Srinivas Reddy

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...

More Posts - Website

Follow Me:
TwitterFacebook

Srinivas Reddy
Written By:

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...