Announcement Announcement Module
Collapse
No announcement yet.
[Spring MVC] Why in this example I have to use annotation-driven tag? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Spring MVC] Why in this example I have to use annotation-driven tag?

    Some days ago I begin to study this Spring Hello World Tutorial: http://viralpatel.net/blogs/spring-3...-spring-3-mvc/

    In this tutorial Spring DispatcherServlet is configured using the spring-servlet.xml file, this one:

    [code=xml]
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...ring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="net.viralpatel.spring3.controller" />

    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBas edViewResolver">
    <property name="viewClass"
    value="org.springframework.web.servlet.view.JstlVi ew" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    </bean>
    [/code]

    In this file I am using the context:component-scan tag to say that Spring have to scan my file serching the annotation, so for exemple, when in the controller class find that a method is annoted by @RequestMapping("/hello") annotation know that this method handle the HTTP Request toward the URL ending with "/hello". This is simple...

    Now my doubt is related to the Spring MVC template project that I could automatically build in STS\Eclipse

    When I create a new Spring MVC project in STS I have that my DispatcherServlet is configured by a file named servlet-context.xml that contains some configuration similar to the previus example file.

    In this file, I still have the component scan tag:

    [code=xml]<context:component-scan base-package="com.mycompany.maventestwebapp" />[/code]

    but I have also another tag (that look like have similar task), this one:

    [code=xml]<annotation-driven />[/code]

    What is the difference between these two tags? between these two tags?

    An other "strange" thing is that the previus example (that don't use the annotation-driven tag) is very very similar to the project create by STS using the Spring MVC Template project but if I delete the annotation-driven tag from its configuration file the project don't run and give me the following error: HTTP Status 404 -

    And in the stacktrace I have:

    WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/maventestwebapp/] in DispatcherServlet with name 'appServlet'

    But why? In the previus example work well without annotation-driven tag, and this controller class is very similar...infact have only one method that handles HTTP request toward "/" path

    This is the code of my controller class:
    [code=java]
    package com.mycompany.maventestwebapp;

    import java.text.DateFormat;
    import java.util.Date;
    import java.util.Locale;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMap ping;
    import org.springframework.web.bind.annotation.RequestMet hod;

    /**
    * Handles requests for the application home page.
    */
    @Controller
    public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
    * Simply selects the home view to render by returning its name.
    */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);

    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate );

    return "home";
    }
    [/code]

    Can someone help me to understand this thing?

    Thank you very much

    Andrea

  • #2
    Please use [ code][/code ] tags when posting code, that way it remains readable ...

    I suggest a read of the reference guide (or my book for more insight) which basically explains what the tags do.

    component-scan is only scanning for @Components (@Controller is a component)..

    annotation-driven (from the mvc namespace) also enables support for @RequestMapping.

    What happens when you don't have this tag depends on the spring version. The DispatcherServlet has some defaults and in earlier versions the DefaultAnnotationHandlerMapping wasn't loaded by default in older versions it is. So the behavior depends on which spring version you use.

    Also in Spring 3.1 the DefaultAnnotationHandlerMapping is replaced by the RequestMappingHandlerMapping which is more powerful/flexible. However for backwards compatibility with Spring 3.0 the default is still to load DefaultAnnotationHandlerMapping (this probably changes after Spring 3.2 which deprecatd the DefaultAnnotationHandlerMapping )

    Comment

    Working...
    X