SpringMVC簡(jiǎn)介
SpringMVC組件概述
SpringMVC請(qǐng)求
SpringMVC響應(yīng)
靜態(tài)資源開(kāi)啟
來(lái)自拉鉤教育就業(yè)訓(xùn)練營(yíng)
一 SpringMVC簡(jiǎn)介
1.1 MVC模式
MVC是軟件工程中的一種軟件架構(gòu)模式,它是一種分離業(yè)務(wù)邏輯與顯示界面的開(kāi)發(fā)思想咒林。
* M(model)模型:處理業(yè)務(wù)邏輯昵宇,封裝實(shí)體
* V(view) 視圖:展示內(nèi)容
* C(controller)控制器:負(fù)責(zé)調(diào)度分發(fā)(1.接收請(qǐng)求、2.調(diào)用模型灶芝、3.轉(zhuǎn)發(fā)到視圖)
1.2 SpringMVC概述
SpringMVC 是一種基于 Java 的實(shí)現(xiàn) MVC 設(shè)計(jì)模式的輕量級(jí) Web 框架郑原,屬于SpringFrameWork 的后續(xù)產(chǎn)品,已經(jīng)融合在 Spring Web Flow 中夜涕。
SpringMVC 已經(jīng)成為目前最主流的MVC框架之一犯犁,并且隨著Spring3.0 的發(fā)布,全面超越 Struts2女器,成為最優(yōu)秀的 MVC 框架酸役。它通過(guò)一套注解,讓一個(gè)簡(jiǎn)單的 Java 類成為處理請(qǐng)求的控制器驾胆,而無(wú)須實(shí)現(xiàn)任何接口涣澡。同時(shí)它還支持 RESTful 編程風(fēng)格的請(qǐng)求。
1.3 SpringMVC快速入門(mén)
需求
客戶端發(fā)起請(qǐng)求丧诺,服務(wù)器接收請(qǐng)求入桂,執(zhí)行邏輯并進(jìn)行視圖跳轉(zhuǎn)。
1. 創(chuàng)建web項(xiàng)目驳阎,導(dǎo)入SpringMVC相關(guān)坐標(biāo)
2. 配置SpringMVC前端控制器 DispathcerServlet
3. 編寫(xiě)Controller類和視圖頁(yè)面
4. 使用注解配置Controller類中業(yè)務(wù)方法的映射地址
5. 配置SpringMVC核心文件 spring-mvc.xml
1)創(chuàng)建web項(xiàng)目抗愁,導(dǎo)入SpringMVC相關(guān)坐標(biāo)
<!-- 設(shè)置為web工程 -->
<packaging>war</packaging>
<dependencies>
<!--springMVC坐標(biāo)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!--servlet坐標(biāo)-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--jsp坐標(biāo)-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
2)配置SpringMVC前端控制器DispathcerServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet?class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3)編寫(xiě)Controller類和視圖頁(yè)面
public class UserController {
public String quick() {
System.out.println("quick running.....");
return "/WEB-INF/pages/success.jsp";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>success</title>
</head>
<body>
<h3>請(qǐng)求成功!</h3>
</body>
</html>
4)使用注解配置Controller類中業(yè)務(wù)方法的映射地址
@Controller
public class UserController {
@RequestMapping("/quick")
public String quick() {
System.out.println("quick running.....");
return "/WEB-INF/pages/success.jsp";
}
}
1.4 web工程執(zhí)行流程
二 SpringMVC組件概述
2.1 SpringMVC的執(zhí)行流程
1.用戶發(fā)送請(qǐng)求到前端控制器dispatcherServlet
2.dispatcherServlet收到請(qǐng)求調(diào)HandlerMapping處理器映射器
3. 處理器映射器找到具體的處理器(可以根據(jù)xml配置、注解進(jìn)行查找)癞季,生成處理器對(duì)象及處理器攔截器(如
果有則生成)一并返回給DispatcherServlet志于。
4. DispatcherServlet調(diào)用HandlerAdapter處理器適配器废睦。
5. HandlerAdapter經(jīng)過(guò)適配調(diào)用具體的處理器(Controller奈应,也叫后端控制器)杖挣。
6. Controller執(zhí)行完成返回ModelAndView惩妇。
7. HandlerAdapter將controller執(zhí)行結(jié)果ModelAndView返回給DispatcherServlet歌殃。
8. DispatcherServlet將ModelAndView傳給ViewReslover視圖解析器氓皱。
9. ViewReslover解析后返回具體View波材。
10. DispatcherServlet根據(jù)View進(jìn)行渲染視圖(即將模型數(shù)據(jù)填充至視圖中)揣非。
11. DispatcherServlet將渲染后的視圖響應(yīng)響應(yīng)用戶。
2.2 SpringMVC組件解析
1. 前端控制器:DispatcherServlet
用戶請(qǐng)求到達(dá)前端控制器大脉,它就相當(dāng)于 MVC 模式中的 C镰矿,DispatcherServlet 是整個(gè)流程控制的中心秤标,由它調(diào)用其它組件處理用戶的請(qǐng)求苍姜,DispatcherServlet 的存在降低了組件之間的耦合性。
2. 處理器映射器:HandlerMapping
HandlerMapping 負(fù)責(zé)根據(jù)用戶請(qǐng)求找到 Handler 即處理器垫释,SpringMVC 提供了不同的映射器實(shí)現(xiàn)不同的映射方式棵譬,例如:配置文件方式订咸,實(shí)現(xiàn)接口方式,注解方式等涩禀。
3. 處理器適配器:HandlerAdapter
通過(guò) HandlerAdapter 對(duì)處理器進(jìn)行執(zhí)行艾船,這是適配器模式的應(yīng)用屿岂,通過(guò)擴(kuò)展適配器可以對(duì)更多類型的處理器進(jìn)行執(zhí)行爷怀。
4. 處理器:Handler【**開(kāi)發(fā)者編寫(xiě)**】
它就是我們開(kāi)發(fā)中要編寫(xiě)的具體業(yè)務(wù)控制器。由 DispatcherServlet 把用戶請(qǐng)求轉(zhuǎn)發(fā)到Handler吁朦。由Handler 對(duì)具體的用戶請(qǐng)求進(jìn)行處理逗宜。
5. 視圖解析器:ViewResolver
View Resolver 負(fù)責(zé)將處理結(jié)果生成 View 視圖,View Resolver 首先根據(jù)邏輯視圖名解析成物理視圖名熬甚,即具體的頁(yè)面地址则涯,再生成 View 視圖對(duì)象粟判,最后對(duì) View 進(jìn)行渲染將處理結(jié)果通過(guò)頁(yè)面展示給用戶。
6. 視圖:View 【**開(kāi)發(fā)者編寫(xiě)**】
SpringMVC 框架提供了很多的 View 視圖類型的支持呻澜,包括:jstlView羹幸、freemarkerView栅受、pdfView等依疼。最常用的視圖就是 jsp律罢。一般情況下需要通過(guò)頁(yè)面標(biāo)簽或頁(yè)面模版技術(shù)將模型數(shù)據(jù)通過(guò)頁(yè)面展示給用戶误辑,需要由程序員根據(jù)業(yè)務(wù)需求開(kāi)發(fā)具體的頁(yè)面。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置注解掃描-->
<context:component-scan base-package="com.lagou.controller"/>
<!--處理器映射器和處理器適配器功能增強(qiáng)-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--視圖解析器-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
2.3 SpringMVC注解解析
@Controller
SpringMVC基于Spring容器盒蟆,所以在進(jìn)行SpringMVC操作時(shí)历等,需要將Controller存儲(chǔ)到Spring容器中荐捻,如果使用@Controller注解標(biāo)注的話处面,就需要使用:
<!--配置注解掃描-->
<context:component-scan base-package="com.lagou.controller"/>
@RequestMapping
* 作用:
用于建立請(qǐng)求 URL 和處理請(qǐng)求方法之間的對(duì)應(yīng)關(guān)系
* 位置:
1.類上:請(qǐng)求URL的第一級(jí)訪問(wèn)目錄。此處不寫(xiě)的話野揪,就相當(dāng)于應(yīng)用的根目錄海铆。寫(xiě)的話需要以/開(kāi)頭。
它出現(xiàn)的目的是為了使我們的URL可以按照模塊化管理:
用戶模塊
/user/add
/user/update
/user/delete
...
賬戶模塊
/account/add
/account/update
/account/delete
2.方法上:請(qǐng)求URL的第二級(jí)訪問(wèn)目錄挣惰,和一級(jí)目錄組成一個(gè)完整的 URL 路徑卧斟。
* 屬性:
1.value:用于指定請(qǐng)求的URL。它和path屬性的作用是一樣的
2.method:用來(lái)限定請(qǐng)求的方式
3.params:用來(lái)限定請(qǐng)求參數(shù)的條件
例如:params={"accountName"} 表示請(qǐng)求參數(shù)中必須有accountName
pramss={"money!100"} 表示請(qǐng)求參數(shù)中money不能是100
三 SpringMVC的請(qǐng)求
3.1 請(qǐng)求參數(shù)類型介紹
客戶端請(qǐng)求參數(shù)的格式是: name=value&name=value……
服務(wù)器要獲取請(qǐng)求的參數(shù)的時(shí)候要進(jìn)行類型轉(zhuǎn)換通熄,有時(shí)還需要進(jìn)行數(shù)據(jù)的封裝
SpringMVC可以接收如下類型的參數(shù):
- 基本類型參數(shù)
- 對(duì)象類型參數(shù)
- 數(shù)組類型參數(shù)
- 集合類型參數(shù)
3.2 獲取基本類型參數(shù)
Controller中的業(yè)務(wù)方法的參數(shù)名稱要與請(qǐng)求參數(shù)的name一致唆涝,參數(shù)值會(huì)自動(dòng)映射匹配。并且能自動(dòng)做類型轉(zhuǎn)換唇辨;自動(dòng)的類型轉(zhuǎn)換是指從String向其他類型的轉(zhuǎn)換廊酣。
[基本類型](${pageContext.request.contextPath}/user/simpleParam?id=1&username=杰克)
@RequestMapping("/simpleParam")
public String simpleParam(Integer id,String username) {
System.out.println(id);
System.out.println(username);
return "success";
}
3.3 獲取對(duì)象類型參數(shù)
Controller中的業(yè)務(wù)方法參數(shù)的POJO屬性名與請(qǐng)求參數(shù)的name一致,參數(shù)值會(huì)自動(dòng)映射匹配。
3.4 中文亂碼過(guò)濾器
當(dāng)post請(qǐng)求時(shí),數(shù)據(jù)會(huì)出現(xiàn)亂碼,我們可以設(shè)置一個(gè)過(guò)濾器來(lái)進(jìn)行編碼的過(guò)濾市咆。
<!--配置全局過(guò)濾的filter-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter?class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.5 獲取數(shù)組類型參數(shù)
Controller中的業(yè)務(wù)方法數(shù)組名稱與請(qǐng)求參數(shù)的name一致采缚,參數(shù)值會(huì)自動(dòng)映射匹配。
<form action="${pageContext.request.contextPath}/user/arrayParam">
編號(hào):<br>
<input type="checkbox" name="ids" value="1">1<br>
<input type="checkbox" name="ids" value="2">2<br>
<input type="checkbox" name="ids" value="3">3<br>
<input type="checkbox" name="ids" value="4">4<br>
<input type="checkbox" name="ids" value="5">5<br>
<input type="submit" value="數(shù)組類型">
</form>
@RequestMapping("/arrayParam")
public String arrayParam(Integer[] ids) {
System.out.println(Arrays.toString(ids));
return "success";
}
3.6 獲取集合(復(fù)雜)
類型參數(shù)獲得集合參數(shù)時(shí)败砂,要將集合參數(shù)包裝到一個(gè)POJO中才可以斜姥。
<form action="${pageContext.request.contextPath}/user/queryParam" method="post">
搜索關(guān)鍵字:
<input type="text" name="keyword"> <br>
user對(duì)象:
<input type="text" name="user.id" placeholder="編號(hào)">
<input type="text" name="user.username" placeholder="姓名"><br>
list集合<br>
第一個(gè)元素:
<input type="text" name="userList[0].id" placeholder="編號(hào)">
<input type="text" name="userList[0].username" placeholder="姓名"><br>
第二個(gè)元素:
<input type="text" name="userList[1].id" placeholder="編號(hào)">
<input type="text" name="userList[1].username" placeholder="姓名"><br>
map集合<br>
第一個(gè)元素:
<input type="text" name="userMap['u1'].id" placeholder="編號(hào)">
<input type="text" name="userMap['u1'].username" placeholder="姓名"><br>
第二個(gè)元素:
<input type="text" name="userMap['u2'].id" placeholder="編號(hào)">
<input type="text" name="userMap['u2'].username" placeholder="姓名"><br>
<input type="submit" value="復(fù)雜類型">
</form>
public class QueryVo {
private String keyword;
private User user;
private List<User> userList;
private Map<String, User> userMap;
}
@RequestMapping("/queryParam")
public String queryParam(QueryVo queryVo) {
System.out.println(queryVo);
return "success";
}
3.7 自定義類型轉(zhuǎn)換器
SpringMVC 默認(rèn)已經(jīng)提供了一些常用的類型轉(zhuǎn)換器朽肥;例如:客戶端提交的字符串轉(zhuǎn)換成int型進(jìn)行參
數(shù)設(shè)置,日期格式類型要求為:yyyy/MM/dd 不然的話會(huì)報(bào)錯(cuò),對(duì)于特有的行為,SpringMVC提供了自
定義類型轉(zhuǎn)換器方便開(kāi)發(fā)者自定義處理。
<form action="${pageContext.request.contextPath}/user/converterParam">
生日:<input type="text" name="birthday">
<input type="submit" value="自定義類型轉(zhuǎn)換器">
</form>
public class DateConverter implements Converter<String, Date> {
public Date convert(String dateStr) {
//將日期字符串轉(zhuǎn)換成日期對(duì)象 返回
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
<!--處理器映射器和適配器增強(qiáng)-->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation?driven>
<!--自定義轉(zhuǎn)換器配置-->
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.lagou.converter.DateConverter"></bean>
</set>
</property>
</bean>
@RequestMapping("/converterParam")
public String converterParam(Date birthday) {
System.out.println(birthday);
return "success";
}
3.8 相關(guān)注解
@RequestParam
當(dāng)請(qǐng)求的參數(shù)name名稱與Controller的業(yè)務(wù)方法參數(shù)名稱不一致時(shí)温数,就需要通過(guò)@RequestParam注解顯示的綁定
[分頁(yè)查詢](${pageContext.request.contextPath}/user/findByPage?pageNo=2)
/*
@RequestParam() 注解
defaultValue 設(shè)置參數(shù)默認(rèn)值
name 匹配頁(yè)面?zhèn)鬟f參數(shù)的名稱
required 設(shè)置是否必須傳遞參數(shù),默認(rèn)值為true刹衫;如果設(shè)置了默認(rèn)值,值自動(dòng)改為false
*/
@RequestMapping("/findByPage")
public String findByPage(@RequestParam(name = "pageNo", defaultValue = "1")
Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
System.out.println(pageNum);
System.out.println(pageSize);
return "success";
}
@RequestHeader
獲取請(qǐng)求頭的數(shù)據(jù)叽躯。
@RequestMapping("/requestHead")
public String requestHead(@RequestHeader("cookie") String cookie) {
System.out.println(cookie);
return "success";
}
@CookieValue
獲取cookie中的數(shù)據(jù)。
@RequestMapping("/cookieValue")
public String cookieValue(@CookieValue("JSESSIONID") String jesessionId) {
System.out.println(jesessionId);
return "success";
}
3.9 獲取Servlet相關(guān)API
SpringMVC支持使用原始ServletAPI對(duì)象作為控制器方法的參數(shù)進(jìn)行注入葛菇,常用的對(duì)象如下:
@RequestMapping("/servletAPI")
public String servletAPI(HttpServletRequest request, HttpServletResponse
response, HttpSession session) {
System.out.println(request);
System.out.println(response);
System.out.println(session);
return "success";
}
四 SpringMVC的響應(yīng)
4.1 SpringMVC響應(yīng)方式介紹
頁(yè)面跳轉(zhuǎn)
- 返回字符串邏輯視圖
- void原始ServletAPI
- ModelAndView
返回?cái)?shù)據(jù)
- 直接返回字符串?dāng)?shù)據(jù)
- 將對(duì)象或集合轉(zhuǎn)為json返回(任務(wù)二演示)
4.2 返回字符串邏輯視圖
直接返回字符串:此種方式會(huì)將返回的字符串與視圖解析器的前后綴拼接后跳轉(zhuǎn)到指定頁(yè)面
@RequestMapping("/returnString")
public String returnString() {
return "success";
}
4.3 void原始ServletAPI
我們可以通過(guò)request卿泽、response對(duì)象實(shí)現(xiàn)響應(yīng)
@RequestMapping("/returnVoid")
public void returnVoid(HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 1.通過(guò)response直接響應(yīng)數(shù)據(jù)
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("拉勾網(wǎng)");
----------------------------------------
request.setAttribute("username", "拉勾教育");
// 2.通過(guò)request實(shí)現(xiàn)轉(zhuǎn)發(fā)
request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,
response);
----------------------------------------
// 3.通過(guò)response實(shí)現(xiàn)重定向
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
4.4 轉(zhuǎn)發(fā)和重定向
企業(yè)開(kāi)發(fā)我們一般使用返回字符串邏輯視圖實(shí)現(xiàn)頁(yè)面的跳轉(zhuǎn)签夭,這種方式其實(shí)就是請(qǐng)求轉(zhuǎn)發(fā)慎宾。
我們也可以寫(xiě)成:forward轉(zhuǎn)發(fā)
如果用了forward:則路徑必須寫(xiě)成實(shí)際視圖url,不能寫(xiě)邏輯視圖。它相當(dāng)于:
request.getRequestDispatcher("url").forward(request,response)
使用請(qǐng)求轉(zhuǎn)發(fā)苍凛,既可以轉(zhuǎn)發(fā)到j(luò)sp兵志,也可以轉(zhuǎn)發(fā)到其他的控制器方法醇蝴。
@RequestMapping("/forward")
public String forward(Model model) {
model.addAttribute("username", "拉勾招聘");
return "forward:/WEB-INF/pages/success.jsp";
}
Redirect重定向
我們可以不寫(xiě)虛擬目錄,springMVC框架會(huì)自動(dòng)拼接想罕,并且將Model中的數(shù)據(jù)拼接到url地址上
@RequestMapping("/redirect")
public String redirect(Model model) {
model.addAttribute("username", "拉勾教育");
return "redirect:/index.jsp";
}
4.5 ModelAndView
4.5.1 方式一
在Controller中方法創(chuàng)建并返回ModelAndView對(duì)象悠栓,并且設(shè)置視圖名稱
@RequestMapping("/returnModelAndView1")
public ModelAndView returnModelAndView1() {
/*
Model:模型 作用封裝數(shù)據(jù)
View:視圖 作用展示數(shù)據(jù)
*/
ModelAndView modelAndView = new ModelAndView();
//設(shè)置模型數(shù)據(jù)
modelAndView.addObject("username", " lagou");
//設(shè)置視圖名稱
modelAndView.setViewName("success");
return modelAndView;
}
4.5.2 方式二
在Controller中方法形參上直接聲明ModelAndView,無(wú)需在方法中自己創(chuàng)建按价,在方法中直接使用該對(duì)象設(shè)置視圖惭适,同樣可以跳轉(zhuǎn)頁(yè)面
@RequestMapping("/returnModelAndView2")
public ModelAndView returnModelAndView2(ModelAndView modelAndView) {
//設(shè)置模型數(shù)據(jù)
modelAndView.addObject("username", "itheima");
//設(shè)置視圖名稱
modelAndView.setViewName("success");
return modelAndView;
}
4.6 @SessionAttributes
如果在多個(gè)請(qǐng)求之間共用數(shù)據(jù),則可以在控制器類上標(biāo)注一個(gè) @SessionAttributes,配置需要在
session中存放的數(shù)據(jù)范圍楼镐,Spring MVC將存放在model中對(duì)應(yīng)的數(shù)據(jù)暫存到 HttpSession 中癞志。
注意:@SessionAttributes只能定義在類上
@Controller
@SessionAttributes("username") //向request域存入的key為username時(shí),同步到session域中
public class UserController {
@RequestMapping("/forward")
public String forward(Model model) {
model.addAttribute("username", "子慕");
return "forward:/WEB-INF/pages/success.jsp";
}
@RequestMapping("/returnString")
public String returnString() {
return "success";
}
}
五 靜態(tài)資源訪問(wèn)的開(kāi)啟
當(dāng)有靜態(tài)資源需要加載時(shí)鸠蚪,比如jquery文件今阳,通過(guò)谷歌開(kāi)發(fā)者工具抓包發(fā)現(xiàn),沒(méi)有加載到j(luò)query文件茅信,原因是SpringMVC的前端控制器DispatcherServlet的url-pattern配置的是 /(缺识苌唷),代表對(duì)所有的靜態(tài)資源都進(jìn)行處理操作,這樣就不會(huì)執(zhí)行Tomcat內(nèi)置的DefaultServlet處理蘸鲸,我們可以通過(guò)以下兩種方式指定放行靜態(tài)資源:
方式一
<!--在springmvc配置文件中指定放行資源-->
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/img/**" location="/img/"/>
方式二
<!--在springmvc配置文件中開(kāi)啟DefaultServlet處理靜態(tài)資源-->
<mvc:default-servlet-handler/>