首页 文章

Spring MVC为什么这个Hello World在没有注释驱动标签的情况下运行良好(不像其他任何项目Spring)

提问于
浏览
6

我已经开始学习Spring MVC阅读本教程:http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/

好的,这对我来说非常清楚 .

在这个例子中,我有 web.xml 文件来配置我的Web应用程序:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee    /web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>Spring3MVC</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

用于配置mu DispatcherServlet的 spring-servlet.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:p="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/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

而且,正如你在previus链接中看到的那样,我只有一个控制器类来处理“/ hello”URL的HTTP请求......好吧......这对我来说很明显......

在这个例子旁边,我通过STS \ Eclipse中的相关模板项目创建了一个新的Spring MVC项目 .

此示例与te previus非常相似:我总是使用web.xml文件来配置我的Web应用程序,配置DispatcherServlet的文件和处理HTTP请求的控制器类 .

但是我有些不同,我无法理解 .

这是我的 web.xml 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>


<!-- Creates the Spring Container shared by all Servlets and Filters -->

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

这是 servlet-context.xml 文件(配置我的DispatcherServlet的配置文件):

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<context:component-scan base-package="com.mycompany.maventestwebapp" />
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

好的,正如您所看到的,这两个示例的配置文件存在一些差异:

在由STS \ Eclipse创建的项目中,在 servlet-context.xml 文件中,我有这些配置,这些配置在我发布的第一个示例中不存在:

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

好的...我已经阅读了很多关于此配置标记的文档:意味着您可以定义spring bean依赖项,而无需在xml中指定一堆元素或实现接口或扩展基类 . 当spring启动时,它会读取它的xml配置文件并查找并使用@Controller标记Foo,它知道该类是一个控制器并将其视为这样 . 默认情况下,spring假定它应该管理的所有类都在beans.xml文件中显式定义 . 然后有一个 component scanning 标签告诉Spring它应该在com.mycompany.maventestweapp下搜索所有类的类路径并查看每个类以查看它是否有@Controller,@ Repository或@Service,或者@Component,如果确实如此,那么Spring将使用bean工厂注册该类,就像您输入了xml配置文件一样 . BLABLABLA ETCETCETC BLABLA

好的,这对我来说绝对清楚!我认为我没有任何问题可以理解为什么我在第二个例子的DispatcherServler配置文件中有 annotation-driven 标签,这很清楚 .

问题是理解 WHY I HAVE NOT THIS TAG in the DispatcherServlet configuration file of my first example and why it work well 如果我尝试从servlet-context.xml文件中删除此标记(在我的第二个示例中),这不会运行并进入错误说我: HTTP Status 404 - 并且在堆栈跟踪中我有: WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/maventestwebapp/] in DispatcherServlet with name 'appServlet'

我认为这是理性的行为,因为Spring没有启用注释支持,所以没有注册我的控制器类(使用注释驱动和使用@Controller注释我的控制器类是如果我在bean.xml中显式定义此类的方式文件),所以不能使用这个bean的方法来处理HTTP请求......好吧......听起来不错......

But then why in the first example works without having annotation-driven tag ? 我没有注释驱动标记(所以我没有使用注释来声明什么类是一个Spring bean,比如控制器)而且我没有在beans.xml文件中显式声明这个类...所以应该我没有工作,因为当我删除注释驱动的标签时,第二个例子不起作用......但是它起作用: - /

我疯了才明白为什么......

拜托,你能帮帮我吗?

非常感谢

安德里亚

1 回答

  • 4

    <mvc:annotation-driven> 是一个提供附加服务的帮助程序标记:

    • 注册默认处理程序映射

    • 消息转换(例如,将控制器中返回的对象转换为JSON)

    • JSR 303验证(@Valid注释)

    • 转换服务(如果指定了转换器,那么控制器将能够将实体作为参数,即来自请求的文本ID将转换为具有MVC层的实际业务对象 . 对于其他对象,如日期,枚举也是如此等等 . )

    • 也许在新版本的Spring中会添加更多功能..

    它是在org.springframework.web.servlet.config包中使用AnnotationDrivenBeanDefinitionParser实现的

    如果您有适当的映射(you can add the required beans explicitly if you want),则简单控制器不需要工作 . 但是如果你想做一些或多或少的高级东西,例如JSR 303验证,用JSON实现REST服务,为MVC注册转换服务,这个标签结果证明是有用的,因为它保留了在一个地方配置这些服务 .

    这就是为什么它被包含在Spring模板项目中(模板项目是开发Spring应用程序的起点(所以你可以只是go and put @Responsebody on the values returned by the controller methods或向MVC添加一个转换器),而不仅仅是Spring学习者的一个例子) .

    另请注意the handler mapping configuration changed in Spring 3.1

    在Spring 3.1之前,在两个单独的阶段中检查类型和方法级请求映射 - 首先通过DefaultAnnotationHandlerMapping选择控制器,然后通过AnnotationMethodHandlerAdapter缩小调用的实际方法 .

相关问题