Anotation Basic Use 3

Annotation-based controller configuration

2.5 introduces further stereotype annotations: @Component, @Service and @Controller.
@Component serves as a generic stereotype for any Spring-managed component;
whereas, @Repository, @Service, and @Controller serve as specializations of @Component
for more specific use cases (e.g., in the persistence, service, and
presentation layers, respectively.


provides the capability of automatically detecting ‘stereotyped’ classes and
registering corresponding BeanDefinitions with the ApplicationContext.


autodetect these classes and register the corresponding beans requires the
inclusion of the following element in XML where ‘basePackage’ would be a common
parent package for the two classes (or alternatively a comma-separated list
could be specified that included the parent package of each class).


version="1.0" encoding="UTF-8"?>


















a component is autodetected as part of the scanning process, its bean name will
be generated by the BeanNameGenerator strategy known to that scanner. By
default, any Spring ‘stereotype’ annotation (@Component, @Repository, @Service,
and @Controller) that contains a name value will thereby provide that name to
the corresponding bean definition. If such an annotation contains no name value
or for any other detected component (such as those discovered due to custom
filters), the default bean name generator will return the uncapitalized
non-qualified class name.










Hence, for the corresponding controller should be defined
like this


public class DataPageController {




following code is an example of how to inject the DAO.

    private SidDao sidDao;



    public void setSidDao(SidDao sidDao) {

        this.sidDao = sidDao;



start with a form submission and compare it with the SimpleFormController.


@RequestMapping(method = RequestMethod.GET, params = {"type=bookPage",

public String setupForm BookPage ((ModelMap model,                                                                                

                    @RequestParam("bookingType")String bookingType)

//similar to formBackingObject() method in SimpleFormController

BookingForm bookingForm = new BookingForm ();


model.put("bookingForm ", bookingForm);


//Similar to referenceData method to provide the data that will
preloaded in the form             

List<Date> availableDatesList = sidDao.get AvailableDates(bookingType);

model.put("availableDatesList ", availableDatesList);

//Redirect back. Just for demonstration purpose, not recommend

return "dataPages/ bookPage ";



@RequestMapping annotation is used to map URLs onto an entire class or a
particular handler method. Typically the type-level annotation maps a specific
request path (or path pattern) onto a form controller, with additional
method-level annotations ‘narrowing’ the primary mapping for a specific HTTP
method request method ("GET"/"POST") or specific HTTP
request parameters. The example
@RequestMapping(method = RequestMethod.GET, params =
{"type=bookPage", "!isSubmit"})  
which indicates that
corresponding url should have two parameters “type” and “isSubmit” and value
for type is “bookPage” and value for isSubmit should not be true


can customise the signature for the handler method whatever you like, but in
convention, setupForm() to initialise the form and  processSubmit() for form submission. The
parameters that you can provide includes ModelMap , @RequestParam, HttpServletRequest
and etc (check the doc for annotation org.springframework.web.bind.annotation).


the procssSubmit() method you might want to include a @ModelAttribute parameter,
which binds a method parameter or method return value to a named model
attribute, exposed to a web view.


= RequestMethod.POST,
params = {"type=

", "isSubmit=true"})

ModelAndView processSubmit
BookPage (

                @ModelAttribute("bookingForm") BookingForm
bookingForm) throws Exception {

    ModelAndView modelAndView = new ModelAndView("bookSuccessPage ");

    String message = sidDao.bookTicket(bookingForm);

    modelAndView.addObject("message ",

    return modelAndView;



the corresponding jsp page you should have something like this


" action=”/dataPage.htm" method="POST">


<input type="submit"
value="Submit" >

 <input type="hidden" value="
" name="type">

    <input type="hidden"
value="true" name="isSubmit">


value for commandName should be identical to the argument used in @ModelAttribute
earlier. It is recommended to specify the method for form submission, which
should also be identical to the one declared in the @RequestMapping. This would
be very useful to identify the problem, if your successful page includes a page
that would trigger another controller which applying different request method.
Also be aware that you need to submit the associated parameters in your form.  

This entry was posted in Spring. Bookmark the permalink.

2 Responses to Anotation Basic Use 3

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s