`
a137268431
  • 浏览: 146436 次
文章分类
社区版块
存档分类
最新评论

Spring mvc系列三之 开启注解

 
阅读更多

spring mvc 基于注解的使用,相当于配置文件的使用简单的多.下面讲一下spring mvc 注解的使用

先首确保已经把spring mvc的环境搭配好.这里可以看我的前一篇文章Spring mvc系列一之 Spring mvc简单配置.

先看一下再未使用注解前,spring mvc的配置文件

Xml代码收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  11. http://www.springframework.org/schema/mvc
  12. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  13. <bean
  14. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  15. <propertyname="prefix"value="/"/>
  16. <propertyname="suffix"value=".jsp"/>
  17. </bean>
  18. <!--声明一个Controller中使用多个方法-->
  19. <beanid="parameterMethodNameResolver"class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
  20. <!--传参数时用这个作为名称-->
  21. <propertyname="paramName"value="action"></property>
  22. </bean>
  23. <!--声明DispatcherServlet不要拦截下面声明的目录-->
  24. <mvc:resourceslocation="/images/"mapping="/images/**"/>
  25. </beans>

上面我们声明在一个控制器中使用多个方法.

再看看spring mvc使用注解配置文件的配置:

Xml代码收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  11. http://www.springframework.org/schema/mvc
  12. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  13. <!--开启注解扫描功能-->
  14. <context:component-scanbase-package="gd.hz.springmvc.controller"></context:component-scan>
  15. <!--开启注解DefaultAnnotationHandlerMapping:映射相应的类,DefaultAnnotationHandlerMapping相应的类方法-->
  16. <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
  17. <beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
  18. <bean
  19. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  20. <propertyname="prefix"value="/"/>
  21. <propertyname="suffix"value=".jsp"/>
  22. </bean>
  23. </beans>

上面我们开启了注解扫描,注入了AnnotationMethodHandlerAdapter作用是对有RequestMapping注解的控制器进行HTTP路径、HTTP方法和请求参数解析.DefaultAnnotationHandlerMapping作用是映射处理程序方法级别的HTTP路径.在spring 3.1之后由RequestMappingHandlerAdapter和RequestMappingHandlerMapping代替.

Xml代码收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  11. http://www.springframework.org/schema/mvc
  12. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  13. <!--开启注解扫描功能-->
  14. <context:component-scanbase-package="gd.hz.springmvc.controller"></context:component-scan>
  15. <!--开启注解DefaultAnnotationHandlerMapping:映射相应的类,DefaultAnnotationHandlerMapping相应的类方法-->
  16. <!--
  17. <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
  18. <beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
  19. -->
  20. <!--spring3.1之后由RequestMappingHandlerAdapter和RequestMappingHandlerMapping代替-->
  21. <beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
  22. <beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
  23. <bean
  24. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  25. <propertyname="prefix"value="/"/>
  26. <propertyname="suffix"value=".jsp"/>
  27. </bean>
  28. <!--声明DispatcherServlet不要拦截下面声明的目录-->
  29. <mvc:resourceslocation="/images/"mapping="/images/**"/>
  30. </beans>

上面的两个注解也可以用mvc标签表示,spring 3.1之后默认注入的是RequestMappingHandlerAdapter和RequestMappingHandlerMapping:

Xml代码收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  11. http://www.springframework.org/schema/mvc
  12. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  13. <!--开启注解扫描功能-->
  14. <context:component-scanbase-package="gd.hz.springmvc.controller"></context:component-scan>
  15. <mvc:annotation-driven/>
  16. <bean
  17. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  18. <propertyname="prefix"value="/"/>
  19. <propertyname="suffix"value=".jsp"/>
  20. </bean>
  21. </beans>

接下来新建一个控制器:

Xml代码收藏代码
  1. packagegd.hz.springmvc.controller;
  2. importorg.springframework.stereotype.Controller;
  3. importorg.springframework.web.bind.annotation.RequestMapping;
  4. importorg.springframework.web.servlet.ModelAndView;
  5. @Controller("userController")
  6. publicclassUserController{
  7. @RequestMapping("addUser")
  8. publicModelAndViewaddUser()
  9. {
  10. returnnewModelAndView("hello");
  11. }
  12. }

注解@Controller标志该类为控制器.注解@RequestMapping,映射相应的路径.

我们可以这样访问:http://localhost/Springmvc/addUser ,其中addUser 就是@RequestMapping映射的名称,名字随意.下一章我们将对spring mvc的注解进行介绍.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics