(转)SpringMVC学习(三)——SpringMVC的配置文件
http://blog.csdn.net/yerenyuan_pku/article/details/72231527
读者阅读过SpringMVC学习(一)——SpringMVC介绍与入门这篇文章后,想必都会写写SpringMVC的入门小程序,在这个小程序中,SpringMVC的核心配置文件——springmvc.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"
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.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.itheima.springmvc.controller"/>
</beans>
读者可能怀疑这写的不对啊!怎么可能只配这点东西呢?SpringMVC的三大组件哪去了,它们不是要配置吗?且听我慢慢讲解。我们发现这几个组件并没配置,但却是好使的,就是因为它有一个默认配置,DispatcherServlet.properties这个默认配置文件里面默认加载了,看图:
可以看出我们使用了注解方式的处理器映射器和处理器适配器。
默认加载的注解方式的处理器映射器
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
默认加载的注解方式的处理器适配器
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
默认加载的视图适配器(默认解析JSP视图的视图解析器)
org.springframework.web.servlet.view.InternalResourceViewResolver
我们如果使用默认加载的注解方式的映射器和适配器,那么对它们的可控制性是比较小的,所以一般来说,我们都是自己配置的,因为有的时候我们需要扩展一些其他的组件。
注解映射器和适配器
配置组件扫描器
使用组件扫描器可省去在Spring容器中配置每个Controller类的繁琐。使用<context:component-scan>
自动扫描标记@controller
注解的控制器类,配置如下:
<context:component-scan base-package="com.itheima.springmvc.controller"/>
注意:如果要扫描多个包,多个包中间使用半角逗号分隔。很明显在入门小程序中我已经配置了。
配置RequestMappingHandlerMapping
注解式处理器映射器,对类中标记@ResquestMapping注解的方法进行映射,根据@ResquestMapping注解定义的url匹配@ResquestMapping注解标记的方法,匹配成功返回HandlerMethod对象给前端控制器,HandlerMethod对象中封装了url对应的方法Method。
从Spring3.1版本开始,废除了DefaultAnnotationHandlerMapping的使用,推荐使用RequestMappingHandlerMapping完成注解式处理器映射。配置如下:
<!-- 配置注解式处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
推荐使用最新版本的注解式处理器映射器,如果你想对其扩展,可以在这个bean里面配置其他的属性。 @RequestMapping
注解的描述:定义请求url到处理器功能方法的映射。
配置RequestMappingHandlerAdapter
注解式处理器适配器,对标记@ResquestMapping注解的方法进行适配。
从Spring3.1版本开始,废除了AnnotationMethodHandlerAdapter的使用,推荐使用RequestMappingHandlerAdapter完成注解式处理器适配。配置如下:
<!-- 配置注解式处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
推荐使用最新版本的注解式处理器适配器,如果你想对其扩展,可以在这个bean里面配置其他的属性。
当我们配置完注解式处理器映射器和注解式处理器适配器之后,SpringMVC的核心配置文件——springmvc.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"
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.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.itheima.springmvc.controller"/>
<!-- 配置注解式处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 配置注解式处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
</beans>
然后在浏览器地址栏中输入url地址——http://localhost:8080/springmvc-first/itemList.action
,回车,也能同样看到如下效果:
继续优化注解,配置<mvc:annotation-driven>
使用注解要注意一个问题,就是注解适配器和映射器必须配对使用,也就是说,不能一个用注解,一个用非注解。要用一起用,要么都不用。其实在SpringMVC中还有更加简便的注解,SpringMVC使用<mvc:annotation-driven>
自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter,可在springmvc.xml配置文件中使用<mvc:annotation-driven>
替代注解处理器和适配器的配置,如下图所示:
注意:如果配置一个注解驱动之后,那么就可以不用配置处理器映射器和处理器适配器了。
此时在浏览器地址栏中输入url地址——http://localhost:8080/springmvc-first/itemList.action
,回车,同样也能看到如上效果。
配置视图解析器
我们也可在springmvc.xml配置文件中自己手动配置视图解析器,如下:
<!-- 配置视图解析器(对jsp默认解析的视图解析器) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- prefix:前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- suffix:后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
- InternalResourceViewResolver:支持JSP视图解析。
- viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,所以classpath中必须包含jstl的相关jar 包。此属性可以不设置,默认为JstlView。
- prefix和suffix:查找视图页面的前缀和后缀,最终视图的址为:前缀+逻辑视图名+后缀,逻辑视图名需要在Controller返回的ModelAndView中指定,比如逻辑视图名为hello,则最终返回的jsp物理视图地址就为 “WEB-INF/jsp/hello.jsp”。
这样一来,ItemController类的代码就要修改为:
@Controller
public class ItemController {
// .action可以省略 (请求的url地址)
@RequestMapping("/itemList.action")
public ModelAndView itemList() {
// 查询商品列表,使用静态数据生成一个商品列表
List<Items> itemList = new ArrayList<Items>();
itemList.add(new Items(1, "imac", 20000, new Date(), "苹果本很贵"));
itemList.add(new Items(2, "imac1", 20000, new Date(), "苹果本很贵"));
itemList.add(new Items(3, "imac2", 20000, new Date(), "苹果本很贵"));
itemList.add(new Items(4, "imac3", 20000, new Date(), "苹果本很贵"));
itemList.add(new Items(5, "imac4", 20000, new Date(), "卧槽,苹果本很贵啦!"));
// 把商品列表传递给jsp
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", itemList);
// 配置完视图解析器之后只需要返回返回jsp的名称即可
modelAndView.setViewName("itemList");
// 返回结果
return modelAndView;
}
}
如果这时返回全路径,即/WEB-INF/jsp/itemList.jsp,那就不好使了。
到这就基本总结完了SpringMVC中使用注解方式的适配器和映射器了,很明显,开发中我们就使用注解配置,那样非常方便。
(转)SpringMVC学习(三)——SpringMVC的配置文件的更多相关文章
- (转)SpringMVC学习(一)——SpringMVC介绍与入门
http://blog.csdn.net/yerenyuan_pku/article/details/72231272 SpringMVC介绍 SpringMVC是什么? SpringMVC和Stru ...
- (转)SpringMVC学习(六)——SpringMVC高级参数绑定与@RequestMapping注解
http://blog.csdn.net/yerenyuan_pku/article/details/72511749 高级参数绑定 现在进入SpringMVC高级参数绑定的学习,本文所有案例代码的编 ...
- springMVC学习(3)-springMVC和mybatis整合
一.需求:使用springmvc和mybatis完成商品列表查询. 二.整合思路:springMVC+mybaits的系统架构: 1步):整合dao层 mybatis和spring整合,通过sprin ...
- (转)SpringMVC学习(五)——SpringMVC的参数绑定
http://blog.csdn.net/yerenyuan_pku/article/details/72511611 SpringMVC中的参数绑定还是蛮重要的,所以单独开一篇文章来讲解.本文所有案 ...
- springMVC学习三 注解开发环境搭建
第一步:导入jar包 第二步:配置DispatcherServlet 前端控制器 因为此处把DsipatcherServlet的映射路径配置成了"/",代表除了.jsp文件之外, ...
- SpringMVC学习三
实现有点用处的增删改查,并利用了AJAX(javascript)动态修改,还有json的返回读取,以及文件上传和下载. 配置基础Employee类以及Dao类 package com.springmv ...
- springMVC学习(7)-springMVC校验
一.校验理解: 对于安全要求较高点建议在服务端进行校验. 控制层conroller:校验页面请求的参数的合法性.在服务端控制层conroller校验,不区分客户端类型(浏览器.手机客户端.远程调用) ...
- (转)SpringMVC学习(二)——SpringMVC架构及组件
http://blog.csdn.net/yerenyuan_pku/article/details/72231385 相信大家通过前文的学习,已经对SpringMVC这个框架多少有些理解了.还记得上 ...
- SpringMVC学习(二)——SpringMVC架构及组件(及其运行原理)-转载
相信大家通过前文的学习,已经对SpringMVC这个框架多少有些理解了.还记得上一篇文章中SpringMVC的处理流程吗? 这个图大致描述了SpringMVC的整个处理流程,这个流程图还是相对来说比 ...
随机推荐
- ASP.NET页面执行顺序
原文地址:http://blog.csdn.net/a497785609/article/details/4510335 1.对象初始化(OnInit方法) 页面中的控件(包括页面本身)都是在它们最初 ...
- lua面向对象实现(实例化对象、继承、多态、多继承、单例模式)
lua面向对象实现: 一个类就像是一个创建对象的模具.有些面向对象语言提供了类的概念,在这些语言中每个对象都是某个特定类的实例.lua则没有类的概念,每个对象只能自定义行为和形态.不过,要在lua中模 ...
- OSPF-1-OSPF的数据库交换(2)
2.Hello过程: (1)在同一子网中发现其他运行OSPF的路由器 所有启用了OSPF的接口,都会监听发往224.0.0.5的组播Hello消息,这是表示所有OSPF路由器的组播地址.Hello包使 ...
- saltstack_API接口
1 介绍 saltstack部署完后,要和master交互,就需要使用/usr/bin/salt这个工具.but..我不想手动到salt服务器上去执行这个命令,而是让web工具来调用salt. sal ...
- nginx,tomcat,apache三者分别用来做什么,有何区别
1. Nginx和tomcat的区别 nginx常用做静态内容服务和代理服务器,直接外来请求转发给后面的应用服务器(tomcat,Django等),tomcat更多用来做一个应用容器,让java we ...
- dzzoffice 任意文件下载漏洞分析
dzzoffice 任意文件下载 \updload\dzz\system\save.php第72行开始: elseif($_GET['do']=='move'){ $obz=trim($_ ...
- Codeforces Round #542(Div. 2) B.Two Cakes
链接:https://codeforces.com/contest/1130/problem/B 题意: 给定n和 2 * n个数,表示i位置卖ai层蛋糕, 有两个人在1号,必须严格按照1-n的顺序买 ...
- Codeforces Round #402 (Div. 2) A
Description In Berland each high school student is characterized by academic performance — integer v ...
- 使用 MiniProfiler 来分析 ASP.NET Core 应用
MiniProfiler(https://miniprofiler.com/)是一个轻量级且简单易用的分析工具库,它可以用来分析ASP.NET Core应用. 优点 针对ASP.NET Core MV ...
- linux下输出json字符串,用python格式化
echo '{"name":"chen","age":"11"}' |python -m json.tool 如果是文件 ...