//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