草庐IT

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

coder 2024-04-01 原文

我已经开始学习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>

并使用 spring-servlet.xml 文件来配置mu DispatcherServlet:
<?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>

而且,正如您在上一个链接中看到的那样,我只有一个 Controller 类来处理朝向“/hello” URL的HTTP请求...好吧...这对我来说很清楚...

在此示例旁边,我通过STS\Eclipse中的相​​关模板项目创建了一个新的Spring MVC项目。

这个示例与上一个示例非常相似:我始终拥有web.xml文件来保护我的Web应用程序,一个用于配置DispatcherServlet的文件以及一个用于处理HTTP请求的 Controller 类。

但是有一些我无法理解的差异。

这是我的 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中指定一堆元素或实现接口(interface)或扩展基类。当spring启动时,它读取其xml配置文件并查找,并且Foo被@Controller标记,它知道该类是一个 Controller ,因此将其视为此类。默认情况下,spring假设它应该管理的所有类都在beans.xml文件中明确定义。然后是组件扫描标记,该标记告诉Spring它应在com.mycompany.maventestweapp下的所有类的类路径中搜索,并查看每个类以查看其是否具有@ Controller,@ Repository或@。 Service或@Component,如果这样做,那么Spring会将类注册到Bean工厂,就像您在xml配置文件中键入的一样。 BLABLABLA ETCETCETC BLABLA

好吧,这对我来说是绝对清楚的!我认为我没有问题要理解为什么我在第二个示例的DispatcherServler配置文件中有注释驱动的标记,这很清楚。

问题是要了解,为什么我在第一个示例的DispatcherServlet配置文件中没有此标签,以及为什么它工作良好如果我尝试从servlet-context.xml文件(在第二个示例中)删除此标签,则不要不会运行并出错,告诉我: HTTP状态404-,在堆栈跟踪中,我有: WARN:org.springframework.web.servlet.PageNotFound-找不到使用URI [/maventestwebapp/]的HTTP请求的映射在DispatcherServlet中,名称为“appServlet”

我认为这是合理的行为,因为Spring尚未启用注释支持,因此没有注册我的 Controller 类(如果我在bean.xml中明确定义了该类,如何使用@Controller注释并使用@Controller注释我的 Controller 类。文件),因此无法使用此bean的方法来处理HTTP请求...好吧...听起来不错...

但是,为什么在第一个示例中没有注释驱动的标签就可以工作呢?
我没有注释驱动的标签(因此,我没有启用注释来声明什么是Spring bean,例如 Controller ),也没有在bean.xml文件中明确声明该类……因此不应当我删除注释驱动的标签时,第二个示例无法正常工作...但是它有效:-/

我快要明白为什么...

拜托,你能帮我吗?

非常感谢

安德里亚

最佳答案

<mvc:annotation-driven>是一个帮助程序标记,可提供其他服务:

  • 注册默认处理程序映射
  • 消息转换(例如,将 Controller 中返回的对象转换为JSON)
  • JSR 303验证(@有效注释)
  • 转换服务(如果指定了转换器,则 Controller 将能够将实体作为参数,即来自请求的文本ID将通过MVC层转换为实际的业务对象。其他对象(如Dates,Enums)也是如此等)
  • 也许在Spring的新版本中会添加更多功能。

    它是通过org.springframework.web.servlet.config包中的AnnotationDrivenBeanDefinitionParser实现的

    如果您具有适当的映射(you can add the required beans explicitly if you want),则您的简单 Controller 不需要工作。
    但是,如果您想做一些或多或少的高级工作,例如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:

    Prior to Spring 3.1, type and method-level request mappings were examined in two separate stages -- a controller was selected first by the DefaultAnnotationHandlerMapping and the actual method to invoke was narrowed down second by the AnnotationMethodHandlerAdapter.

  • 关于java - Spring MVC为什么此Hello World在没有注释驱动标签的情况下运行良好(与Spring的任何其他项目不同),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13671776/

    有关java - Spring MVC为什么此Hello World在没有注释驱动标签的情况下运行良好(与Spring的任何其他项目不同)的更多相关文章

    1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

      总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

    2. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

      类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

    3. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

      我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

    4. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

      如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

    5. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

      我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

    6. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

      我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

    7. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

      在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

    8. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

      为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

    9. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

      exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

    10. ruby - 无法运行 Rails 2.x 应用程序 - 2

      我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

    随机推荐