And*_*ili 6 java spring annotations spring-mvc
我已经开始学习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>
Run Code Online (Sandbox Code Playgroud)
以及用于配置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>
Run Code Online (Sandbox Code Playgroud)
而且,正如你在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>
Run Code Online (Sandbox Code Playgroud)
这是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>
Run Code Online (Sandbox Code Playgroud)
好的,正如您所看到的,这两个示例的配置文件存在一些差异:
在由STS\Eclipse创建的项目中,在servlet-context.xml文件中,我有这些配置,这些配置在我发布的第一个示例中不存在:
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
Run Code Online (Sandbox Code Playgroud)
好的...我已经阅读了很多关于此配置标记的文档:意味着您可以定义spring bean依赖项,而无需在xml中指定一堆元素或实现接口或扩展基类.当spring启动时,它会读取它的xml配置文件并查找并使用@Controller标记Foo,它知道该类是一个控制器并将其视为这样.默认情况下,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 - 在具有名称'的DispatcherServlet中找不到带有URI [/ maventestwebapp /]的HTTP请求的映射appServlet"
我认为这是理性的行为,因为Spring没有启用注释支持,所以没有注册我的控制器类(使用注释驱动和使用@Controller注释我的控制器类是如果我在bean.xml中显式定义此类的方式文件),所以不能使用这个bean的方法来处理HTTP请求......好吧......听起来不错......
但是,为什么第一个例子在没有注释驱动标签的情况下工作呢? 我没有注释驱动标记(所以我没有启用使用注释来声明什么类是一个Spring bean,如控制器),我没有在beans.xml文件中显式声明这个类...所以不应该当我删除注释驱动的标签时,第二个例子的工作不起作用......但它起作用: - /
我疯了才明白为什么......
拜托,你能帮帮我吗?
非常感谢
安德里亚
<mvc:annotation-driven>
是一个提供附加服务的辅助标签:
它是通过 org.springframework.web.servlet.config 包中的AnnotationDrivenBeanDefinitionParser实现的
如果您有适当的映射(您可以根据需要显式添加所需的 bean) ,那么您的简单控制器不需要工作。但是,如果您想做一些或多或少的高级操作,例如 JSR 303 验证、使用 JSON 实现 REST 服务、为 MVC 注册转换服务,则此标记非常有用,因为它将这些服务的配置保留在一个位置。
这就是为什么它被包含在 Spring 模板项目中(模板项目是开发 Spring 应用程序的起点(因此您可以直接添加@Responsebody
控制器方法返回的值或向 MVC 添加转换器)而不仅仅是一个示例对于春季学习者)。
另请注意,Spring 3.1 中的处理程序映射配置发生了变化:
在 Spring 3.1 之前,类型和方法级请求映射在两个单独的阶段进行检查——首先由 DefaultAnnotationHandlerMapping 选择控制器,然后由 AnnotationMethodHandlerAdapter 缩小要调用的实际方法。
归档时间: |
|
查看次数: |
4941 次 |
最近记录: |