springmvc是spring框架的一個模塊,springmvc和spring無需通過中間整合層進行整合。
springmvc是一個基于mvc的web框架
springmvc框架
第一步:發起請求到前端控制器(DispatcherServlet)
第二步:前端控制器請求HandlerMapping查找 Handler
可以根據xml配置、注解進行查找
第三步:處理器映射器HandlerMapping向前端控制器返回Handler
第四步:前端控制器調用處理器適配器去執行Handler
第五步:處理器適配器去執行Handler
第六步:Handler執行完成給適配器返回ModelAndView
第七步:處理器適配器向前端控制器返回ModelAndView
ModelAndView是springmvc框架的一個底層對象,包括 Model和view
第八步:前端控制器請求視圖解析器去進行視圖解析
根據邏輯視圖名解析成真正的視圖(jsp)
第九步:視圖解析器向前端控制器返回View
第十步:前端控制器進行視圖渲染
視圖渲染將模型數據(在ModelAndView對象中)填充到request域
第十一步:前端控制器向用戶響應結果
組件:
1、前端控制器DispatcherServlet(不需要程序員開發)
作用接收請求,響應結果,相當于轉發器,中央處理器。
有了DispatcherServlet減少了其它組件之間的耦合度。
2、處理器映射器HandlerMapping(不需要程序員開發)
作用:根據請求的url查找Handler
3、處理器適配器HandlerAdapter
作用:按照特定規則(HandlerAdapter要求的規則)去執行Handler
4、處理器Handler(需要程序員開發)
注意:編寫Handler時按照HandlerAdapter的要求去做,這樣適配器才可以去正確執行Handler
5、視圖解析器View resolver(不需要程序員開發)
作用:進行視圖解析,根據邏輯視圖名解析成真正的視圖(view)
6、視圖View(需要程序員開發jsp)
View是一個接口,實現類支持不同的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結尾的由DispatcherServlet進行解析
2. / 搜有訪問地址都有DispatcherServlet解析,對于靜態文件的解析需要配置不讓DispatcherServlet解析,
使用此方法可以實現RESTful風格的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進行查找,需要在配置Handler是指定beanname
-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- 處理器適配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 視圖解析器
解析jsp,默認使用jstl標簽
-->
<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 {
//調用service查找數據庫,查詢商品列表,這里使用靜態數據模擬
List<Items> itemsList = new ArrayList<Items>();
Items items_1 = new Items();
items_1.setName("聯想筆記本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 聯想筆記本電腦!");
Items items_2 = new Items();
items_2.setName("蘋果手機");
items_2.setPrice(5000f);
items_2.setDetail("iphone6蘋果手機!");
itemsList.add(items_1);
itemsList.add(items_2);
ModelAndView modelAndView = new ModelAndView();
//在jsp頁面中通過itemsList取數據
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>商品價格</td>
<td>生產日期</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>```