SpringMvc的两种基于注解的映射器与适配器配置:
1.通过显式的配置映射器与适配器,并通过自动扫描标签去加载Controller类。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  xmlns:p="http://www.springframework.org/schema/p"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  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-4.2.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.2.xsd  http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"><!-- spring可以自动去烧苗base-package下的包或者子包下的Java文件,如果扫描到有Spring的相关注解的类则会把这些类注册为Spring的bean --><context:component-scan base-package="com.xiaohui.controller" />        <!-- 配置注解类型的处理映射器 --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /><!--配置annotation类型的处理器适配器  --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/><!-- 配置视图渲染器 --><bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"/></beans>

通过component-scan 标签可以扫描加有Controller、Service等注解的类进行创建。
2.通过context:annotation-config 标签配置
通过context:annotation-config 标签配置后则可以省略1中的两条显式的配置映射器与适配器。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  xmlns:p="http://www.springframework.org/schema/p"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  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-4.2.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.2.xsd  http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"><!-- spring可以自动去烧苗base-package下的包或者子包下的Java文件,如果扫描到有Spring的相关注解的类则会把这些类注册为Spring的bean --><context:component-scan base-package="com.xiaohui.controller" />        <!-- context:annotation-config 配置该标签后 则不用配置上面的注解映射器和注解是适配器  --><context:annotation-config/><!-- 配置视图渲染器 --><bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"/></beans>

3.基于注解的Controller配置

package com.xiaohui.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;@Controller
public class HelloController {@RequestMapping("/hello")public ModelAndView hello(){ModelAndView modelAndView = new ModelAndView();//添加数据模型modelAndView.addObject("msg", "hello springmvc");//设置逻辑试图名,视图解析器会根据改名字解析到具体的视图页面modelAndView.setViewName("/WEB-INF/jsp/hello.jsp");return modelAndView;}}

controller类需要添加@Controller注解以被Spring配置component-scan标签中扫描加载,@RequestMapping中默认的字符串对应映射的请求地址。

SpringMvc 04 基于注解的映射器与适配器配置相关推荐

  1. 【SpringMVC框架】注解的处理器映射器和适配器配置

    下面我们来探讨注解的处理器映射器和适配器 1.注解的处理器映射器和适配器 在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.De ...

  2. SpringMVC 配置注解的映射器、适配器(重点)

    开发中实际用到的方式,是配置注解的映射器适配器. >配置 mvc:annotation-driven元素 在3.1之后的注解的处理器映射器.适配器添加了很多的优化以及参数设置,所以在开发时,推荐 ...

  3. 非注解和注解的处理器映射器和适配器---SpringMVC学习笔记(三)

    非注解的处理器映射器和适配器 非注解的处理器映射器 之前的入门Demo中使用的就是非注解的处理器映射器: org.springframework.web.servlet.handler.BeanNam ...

  4. 【SpringMVC框架】非注解的处理器映射器和适配器

    非注解的处理器映射器和适配器 1.非注解的处理器映射器 之前的处理器映射器: org.springframework.web.servlet.handler.BeanNameUrlHandlerMap ...

  5. SpringMvc 03 非注解形式下的映射器与适配器

        1,映射器 1.1 ControllerBean +BeanNameUrlHandlerMapping              其中ControllerBean 需要指明name(url)通 ...

  6. SpringMVC学习记录二——非注解和注解的处理器映射器和适配器

    3      非注解的处理器映射器和适配器 3.1      非注解的处理器映射器 处理器映射器: org.springframework.web.servlet.handler.BeanNameUr ...

  7. SpringMVC - 非注解的处理器映射器和适配器

    为什么80%的码农都做不了架构师?>>>    一.非注解的处理器映射器 提供的处理器有两个属性.一个是id属性,一个是name属性.分别对应两种不同的映射器. <bean i ...

  8. 管理springmvc组件——前端控制器、控制器映射器和适配器、视图解析器、文件上传的、拦截器||消息转化

    管理springmvc组件 概述 在使用springmvc时要配置哪些东西 前端控制器 控制器映射器和适配器 映射器  Map<Set<String>,Object> Set& ...

  9. Spring映射器、适配器、解析器

    1 springmvc的映射器和适配器 1.1springmvc的映射器 根据客户端请求的url,找到处理本次请求的handler(处理器),将url和controller关联起来 1.2spring ...

最新文章

  1. 训练作用_我们口才训练微信群有哪些重要作用?
  2. OpenStack(Kilo版本)镜像服务glance的安装部署
  3. iOS:app直播---采集篇
  4. 大富翁已成过去-我的一些感想
  5. boost::regex模块基于 ftp 的 regex_match 示例
  6. 计算机网络8832,2021年4月份自学考试计算机网络原理04741答案.doc
  7. python中类的定义和使用_Python中类的定义与使用
  8. 第一讲:网络协议概述
  9. 系统辨识的最小二乘法原理及其算法实现
  10. 51单片机指令汇集,分类,以及典型指令案例分析
  11. SWing组件说明及使用
  12. 禁用右键 回车 ESC 和 ALT+F4组合建
  13. 009 极限的四则运算定理(加、减、乘、除)
  14. java-从菜鸟到大神
  15. 射灯安装方法图解_射灯怎么安装?射灯安装离墙距离多少合适?射灯安装图解介绍!...
  16. Idea编译出现[ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter这个问题
  17. 阿里云添加云盘并挂载使用步骤
  18. 怒!移动免费上网卡是骗人的!还是黑我的手机费!
  19. JAAS配置介绍(转载)
  20. html怎么把元素垂直居中显示,分享html css元素垂直居中的几种方法

热门文章

  1. idea插件sonar安装使用教程
  2. Debug解决问题方法论
  3. 十分钟轻松搞懂CSS的五大定位方式!(建议收藏)
  4. 短代码的java打地鼠_使用JavaScript实现网页版Pongo设计思路及源代码分享
  5. using filesort和using temporary
  6. draggable禁止拖动_通过 JS 实现简单的拖拽功能并且可以在特定元素上禁止拖拽...
  7. python pptx怎么复制ppt_python pptx复制ppt中的某一页并且放在这一页之后
  8. vue router 路由鉴权(非动态路由)
  9. [译] 使用 python 分析 14 亿条数据
  10. Cocos2d-x的Android.mk自动生成