springmvc是spring框架的一個(gè)模塊,springmvc和spring無需通過中間整合層進(jìn)行整合。
springmvc是一個(gè)基于mvc的web框架

springmvc框架

第一步:發(fā)起請(qǐng)求到前端控制器(DispatcherServlet)
第二步:前端控制器請(qǐng)求HandlerMapping查找 Handler
可以根據(jù)xml配置、注解進(jìn)行查找
第三步:處理器映射器HandlerMapping向前端控制器返回Handler
第四步:前端控制器調(diào)用處理器適配器去執(zhí)行Handler
第五步:處理器適配器去執(zhí)行Handler
第六步:Handler執(zhí)行完成給適配器返回ModelAndView
第七步:處理器適配器向前端控制器返回ModelAndView
ModelAndView是springmvc框架的一個(gè)底層對(duì)象,包括 Model和view
第八步:前端控制器請(qǐng)求視圖解析器去進(jìn)行視圖解析
根據(jù)邏輯視圖名解析成真正的視圖(jsp)
第九步:視圖解析器向前端控制器返回View
第十步:前端控制器進(jìn)行視圖渲染
視圖渲染將模型數(shù)據(jù)(在ModelAndView對(duì)象中)填充到request域
第十一步:前端控制器向用戶響應(yīng)結(jié)果
組件:
1、前端控制器DispatcherServlet(不需要程序員開發(fā))
作用接收請(qǐng)求,響應(yīng)結(jié)果,相當(dāng)于轉(zhuǎn)發(fā)器,中央處理器。
有了DispatcherServlet減少了其它組件之間的耦合度。
2、處理器映射器HandlerMapping(不需要程序員開發(fā))
作用:根據(jù)請(qǐng)求的url查找Handler
3、處理器適配器HandlerAdapter
作用:按照特定規(guī)則(HandlerAdapter要求的規(guī)則)去執(zhí)行Handler
4、處理器Handler(需要程序員開發(fā))
注意:編寫Handler時(shí)按照HandlerAdapter的要求去做,這樣適配器才可以去正確執(zhí)行Handler
5、視圖解析器View resolver(不需要程序員開發(fā))
作用:進(jìn)行視圖解析,根據(jù)邏輯視圖名解析成真正的視圖(view)
6、視圖View(需要程序員開發(fā)jsp)
View是一個(gè)接口,實(shí)現(xiàn)類支持不同的View類型(jsp、freemarker、pdf...)
SpringMVC入門程序

配置前端控制器:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>springmvc01</display-name>
<!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation配置springmvc加載的配置文件(處理器的映射器、適配器等) -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--
1. .action 訪問以.action結(jié)尾的由DispatcherServlet進(jìn)行解析
2. / 搜有訪問地址都有DispatcherServlet解析,對(duì)于靜態(tài)文件的解析需要配置不讓DispatcherServlet解析,
使用此方法可以實(shí)現(xiàn)RESTful風(fēng)格的url
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>```
配置處理器映射器 、處理器適配器、視圖解析器
springmvc.xml
<beans xmlns="http://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 ">
<!-- 配置Handler -->
<bean name="/queryItems.action" class="cn.ztc.ssm.controller.ItemsController1"/>
<!-- 處理器映射器
將bean的那么作為url進(jìn)行查找,需要在配置Handler是指定beanname
-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- 處理器適配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 視圖解析器
解析jsp,默認(rèn)使用jstl標(biāo)簽
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>```
Handler
package cn.ztc.ssm.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import cn.ztc.ssm.po.*;
public class ItemsController1 implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
//調(diào)用service查找數(shù)據(jù)庫,查詢商品列表,這里使用靜態(tài)數(shù)據(jù)模擬
List<Items> itemsList = new ArrayList<Items>();
Items items_1 = new Items();
items_1.setName("聯(lián)想筆記本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 聯(lián)想筆記本電腦!");
Items items_2 = new Items();
items_2.setName("蘋果手機(jī)");
items_2.setPrice(5000f);
items_2.setDetail("iphone6蘋果手機(jī)!");
itemsList.add(items_1);
itemsList.add(items_2);
ModelAndView modelAndView = new ModelAndView();
//在jsp頁面中通過itemsList取數(shù)據(jù)
modelAndView.addObject("itemsList",itemsList);
//指定視圖
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
return modelAndView;
}
}
jsp頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查詢商品列表</title>
</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>商品價(jià)格</td>
<td>生產(chǎn)日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr>
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td>
<td><a href="${pageContext.request.contextPath }/item/editItem.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>```