spring mvc 基于注解的使用,相当于配置文件的使用简单的多.下面讲一下spring mvc 注解的使用
先首确保已经把spring mvc的环境搭配好.这里可以看我的前一篇文章Spring mvc系列一之 Spring mvc简单配置.
先看一下再未使用注解前,spring mvc的配置文件
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="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"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- 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
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <propertyname="prefix"value="/"/>
- <propertyname="suffix"value=".jsp"/>
- </bean>
- <!--声明一个Controller中使用多个方法-->
- <beanid="parameterMethodNameResolver"class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
- <!--传参数时用这个作为名称-->
- <propertyname="paramName"value="action"></property>
- </bean>
- <!--声明DispatcherServlet不要拦截下面声明的目录-->
- <mvc:resourceslocation="/images/"mapping="/images/**"/>
- </beans>
上面我们声明在一个控制器中使用多个方法.
再看看spring mvc使用注解配置文件的配置:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="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"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- 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
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <!--开启注解扫描功能-->
- <context:component-scanbase-package="gd.hz.springmvc.controller"></context:component-scan>
- <!--开启注解DefaultAnnotationHandlerMapping:映射相应的类,DefaultAnnotationHandlerMapping相应的类方法-->
- <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
- <beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <propertyname="prefix"value="/"/>
- <propertyname="suffix"value=".jsp"/>
- </bean>
- </beans>
上面我们开启了注解扫描,注入了AnnotationMethodHandlerAdapter作用是对有RequestMapping注解的控制器进行HTTP路径、HTTP方法和请求参数解析.DefaultAnnotationHandlerMapping作用是映射处理程序方法级别的HTTP路径.在spring 3.1之后由RequestMappingHandlerAdapter和RequestMappingHandlerMapping代替.
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="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"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- 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
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <!--开启注解扫描功能-->
- <context:component-scanbase-package="gd.hz.springmvc.controller"></context:component-scan>
- <!--开启注解DefaultAnnotationHandlerMapping:映射相应的类,DefaultAnnotationHandlerMapping相应的类方法-->
- <!--
- <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
- <beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
- -->
- <!--spring3.1之后由RequestMappingHandlerAdapter和RequestMappingHandlerMapping代替-->
- <beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
- <beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <propertyname="prefix"value="/"/>
- <propertyname="suffix"value=".jsp"/>
- </bean>
- <!--声明DispatcherServlet不要拦截下面声明的目录-->
- <mvc:resourceslocation="/images/"mapping="/images/**"/>
- </beans>
上面的两个注解也可以用mvc标签表示,spring 3.1之后默认注入的是RequestMappingHandlerAdapter和RequestMappingHandlerMapping:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="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"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- 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
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <!--开启注解扫描功能-->
- <context:component-scanbase-package="gd.hz.springmvc.controller"></context:component-scan>
- <mvc:annotation-driven/>
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <propertyname="prefix"value="/"/>
- <propertyname="suffix"value=".jsp"/>
- </bean>
- </beans>
接下来新建一个控制器:
- packagegd.hz.springmvc.controller;
- importorg.springframework.stereotype.Controller;
- importorg.springframework.web.bind.annotation.RequestMapping;
- importorg.springframework.web.servlet.ModelAndView;
- @Controller("userController")
- publicclassUserController{
- @RequestMapping("addUser")
- publicModelAndViewaddUser()
- {
- returnnewModelAndView("hello");
- }
- }
注解@Controller标志该类为控制器.注解@RequestMapping,映射相应的路径.
我们可以这样访问:http://localhost/Springmvc/addUser ,其中addUser 就是@RequestMapping映射的名称,名字随意.下一章我们将对spring mvc的注解进行介绍.