我已经开始学习 Spring MVC 阅读本教程:http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/ http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/
好吧,这对我来说很清楚。
在这个例子中我有web.xml文件来配置我的网络应用程序:
<?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>
And the 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>
而且,正如您在上一个链接中看到的,我只有一个控制器类来处理针对“/hello”URL 的 HTTP 请求...好吧...这对我来说很清楚...
在此示例旁边,我通过 STS\Eclipse 中的相关模板项目创建了一个新的 Spring MVC 项目。
此示例与上一个示例非常相似:我始终使用 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 beans 依赖项,而实际上不必在 xml 中指定一堆元素或实现接口或扩展基类。当 spring 启动时,它会读取它的 xml 配置文件并查找 Foo 是否用 @Controller 进行标记,它知道该类是一个控制器并将其视为控制器。默认情况下,spring 假定它应该管理的所有类都在 beans.xml 文件中显式定义。然后是元件扫描标签告诉 Spring 它应该在类路径中搜索 com.mycompany.maventestweapp 下的所有类,并查看每个类以查看它是否有 @Controller、@Repository、@Service 或 @Component,以及是否有然后 Spring 将向 bean 工厂注册该类,就像您在 xml 配置文件中键入一样。布拉布拉布拉 等等 布拉布拉
好吧,这对我来说绝对清楚!我认为我可以毫无问题地理解为什么我有注释驱动我的第二个示例的 DispatcherServlet 配置文件中的标记,这一点很清楚。
问题是要理解为什么我的第一个示例的 DispatcherServlet 配置文件中没有此标记以及为什么它运行良好如果我尝试从 servlet-context.xml 文件中删除此标记(在我的第二个示例中),则该标记不会运行并出现错误,提示我:HTTP 状态 404 -在堆栈跟踪中我有:警告:org.springframework.web.servlet.PageNotFound - 在名为“appServlet”的 DispatcherServlet 中未找到带有 URI [/maventestwebapp/] 的 HTTP 请求的映射
我认为这是合理的行为,因为 Spring 没有启用注释支持,因此没有注册我的控制器类(使用注释驱动并使用 @Controller 注释我的控制器类,如果我在 bean.xml 中显式定义了此类)文件),所以不能使用这个 bean 的方法来处理 HTTP 请求...好吧...这听起来不错...
但是为什么在第一个示例中没有注释驱动标签就可以工作呢?我没有注释驱动的标签(所以我不允许使用注释来声明什么类是 Spring bean,例如控制器)并且我没有在 beans.xml 文件中显式声明此类...所以不应该当我删除注释驱动标签时,第二个示例不起作用...但它起作用:-/
我要疯狂地理解为什么......
拜托,你能帮我吗?
太感谢了
Andrea