SpringMvc使用01

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chuan129/article/details/79938524

SpringMvc是spring公司生产的一个表现层框架, 它是spring框架的一个web层组件, 可以很好的和spring集成。
作用:
第一:从请求中接收参数
第二:将处理好的数据返回给页面
执行流程
这里写图片描述
1、用户发送请求至前端控制器DispatcherServlet
2、前端控制器收到请求调用处理器映射器HandlerMapping。
3、处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet。
4、DispatcherServlet通过HandlerAdapter处理器适配器调用处理器Controller
6、Controller执行完成返回ModelAndView
7、HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet
8、DispatcherServlet将ModelAndView传给ViewReslover视图解析器
9、ViewReslover解析后返回具体View
10、DispatcherServlet对View进行渲染视图(即将模型数据填充至视图中)。
11、DispatcherServlet响应用户。
SpringMvc使用
1、导包
2、创建 springmvc.xml
这里写图片描述
这里写图片描述
3、web.xml中添加DispatcherServlet的配置
这里写图片描述
4、创建一个普通java类
这里写图片描述
5、配置注解驱动 SpringMvc.xml
这里写图片描述

注解扫描:扫描包下有@Controller的类;
注解驱动:自动配置最新的适配器和映射器;

6、配置视图解析器 SpringMvc.xml
这里写图片描述
这样,ItemController可以改为
这里写图片描述

处理器映射器、处理器适配器、视图解析器称为springmvc的三大组件

参数绑定
1、默认支持的类型
这里写图片描述
2、基本类型
这里写图片描述
这里写图片描述
3、pojo类型
这里写图片描述
这里写图片描述
4、pojo的包装类型
这里写图片描述
这里写图片描述
这里写图片描述
5、自定义转换器:
作用:将一些spirngMvc不能转换的参数类型,手动转换,比如string转date
这里写图片描述
这里写图片描述
这样提交会报错;
第一步:编写类实现converter接口
这里写图片描述
第二步:在springMvc.xml中配置转换器
这里写图片描述
第三步:在spirngMvc.xml中将配置好的转换器挂载到注解驱动上
这里写图片描述

SSM整合:
(1) dao层
ApplicationContext-dao.xml(数据源, 连接池, 会话工厂, Mapper接口扫描)
SqlMapConfig.xml (mybatis核心配置文件)
使用逆向工程生成 pojo, mapper接口和映射文件
(2) service层
ApplicationContext-tran.xml事务
ApplicaitonContext-service.xml(配置@Service注解扫描)
(3) controller层
SpringMvc.xml(@Controller注解扫描, 注解驱动, 视图解析器)
(4) web.xml
spring监听器(加载所有ApplicationContext-*.xml)
springMvc前端控制器(加载SpringMvc.xml)
这里写图片描述
配置web.xml
这里写图片描述
这里写图片描述
配置 ApplicationContext-dao.xml:
这里写图片描述
这里写图片描述
配置 ApplicationContext-service.xml:
这里写图片描述
配置 ApplicationContext-trans(事务)
这里写图片描述
这里写图片描述
配置 :SpringMvc.xml
这里写图片描述
这里写图片描述
配置 SqlMapConfig.xml
这里写图片描述
post乱码
这里写图片描述
get乱码
这里写图片描述

猜你喜欢

转载自blog.csdn.net/chuan129/article/details/79938524