1.1需求
springmvc和mybatis用商品订单
1.2环境准备
数据库环境
mysql 5.4
mybatis
表
items orderdetail orders user
java环境
jdk1.7
eclipse indigo
spring的版本 spring3.2
将spring3.2所有jar包(一定包括spring-webmvc-3.2.0RELSASE.jar)
在web.xml中配置前端控制器
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>*.actionurl-pattern>
servlet-mapping>
在classpath下的springmvc..xml进行配置处理器配置
"org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
通过查看源代码
public class SimpleControllerHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(Object handler) {
return (handler instanceof Controller);
}
次适配器能执行实现Controller接口的Handler
public interface Controller {
/**
* Process the request and return a ModelAndView object which the DispatcherServlet
* will render. A {@code null} return value is not an error: it indicates that
* this object completed request processing itself and that there is therefore no
* ModelAndView to render.
* @param request current HTTP request
* @param response current HTTP response
* @return a ModelAndView to render, or {@code null} if handled directly
* @throws Exception in case of errors
*/
ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception;
}
需要实现Controller接口,才能由org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter适配器执行
public class ItemController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
//商品列表
List itemsilt=new ArrayList();
Items items1=new Items();
items1.setName("联想电脑");
items1.setPrice(6000f);
items1.setDetail("Thinskkee 联想笔记本");
Items items2=new Items();
items2.setName("苹果手机");
items2.setPrice(6500f);
items2.setDetail("苹果手机");
itemsilt.add(items1);
itemsilt.add(items2);
//创建modelAndView准备填充数据,设置视图
ModelAndView modelAndView=new ModelAndView();
//相当于request的setAttribute在jsp页面中通过itemsList取数据
modelAndView.addObject("itemsList", itemsilt);
//视图
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
return modelAndView;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<base href="<%=basePath%>">
<title>商品列表title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
head>
<body>
<form action="${pageContext.request.contextPath}/item/queryItem.action" method="post">
查询条件:、
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询">td>
tr>
table>
<table width="100%" border=1 >
<tr>
<td>商品名称td>
<td>商品价格td>
<td>商品描述td>
tr>
<c:forEach items="${itemsList}" var="item">
<td>${item.name }td>
<td>${item.price}td>
<td>${item.detail }td>
c:forEach>
table>
form>
body>
html>
将编写的Handler在spring容器中加载
<bean name="/queryItems.action" class="cn.dxq.ssm.controller.ItemController"/>
在classpath下的springmvc.xml中配置处理器映射器
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">bean>
部署调试
访问http://localhost/Spring_Mvc/queryItems.action
处理器映射器根据URL找不到Handler跟下边的错误,说明URL有误
HTTP Statuts 404
处理器映射器根据URL找到了Handler,转发的jsp页面找到报下边的错误,说明jsp地址错误了
4.1处理器映射器
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
4.2另一个映射器
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
<bean id="ItemController1" name="/queryItems.action" class="cn.dxq.ssm.controller.ItemController"/>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/queryItems1.action">ItemController1prop>
props>
property>
bean>
4.3处理器适配器
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
要求编写handler实现controller接口
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
要求编写的handler实现HttpRequestHandler接口
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"> bean>
public class ItemController2 implements HttpRequestHandler{
@Override
public void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//商品列表
List<Items> itemsilt=new ArrayList<Items>();
Items items1=new Items();
items1.setName("联想电脑");
items1.setPrice(6000f);
items1.setDetail("Thinskkee 联想笔记本");
Items items2=new Items();
items2.setName("苹果手机");
items2.setPrice(6500f);
items2.setDetail("苹果手机");
itemsilt.add(items1);
itemsilt.add(items2);
//设置模型数据
request.setAttribute("itemsList", itemsilt);
//设置转发视图
request.getRequestDispatcher("/WEB-INF/jsp/items/itemsList.jsp").forward(request, response);
}
//使用此类方法可以通过修改response,设置响应的数据格式,比如响应json数据
response.setCharacterEncoding("utf-8");
response.setContextType("application/json;charset=utf-8");
respomse.getWriter().writer("json串");
/org/springframework/web/servlet/DispatcherServlet.properties
前端控制器从上边的文件中加载处理器映射器,适配器,视图解析器等组件,如果不在springmvc.xml中配置,使用默认加载的
在spring3.1之前使用的是org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping注解映射器
之后是org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping注解映射器
在spring3.1之前使用的适配器是org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter注解适配器
之后是org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter注解适配器
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">bean>
使用注解的映射器和注解适配器,(注解的映射器和注解的适配器必须配对使用)
//使用@Controller表示它是一个控制器
@Controller
public class ItemController3 {
//商品查询列表
/**
* @RequestMapping实现对queryItems方法和URL进行映射,一个方法对应一个URL
*一般建议方法和URL名字一致
*/
@RequestMapping("/queryItem_1.action")
public ModelAndView queryItenm() throws Exception{
//商品列表
List itemsilt=new ArrayList();
Items items1=new Items();
items1.setName("联想电脑");
items1.setPrice(6000f);
items1.setDetail("Thinskkee 联想笔记本");
Items items2=new Items();
items2.setName("苹果手机");
items2.setPrice(6500f);
items2.setDetail("苹果手机");
itemsilt.add(items1);
itemsilt.add(items2);
//创建modelAndView准备填充数据,设置视图
ModelAndView modelAndView=new ModelAndView();
//相当于request的setAttribute在jsp页面中通过itemsList取数据
modelAndView.addObject("itemsList", itemsilt);
//视图
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
return modelAndView;
}
<context:component-scan base-package="cn.dxq.ssm.controller">context:component-scan>
通过前端控制器源码分析springmvc的执行过程
第一步:前端控制器接受请求
调用doDispatch
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
第二步:前端控制器调用处理器映射器差查找handler
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
for (HandlerMapping hm : this.handlerMappings) {
if (logger.isTraceEnabled()) {
logger.trace(
"Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'");
}
HandlerExecutionChain handler = hm.getHandler(request);
if (handler != null) {
return handler;
}
}
return null;
}
第三步:调用处理器适配器执行handler,得到执行结果ModelAndView
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
第四步:视图渲染:将model数据填充到request域
视图解析 得到view
View=resolveViewName(mv.getViewName(),mv.getModelInternal(). locale, request);
调用view渲染方法,将model数据填充到request域
view.render(mv.getModelInternal(),request,response);
理解springmvc前端控制器,处理器映射器,处理器适配器,视图解析器用法
前端控制器:配置URL
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>*.actionurl-pattern>
servlet-mapping>
处理器映射器
非注解的处理器映射器(了解)
注解的处理器映射器(掌握)
对标记有@controller类中标记有@RequestMapping的方法进行映射,在@RequestMapping里边定义映射的URL
使用注解的映射器,不用在xml中配置URL和handler的映射关系
处理器适配器
非注解的处理器适配器、(了解)
注解的处理器适配器(掌握)
注解处理器适配器和注解处理器映射器是配对使用,理解为不能使用非注解映射器进行映射
可以代替下边的配置
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">bean>
实际开发用
mvc:annotation-driven
//下面的路径,如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀,修改为
//modelAndView .setViewName("/WEB-INF/jsp/items/itemsList.jsp")
//上边的路径配置可以不再程序2总指定jsp路径的前缀和jsp的后缀
modelAndView.setViewName("items/itemsList");
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/">property>
<property name="suffix" value=".jsp">property>
bean>