SpringMVC框架day01.docx

上传人:b****3 文档编号:12624258 上传时间:2023-04-21 格式:DOCX 页数:38 大小:257.19KB
下载 相关 举报
SpringMVC框架day01.docx_第1页
第1页 / 共38页
SpringMVC框架day01.docx_第2页
第2页 / 共38页
SpringMVC框架day01.docx_第3页
第3页 / 共38页
SpringMVC框架day01.docx_第4页
第4页 / 共38页
SpringMVC框架day01.docx_第5页
第5页 / 共38页
点击查看更多>>
下载资源
资源描述

SpringMVC框架day01.docx

《SpringMVC框架day01.docx》由会员分享,可在线阅读,更多相关《SpringMVC框架day01.docx(38页珍藏版)》请在冰豆网上搜索。

SpringMVC框架day01.docx

SpringMVC框架day01

SpringMVC框架

Springmvc就是一个类似struts2的前端web框架。

1课程计划

第一天:

1、了解springmvc在spring框架的地位

2、学习springmvc框架的执行流程(对比struts2)

3、入门程序

a)使用配置的方式运行

b)使用注解的方式

4、springmvc框架结构

a)前端控制器

b)处理器映射器

c)处理器适配器

d)视图解析器

1)使用非注解形式的映射器和适配器

2)注解形式的映射器和适配器

5、springmvc整合mybatis

6、参数绑定

a)简单参数

b)Pojo类型参数

c)自定义参数

第二天:

1、参数绑定的高级应用

集合类型的参数绑定

i.数组

ii.List

iii.Map

2、validation后台校验

3、参数回显

4、异常处理

5、上传图片

6、Json数据交互

7、Retstful风格,的url。

Url模板映射。

8、拦截器

2了解springmvc

Springwebmvc和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出来:

3Springmvc的执行流程

3.1

响应用户

返回html

处理结果传递给jsp

Jsp

填充处理结果

返回html

找到Action处理请求

Action

处理请求

返回处理结果

接收用户请求

前端控制器

StrutsPreparedAndExecuteFilter

Struts2:

3.2Springmvc

响应给用户

生成html

处理结果转发给jsp

Jsp视图

返回处理结果

请求业务处理

处理器

Handler

接收用户请求

前端控制器

DispatcherServlet

4Springmvc入门程序

4.1需求

查询商品列表,在页面显示商品列表。

使用springmvc框架来完成。

使用静态数据来完成。

4.2开发环境

Jdk:

jdk1.7.0_72

Tomcat:

7.0以上

Eclipse:

indigo

Springmvc:

3.2.0

4.3开发步骤

第一步:

创建一个web工程

第二步:

导入jar包。

包含spring的jar包包含springmvc。

第三步:

配置前端控制器。

在Web.xml中配置DispatcherServlet。

第四步:

创建springmvc的配置文件。

Springmvc.xml

第五步:

创建一个Controller,可以实现Controller接口。

第六步:

开发jsp

第七步:

配置到spring容器中。

第八步:

启动tomcat。

4.4程序开发

4.4.1创建工程

4.4.2Controller,实现Controller接口

publicclassItemsControllerimplementsController{

@Override

publicModelAndViewhandleRequest(HttpServletRequestrequest,

HttpServletResponseresponse)throwsException{

//模拟商品列表

ListitemsList=newArrayList<>();

//商品列表

Itemsitems_1=newItems();

items_1.setName("联想笔记本");

items_1.setPrice(6000f);

items_1.setDetail("ThinkPadT430联想笔记本电脑!

");

Itemsitems_2=newItems();

items_2.setName("苹果手机");

items_2.setPrice(5000f);

items_2.setDetail("iphone6苹果手机!

");

itemsList.add(items_1);

itemsList.add(items_2);

//使用request传递结果

//request.setAttribute("itemsList",itemsList);

//创建一个ModelAndView对象

ModelAndViewmodelAndView=newModelAndView();

modelAndView.addObject("itemsList",itemsList);

//添加视图

modelAndView.setViewName("/WEB-INF/jsp/itemsList.jsp");

returnmodelAndView;

}

}

4.4.3Springmvc.xml

xmlversion="1.0"encoding="UTF-8"?

>

//www.springframework.org/schema/beans"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"xmlns:

mvc="http:

//www.springframework.org/schema/mvc"

xmlns:

context="http:

//www.springframework.org/schema/context"

xmlns:

aop="http:

//www.springframework.org/schema/aop"xmlns:

tx="http:

//www.springframework.org/schema/tx"

xsi:

schemaLocation="http:

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans-3.2.xsd

http:

//www.springframework.org/schema/mvc

http:

//www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

http:

//www.springframework.org/schema/context

http:

//www.springframework.org/schema/context/spring-context-3.2.xsd

http:

//www.springframework.org/schema/aop

http:

//www.springframework.org/schema/aop/spring-aop-3.2.xsd

http:

//www.springframework.org/schema/tx

http:

//www.springframework.org/schema/tx/spring-tx-3.2.xsd">

--使用bean的name属性配置url和handler的映射关系-->

4.4.4Controller实现HttpRequestHandler接口

publicclassItemsController2implementsHttpRequestHandler{

@Override

publicvoidhandleRequest(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

//模拟商品列表

ListitemsList=newArrayList<>();

//商品列表

Itemsitems_1=newItems();

items_1.setName("联想笔记本");

items_1.setPrice(6000f);

items_1.setDetail("ThinkPadT430联想笔记本电脑!

");

Itemsitems_2=newItems();

items_2.setName("苹果手机");

items_2.setPrice(5000f);

items_2.setDetail("iphone6苹果手机!

");

itemsList.add(items_1);

itemsList.add(items_2);

//使用request域向页面传递数据

request.setAttribute("itemsList",itemsList);

//跳转到页面

request.getRequestDispatcher("/WEB-INF/jsp/itemsList.jsp").forward(request,response);

}

}

Springmvc.xml

--实现HttpRequestHandler接口的controller-->

4.5使用注解方式开发controller

4.5.1Controller

@Controller

publicclassItemsController3{

//@RequestMapping配置url和方法之间的映射关系。

@RequestMapping("/itemsList3.action")

publicModelAndViewitemsList()throwsException{

//模拟商品列表

ListitemsList=newArrayList<>();

//商品列表

Itemsitems_1=newItems();

items_1.setName("联想笔记本");

items_1.setPrice(6000f);

items_1.setDetail("ThinkPadT430联想笔记本电脑!

");

Itemsitems_2=newItems();

items_2.setName("苹果手机");

items_2.setPrice(5000f);

items_2.setDetail("iphone6苹果手机!

");

itemsList.add(items_1);

itemsList.add(items_2);

//创建ModelAndView对象

ModelAndViewmodelAndView=newModelAndView();

modelAndView.addObject("itemsList",itemsList);

//添加视图

modelAndView.setViewName("/WEB-INF/jsp/itemsList.jsp");

returnmodelAndView;

}

}

4.5.2Springmvc.xml

--配置扫描包-->

component-scanbase-package="cn.itcast.mvc.controller"/>

4.6小结

4.6.1非注解形式

1、实现Controller接口

2、实现HttpRequestHandler接口

3、配置url和handler直接的映射关系

在springmvc.xml中配置bean的name属性。

4.6.2注解形式

1、在Controller类上添加@Controller注解

2、映射关系使用@RequestMapping在方法上配置。

5Springmvc的架构

11、响应给用户

5、执行handler

2、请求查找handler

1、接收用户请求

10、把model数据传递给jsp渲染结果

9、返回物理视图View对象

8、把逻辑视图转换成物理视图

视图解析器

ViewResolver

Jsp视图

7、返回

ModelAndView

返回给前端控制器

6、返回

ModelAndView

包含数据和视图

4、请求执行handler

处理器适配器

HandlerAdapter

执行handler

3、返回处理的执行链

HandlerExecuteChain

【HandlerInterceptor1

HandlerInterceptor2

......

Handler】

处理器映射器

HandlerMapping

根据url查找对应的handler

处理器

Handler

前端控制器

DispatcherServlet

接收用请求响应用户,负责整个处理流程。

5.1框架结构

5.2组件分析

1、前端控制器:

接收用户请求,响应用户。

负责整个处理流程的调度。

可以降低各个组件之间耦合。

2、处理器映射器:

根据url查找handler处理器。

记录了url和handler之间的映射关系。

可以使用bean的name属性建立也可以使用注解方法建立。

返回一个执行链包含拦截器和handler。

3、处理器适配器:

根据不同的handler选择不同的适配器。

可以有多种适配器。

4、处理器:

handler,相当于struts2的action,需要程序员开发。

可以有多种方式,实现Controller接口,实现HttprequestHandler接口,使用注解。

5、视图解析器:

把逻辑视图转换成物理视图。

最常见的视图就是jsp。

其他的视图,freemaker、pdf、excel。

6、视图:

包含很多种,最常见的就是jsp。

渲染视图把model中的数据填充到jsp中。

默认支持jstl。

Jsp也需要开发的

5.3Springmvc框架默认加载的配置

5.4非注解形式的处理器映射器和适配器

--处理器的映射器,如果在映射器中添加扩展时必须在配置文件中配置-->

--处理器适配器-->

5.5注解形式的处理器映射器和适配器

5.5.1早期版本的处理器映射和适配器

映射器:

org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

适配器:

org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

5.5.2最新版本3.2以后

映射器:

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping

适配器:

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter

xmlversion="1.0"encoding="UTF-8"?

>

//www.springframework.org/schema/beans"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"xmlns:

mvc="http:

//www.springframework.org/schema/mvc"

xmlns:

context="http:

//www.springframework.org/schema/context"

xmlns:

aop="http:

//www.springframework.org/schema/aop"xmlns:

tx="http:

//www.springframework.org/schema/tx"

xsi:

schemaLocation="http:

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans-3.2.xsd

http:

//www.springframework.org/schema/mvc

http:

//www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

http:

//www.springframework.org/schema/context

http:

//www.springframework.org/schema/context/spring-context-3.2.xsd

http:

//www.springframework.org/schema/aop

http:

//www.springframework.org/schema/aop/spring-aop-3.2.xsd

http:

//www.springframework.org/schema/tx

http:

//www.springframework.org/schema/tx/spring-tx-3.2.xsd">

--处理器的映射器,如果在映射器中添加扩展时必须在配置文件中配置-->

--注解形式的处理器映射器-->

--处理器适配器-->

--注解形式的处理器适配器-->

--配置扫描包-->

component-scanbase-package="cn.itcast.mvc.controller"/>

--使用bean的name属性配置url和handler的映射关系-->

--实现HttpRequestHandler接口的controller-->

5.5.3配置注解驱动

--注解驱动,配置了这个节点就相当于配置了注解形式的处理器映射器和适配器-->

annotation-driven/>

5.5.4视图解析器

默认的视图解析器:

org.springframework.web.servlet.view.InternalResourceViewResolver

默认支持jsp,使用jstl标签。

--视图解析器-->

--配置前缀和后缀-->

5.6异常分析

5.6.1Jsp找不到

5.6.2404找不到handler

6Springmvc集成mybatis

6.1集成的思路

6.1.1Dao

mybatis集成spring框架使用mapper代理方式,然后配置包扫描将mapper代理对象放到spring容器中。

1、Mapper接口和mapper文件po类可以使用逆向工程获得。

2、在spring配置文件中将sqlsessionfactory对象、mapper扫描器,数据源。

配置到容器中ApplicationContext-dao.xml

3、Mybatis的配置文件。

配置po类的别名。

6.1.2Service

包含业务逻辑,调用dao。

ApplicationContext-service.xml配置扫描包,扫描带@Service注解的类。

ApplicationContext-trans.xml配置事务。

6.1.3Controller

接收页面传递过来的参数,调用service进行业务处理

Springmvc有一个单独的配置文件springmvc.xml

1、配置注解驱动mvc:

annotation-driven

2、配置视图解析器

3、包的扫描器context:

component

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 笔试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1