一、spingMVC 簡單實現
1. 創建一個動態網頁項目(本例:springMVCdemo1) 采用2.5框架版本,需要的 jar 包如下
2. 在 web.xml 中配置核心控制器 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>springMVCdemo1</display-name> <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> <!-- 前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定spring.xml文件位置 默認找 /WEB-INF/[servlet的名稱]-servlet.xml --> <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. /* 攔截所有 jsp js png .css 真的全攔截 建議不使用 2. *.action *.do 攔截以do action 結尾的請求 肯定能使用 ERP 3. / 攔截所有 (不包括jsp) (包含.js .png.css) 強烈建議使用 前台 面向消費者 www.jd.com/search /對靜態資源放行 --> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
3. 新建 config 文件夾與 springmvc.xml 文件 目錄結構與文件內容如下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 掃描@Controler @Service --> <context:component-scan base-package="com.dic"/> </beans>
4. 在 src 下 准備pojo類、controller類;在WEB-INF下建立文件夾jsp並建立文件 itemList.jsp

1 package com.dic.pojo; 2 3 import java.util.Date; 4 5 public class Items { 6 7 public Items(Integer id, String name, Float price, Date createtime, String detail) { 8 super(); 9 this.id = id; 10 this.name = name; 11 this.price = price; 12 this.createtime = createtime; 13 this.detail = detail; 14 } 15 16 private Integer id; 17 18 private String name; 19 20 private Float price; 21 22 private String pic; 23 24 private Date createtime; 25 26 private String detail; 27 28 public Integer getId() { 29 return id; 30 } 31 32 public void setId(Integer id) { 33 this.id = id; 34 } 35 36 public String getName() { 37 return name; 38 } 39 40 public void setName(String name) { 41 this.name = name == null ? null : name.trim(); 42 } 43 44 public Float getPrice() { 45 return price; 46 } 47 48 public void setPrice(Float price) { 49 this.price = price; 50 } 51 52 public String getPic() { 53 return pic; 54 } 55 56 public void setPic(String pic) { 57 this.pic = pic == null ? null : pic.trim(); 58 } 59 60 public Date getCreatetime() { 61 return createtime; 62 } 63 64 public void setCreatetime(Date createtime) { 65 this.createtime = createtime; 66 } 67 68 public String getDetail() { 69 return detail; 70 } 71 72 public void setDetail(String detail) { 73 this.detail = detail == null ? null : detail.trim(); 74 } 75 76 }

1 package com.dic.controller; 2 3 import java.util.ArrayList; 4 import java.util.Date; 5 import java.util.List; 6 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.servlet.ModelAndView; 10 11 import com.dic.pojo.Items; 12 13 @Controller 14 public class ItemController { 15 16 //指定路徑的訪問url .action 可寫可不寫 17 @RequestMapping(value = "/itemlist.action") 18 public ModelAndView itemList(){ 19 20 // 創建頁面需要顯示的商品數據 21 List<Items> list = new ArrayList<Items>(); 22 list.add(new Items(1, "1華為 榮耀8", 2399f, new Date(), "質量好!1")); 23 list.add(new Items(2, "2華為 榮耀8", 2399f, new Date(), "質量好!2")); 24 list.add(new Items(3, "3華為 榮耀8", 2399f, new Date(), "質量好!3")); 25 list.add(new Items(4, "4華為 榮耀8", 2399f, new Date(), "質量好!4")); 26 list.add(new Items(5, "5華為 榮耀8", 2399f, new Date(), "質量好!5")); 27 list.add(new Items(6, "6華為 榮耀8", 2399f, new Date(), "質量好!6")); 28 29 // 創建ModelAndView用來存放數據和視圖 30 ModelAndView mav = new ModelAndView(); 31 // 設置數據到模型中 32 mav.addObject("itemList", list); 33 // 設置視圖jsp 需要設置視圖的物理地址 34 mav.setViewName("/WEB-INF/jsp/itemList.jsp"); 35 36 return mav; 37 } 38 39 }

1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 4 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>查詢商品列表</title> 10 </head> 11 <body> 12 <form action="${pageContext.request.contextPath }/item/queryitem.action" method="post"> 13 查詢條件: 14 <table width="100%" border=1> 15 <tr> 16 <td><input type="submit" value="查詢"/></td> 17 </tr> 18 </table> 19 商品列表: 20 <table width="100%" border=1> 21 <tr> 22 <td>商品名稱</td> 23 <td>商品價格</td> 24 <td>生產日期</td> 25 <td>商品描述</td> 26 <td>操作</td> 27 </tr> 28 <c:forEach items="${itemList }" var="item"> 29 <tr> 30 <td>${item.name }</td> 31 <td>${item.price }</td> 32 <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td> 33 <td>${item.detail }</td> 34 35 <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td> 36 37 </tr> 38 </c:forEach> 39 40 </table> 41 </form> 42 </body> 43 44 </html>
5. 運行程序(Tomcat 7)。打開瀏覽器訪問下列地址 出現商品列表即成功
http://localhost:8080/springMVCdemo1/itemlist.action
二、springMVC 架構
1. 框架結構
2. 架構流程
1. 用戶發送請求到前端控制器DispatcherServlet
2. DispatcherServlet 收到請求調用 HanderMapping 處理器映射器
3. 處理器映射器根據請求 URL 找到具體的處理器,生成處理器對象及處理器攔截器(若有則生成)一並返回給 DispatcherServlet
4. DispatcherServlet 通過 HandlerAdapter 處理器適配器調用處理器
5. 執行處理器 Controller (也叫后端控制器)
6. Controller 執行完成后返回 ModeAndView
7. HandlerAdapter 將 controller 執行結果 ModeAndView 返回給 DispatcherServlet
8. DispatcherServlet 將 ModeAndView 傳給 ViewReslover 視圖解析器
9. ViewReslover 解析后返回具體 View
10. DispatcherServlet 對 View 進行渲染視圖(即將模型數據填充至視圖中)
11. DispatcherServlet 響應用戶
3. springmvc 的三大組件:處理器映射器、處理器適配器、視圖解析器
需要用戶開發的組件有:handler、view
三、組件介紹
1. 組件掃描器
使用 <context:component-scan> 自動掃描標記 @Controller 的控制器類
用法:在 springmvc.xml 配置文件中配置:
<!-- 配置controller掃描包,多個包之間用,分隔 -->
<context:component-scan base-package="com.dic" />
2. 注解映射器和適配器
1. 配置處理器映射器、配置處理器適配器
<!-- 配置處理器映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> <!-- 配置處理器適配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
2. 注冊驅動 :使用注冊驅動可自動加載注解處理器和適配器的配置
<!-- 注解驅動 --> <mvc:annotation-driven />
3. 視圖解析器
邏輯視圖名需要在 controller 中返回 ModeAndView 指定
用法:在 springmvc.xml 配置文件中配置如下
<!-- 配置視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置邏輯視圖的前綴 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 配置邏輯視圖的后綴 --> <property name="suffix" value=".jsp" /> </bean>