Anotation Basic Use 3

Annotation-based controller configuration

Spring
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.

 

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

 

To
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).

 

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

<beans
xmlns="http://www.springframework.org/schema/beans&quot;

      
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot;

      
xmlns:context="http://www.springframework.org/schema/context&quot;

      
xmlns:util="http://www.springframework.org/schema/util&quot;

       xsi:schemaLocation="

                                http://www.springframework.org/schema/beans                                                                                         

                                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

                                http://www.springframework.org/schema/context

                                http://www.springframework.org/schema/context/spring-context-3.0.xsd

                                http://www.springframework.org/schema/util
                                                                                                             

                                http://www.springframework.org/schema/util/spring-util-3.0.xsd"&gt;

 

    <context:component-scan
base-package="com.hongliang"/>

    ……

</beans>              

 

 

When
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.

 

    <bean
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

        <property
name="mappings">

            <props>

                <prop
key="/dataPage.htm">dataPageController</prop>

            </props>

        </property>

</bean>

 

Hence, for the corresponding controller should be defined
like this

@Controller

public class DataPageController {

           

}

 

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

    private SidDao sidDao;

 

    @Autowired

    public void setSidDao(SidDao sidDao) {

        this.sidDao = sidDao;

}

 

Let’s
start with a form submission and compare it with the SimpleFormController.

 

   
@RequestMapping(method = RequestMethod.GET, params = {"type=bookPage",
"!isSubmit"})                       

       
public String setupForm BookPage ((ModelMap model,                                                                                

                    @RequestParam("bookingType")String bookingType)
{

       
//similar to formBackingObject() method in SimpleFormController

       
BookingForm bookingForm = new BookingForm ();

       
bookingForm.setBookingType();

       
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 ";

   
}

 

The
@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
.

 

You
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).

 

In
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.

 

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

bookPage
", "isSubmit=true"})

public
ModelAndView processSubmit
BookPage (

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

    ModelAndView modelAndView = new ModelAndView("bookSuccessPage ");

    String message = sidDao.bookTicket(bookingForm);

    modelAndView.addObject("message ",
message);

    return modelAndView;

}

 

In
the corresponding jsp page you should have something like this

<form:form
commandName="
bookingForm

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

    …………..

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

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

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

</form:form>

The
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.  

Advertisements
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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s