一、SpringMVC的簡介
SpringMVC是什麼?
SpringMVC是目前最好的實現MVC設計模式的框架,是Spring框架的一個分支產品,以SpringIOC容器爲基礎,並利用容器的特性來簡化它的配置。SpringMVC相當於Spring的一個子模塊,可以很好的和Spring結合起來進行開發,是JavaWeb開發者必須要掌握的框架。
SpringMVC能幹什麼?
實現了MVC設計模式,MVC設計模式是一種常用的軟件架構方式:以Controller(控制層),Model(模型層),View(視圖層)三個模塊分離的形式來組織代碼。
MVC流程:控制層接受到客戶端請求,調用模型層生成業務數據,傳遞給視圖層,將最終的業務數據和視圖響應給客戶端做展示。
SpringMVC就是對這套流程的封裝,屏蔽掉很多底層代碼,開放出接口,讓開發者可以更加輕鬆快捷的完成基於MVC模式的Web開發。
SpringMVC實現原理:
核心組件:
1.DispatcherServlet:前端控制器,是整個流程控制的核心,控制其他組件的執行,統一調度,降低組件之間的耦合性,相當於總指揮。
2.Handler:處理器,完成具體業務邏輯,相當於Servlet或Action。
3.HandlerMapping:DispatcherServlet接收到請求之後,通過HandlerMapping將不同的請求分發到不同的Handler。
4.HandlerInterceptor:處理器攔截器,是一個接口,如果我們需要做一些攔截處理,可以來實現這個接口。
5.HandlerExecutionChain:處理器執行鏈,包括兩部分內容:Handler和HandlerInterceptor(系統會有一個默認的HandlerInterceptor,如果需要額外攔截處理,可以添加攔截器設置)。
6.HandlerAdapter:處理器適配器,Handler執行業務方法之前,需要進行一系列的操作包括表單數據的驗證,數據類型的轉換,將表單數據封裝到JavaBean等等,這一系列的操作,都是由HandlerAdapter來完成,DispatcherServlet通過HandlerAdapter執行不同的Handler。
7.ModelAndView:裝載了模型數據和視圖信息,作爲Handler的處理結果,返回給DispatcherServlet。
8.ViewResolver:視圖解析器,DispatcherServlet通過它將邏輯視圖解析成物理視圖,最終將渲染結果響應給客戶端。
以上就是SpringMVC的核心組件。那麼這些組件之間是如何進行交互的呢?
我們來看SpringMVC的實現流程:
1.客戶端請求被DispatcherServlet(前端控制器)接收。
2.根據HandlerMapping映射到Handler。
3.生成Handler和HandlerInterceptor(如果有則生成)。
4.Handler和HandlerInterceptor以HandlerExecutionChain的形式一併返回給DispatcherServlet。
5.DispatcherServlet通過HandlerAdapter調用Handler的方法做業務邏輯處理。
6.返回一個ModelAndView對象給DispatcherServlet。
7.DispatcherServlet將獲取的ModelAndView對象傳給ViewResolver視圖解析器,將邏輯視圖解析成物理視圖View。
8.ViewResolver返回一個View給DispatcherServlet。
9.DispatcherServlet根據View進行視圖渲染(將模型數據填充到視圖中)。
10.DispatcherServlet將渲染後的視圖響應給客戶端。
如何使用?
真正需要我們開發者進行編寫的組件只有兩個,Handler:處理業務邏輯, View:JSP做展示。
二、第一個程序
1.web.xml中配置SpringMVC的DispatcherServlet。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 指定springmvc.xml的路徑 -->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.創建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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置自動掃描 -->
<context:component-scan base-package="com.southwind.handler"></context:component-scan>
<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前綴 -->
<property name="prefix" value="/"></property>
<!-- 後綴 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
記住一條:目標資源路徑=前綴+返回值+後綴。
比如DispatcherServlet返回index,配置文件中前綴是/,後綴是.jsp,代入上述公式:
目標資源路徑=/index.jsp。
簡單流程
1.DispatcherServlet接收到URL請求index,結合@RequestMapping("/index")註解將該請求交給index業務方法。
2.執行index業務方法,控制打印日誌,並返回"index"字符串。
3.結合springmvc.xml中的視圖解析器配置,找到目標資源:/index.jsp,即根目錄的index.jsp,將該jsp資源返回客戶端完成響應。
三、註解
@RequestMapping
SpringMVC通過@RequestMapping註解將URL請求與業務方法進行進行映射。
@RequestMapping(value="/postTest",method=RequestMethod.POST)
public String postTest(@RequestParam("name") String name){
System.out.println("postTest");
return "index";
}
SpringMVC同時也支持restful風格的URL。
@RequestMapping(value="rest/{name}")
public String restTest(@PathVariable("name") String name){
System.out.println(name);
return "index";
}
映射Cookie:
SpringMVC通過映射可以直接在業務方法中獲取Cookie的值。
@RequestMapping("/cookieTest")
public String getCookie(@CookieValue(value="JSESSIONID") String sessionId){
System.out.println(sessionId);
return "index";
}
使用pojo綁定參數:
jsp
<form action="addUser" method="post">
編號:<input type="text" name="id"/><br/>
姓名:<input type="text" name="name"/><br/>
地址:<input type="text" name="address.name"/><br/>
<input type="submit" value="提交"/>
</form>
@RequestMapping("/addUser")
public String getPOJO(User user){
System.out.println(user);
return "index";
}
SpringMVC解決中文亂碼很簡單,在web.xml中添加過濾器即可。
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
JSP頁面的轉發和重定向
轉發:
@RequestMapping("forwardTest")
public String forwardTest(){
return "forward:/index.jsp";
}
重定向:
@RequestMapping("redirectTest")
public String redirectTest(){
return "redirect:/index.jsp";
}
四、數據綁定
SpringMVC的HandlerAdapter組件會在執行Handler業務方法之前,完成參數的綁定
@ResponseBody註解直接返回字符串到前端,不需要返回jsp頁面。
@RequestMapping(value="/packageType")
@ResponseBody
public String packageType(@RequestParam(value="id",required=false,defaultValue="1") Integer id){
return "id:"+id;
}
處理@ResponseBody中文亂碼:
在springmvc.xml中配置消息轉換器
<mvc:annotation-driven >
<!-- 消息轉換器 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
List
SpringMVC不支持List類型的直接轉換,需要包裝成Object。
input的name指向自定義包裝類UserList中的users屬性,級聯到name和age,同時以下標區分集合中不同的對象。
<form action="listType" method="post">
用戶1姓名:<input type="text" name="users[0].name"/><br/>
用戶1年齡:<input type="text" name="users[0].age"/><br/>
用戶2姓名:<input type="text" name="users[1].name"/><br/>
用戶2年齡:<input type="text" name="users[1].age"/><br/>
用戶3姓名:<input type="text" name="users[2].name"/><br/>
用戶3年齡:<input type="text" name="users[2].age"/><br/>
<input type="submit" value="提交"/>
</form>
Set
和List一樣,需要封裝自定義包裝類,將Set集合作爲屬性。不同的是,使用Set集合,需要在包裝類構造函數中,爲Set添加初始化對象。
public class UserSet {
private Set<User> users = new HashSet<User>();
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public UserSet(){
users.add(new User());
users.add(new User());
users.add(new User());
}
}
Map
JSP,與List和Set不同的是,不能通過下標區分不同的對象,改爲通過key值區分。
<form action="mapType" method="post">
用戶1姓名:<input type="text" name="users['a'].name"/><br/>
用戶1年齡:<input type="text" name="users['a'].age"/><br/>
用戶2姓名:<input type="text" name="users['b'].name"/><br/>
用戶2年齡:<input type="text" name="users['b'].age"/><br/>
用戶3姓名:<input type="text" name="users['c'].name"/><br/>
用戶3年齡:<input type="text" name="users['c'].age"/><br/>
<input type="submit" value="提交"/>
</form>
JSON
JSP:Ajax請求後臺業務方法,並將json格式的參數傳給後臺。
<script type="text/javascript">
var user = {
"name":"張三",
"age":22
};
$.ajax({
url:"jsonType",
data:JSON.stringify(user),
type:"post",
contentType: "application/json;charse=UTF-8",
dataType:"text",
success:function(data){
var obj = eval("(" + data + ")");
alert(obj.name+"---"+obj.age);
}
})
</script>
注意
1.json數據必須用JSON.stringify()方法轉換成字符串。
2.contentType不能省略。
業務方法:
@RequestMapping(value="/jsonType")
@ResponseBody
public User jsonType(@RequestBody User user){
//修改年齡
user.setAge(user.getAge()+10);
//返回前端
return user;
}
@RequestBody註解
讀取http請求參數,通過SpringMVC提供的HttpMessageConverter接口將讀取的參數轉爲json,xml格式的數據,綁定到業務方法的形參。
@ResponseBody註解
將業務方法返回的對象,通過HttpMessageConverter接口轉爲指定格式的數據,json,xml等,響應給客戶端。
我們使用的是阿里的fastjson來取代Spring默認的Jackson進行數據綁定。
fastjson的優勢在於如果屬性爲空就不會將其轉化爲json,數據會簡潔很多。
如何使用fastjson
1.pom.xml引入fastjson依賴jar包。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.18</version>
</dependency>
2.springmvc.xml中配置fastjson。
<mvc:annotation-driven >
<!-- 消息轉換器 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
</bean>
<!-- 阿里fastjson -->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4"/>
</mvc:message-converters>
</mvc:annotation-driven>
五、模型數據解析
模型數據的綁定是由ViewResolver來完成的,開發時,我們先添加模型數據,再交給ViewResolver來綁定。
模型數據綁定到request域對象
1.Model
@RequestMapping("/modelTest")
public String modelTest(Model model){
User user = new User();
user.setId(1);
user.setName("張三");
model.addAttribute("user", user);
return "index";
}
2.ModelAndView
@RequestMapping("/modelAndViewTest1")
public ModelAndView modelAndViewTest1(){
ModelAndView modelAndView = new ModelAndView();
User user = new User();
user.setId(1);
user.setName("張三");
modelAndView.addObject("user", user);
modelAndView.setViewName("index");
return modelAndView;
}
3.HttpServletRequest
@RequestMapping("requestTest")
public String requestTest(HttpServletRequest request){
User user = new User();
user.setId(1);
user.setName("張三");
request.setAttribute("user", user);
return "index";
}
模型數據綁定到session域對象
以上的方式全部是將模型數據綁定到request對象中,如果需要將模型數據綁定到session對象中,只需要在類定義處添加@SessionAttributes(value="user")註解即可。
@Controller
@SessionAttributes(value="user")
public class HelloHandler {
@SessionAttributes可同時綁定多個模型數據。
@Controller
@SessionAttributes(value={"user","address"})
public class HelloHandler {
六、自定義數據轉換器
Date類型,HandlerAdapter是不會將String類型轉換爲Date類型的,我們需要實現Converter接口來協助SpringMVC完成數據類型的轉換。
1.創建DateConverter轉換器,實現org.springframework.core.convert.converter.Converter接口,
泛型爲<String,Date>,將String類型的數值轉換爲Date類型。
import org.springframework.core.convert.converter.Converter;
public class DateConverter implements Converter<String,Date>{
private String pattern;
public DateConverter(String pattern){
this.pattern = pattern;
}
public Date convert(String source) {
// TODO Auto-generated method stub
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
try {
return simpleDateFormat.parse(source);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
2.springmvc.xml中配置conversionService bean。
bean的類名稱必須是org.springframework.context.support.ConversionServiceFactoryBean。
bean必須包含一個converters屬性,它將列出在應用程序中用到的所有定製Converter。將我們自定義的DateConverter添加到converters中,
通過有參構造函數創建DateConverter。
同時annotation-driven元素的conversion-service屬性賦bean名稱。
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.southwind.utils.DateConverter">
<!-- 調用有參構造函數創建bean -->
<constructor-arg type="java.lang.String" value="yyyy-MM-dd"/>
</bean>
</list>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"/>
3.創建addDate.jsp,form表單提交數據到後臺。
<form action="dateConverterTest" method="post">
請輸入日期:<input type="text" name="date"/><font style="font-size:13px">(yyyy-MM-dd)</font><br/>
<input type="submit" value="提交"/>
</form>
4.創建業務方法。
@RequestMapping(value="/dateConverterTest")
@ResponseBody
public String dateConverterTest(Date date){
return date.toString();
}
除了Date類型的轉換,還可以自定義數據格式,比如註冊一個Student,前端頁面按照"id-name-age"的形式輸入String類型的數據,通過轉換器,可以將該String類型的數據直接轉換爲Student對象。
創建addStudent.jsp。
<form action="studentConverterTest" method="post">
學生信息:<input type="text" name="student"/><font style="font-size:13px">(id-name-age)</font><br/>
<input type="submit" value="提交"/>
</form>
3.創建業務方法。
@RequestMapping(value="/studentConverterTest")
@ResponseBody
public String studentConverterTest(Student student){
return student.toString();
}
4.創建StudentConverter轉換器。
import org.springframework.core.convert.converter.Converter;
import com.southwind.entity.Student;
public class StudentConverter implements Converter<String,Student>{
public Student convert(String source) {
// TODO Auto-generated method stub
String[] args = source.split("-");
Student student = new Student();
student.setId(Integer.parseInt(args[0]));
student.setName(args[1]);
student.setAge(Integer.parseInt(args[2]));
return student;
}
}
5.springmvc.xml中配置StudentConverter轉換器。
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<!-- 日期轉換器 -->
<bean class="com.southwind.utils.DateConverter">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd"/>
</bean>
<!-- Student轉換器 -->
<bean class="com.southwind.utils.StudentConverter"></bean>
</list>
</property>
</bean>
七、RESTful思想
八、文件上傳和下載
單文件上傳
1.底層使用的是Apache fileupload組件完成上傳,SpringMVC只是進行了封裝,讓開發者使用起來更加方便,所以首先需要引入fileupload組件的依賴。
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
2.JSP
1.input的type設置爲file。
2.form表單的method設置爲post。(get請求只會將文件名傳給後臺)
3.form表單的enctype設置爲multipart/form-data,以二進制的形式傳輸數據。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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>Insert title here</title>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="img">
<input type="submit" name="提交">
</form><br />
<c:if test="${filePath!=null }">
<h1>上傳的圖片</h1><br />
<img width="300px" src="<%=basePath %>${filePath}"/>
</c:if>
</body>
</html>
如果上傳成功,返回當前頁面,展示上傳成功的圖片,這裏需要使用JSTL標籤進行判斷。
pom文件中引入JSTL依賴。
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
3.業務方法,使用MultipartFile對象作爲參數,接收前端發送過來的文件,並完成上傳操作。
@RequestMapping(value="/upload", method = RequestMethod.POST)
public String upload(@RequestParam(value="img")MultipartFile img, HttpServletRequest request)
throws Exception {
//getSize()方法獲取文件的大小來判斷是否有上傳文件
if (img.getSize() > 0) {
//獲取保存上傳文件的file文件夾絕對路徑
String path = request.getSession().getServletContext().getRealPath("file");
//獲取上傳文件名
String fileName = img.getOriginalFilename();
File file = new File(path, fileName);
img.transferTo(file);
//保存上傳之後的文件路徑
request.setAttribute("filePath", "file/"+fileName);
return "upload";
}
return "error";
}
4.springmvc.xml配置CommonsMultipartResolver。
<!-- 配置CommonsMultipartResolver bean,id必須是multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 處理文件名中文亂碼 -->
<property name="defaultEncoding" value="utf-8"/>
<!-- 設置多文件上傳,總大小上限,不設置默認沒有限制,單位爲字節,1M=1*1024*1024 -->
<property name="maxUploadSize" value="1048576"/>
<!-- 設置每個上傳文件的大小上限 -->
<property name="maxUploadSizePerFile" value="1048576"/>
</bean>
<!-- 設置異常解析器 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="/error.jsp"/>
</bean>
多文件上傳
1.JSP。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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>Insert title here</title>
</head>
<body>
<form action="uploads" method="post" enctype="multipart/form-data">
file1:<input type="file" name="imgs"><br />
file2:<input type="file" name="imgs"><br />
file3:<input type="file" name="imgs"><br />
<input type="submit" name="提交">
</form>
<c:if test="${filePaths!=null }">
<h1>上傳的圖片</h1><br />
<c:forEach items="${filePaths }" var="filePath">
<img width="300px" src="<%=basePath %>${filePath}"/>
</c:forEach>
</c:if>
</body>
</html>
2.業務方法,使用MultipartFile數組對象接收上傳的多個文件。
@RequestMapping(value="/uploads", method = RequestMethod.POST)
public String uploads(@RequestParam MultipartFile[] imgs, HttpServletRequest request)
throws Exception {
//創建集合,保存上傳後的文件路徑
List<String> filePaths = new ArrayList<String>();
for (MultipartFile img : imgs) {
if (img.getSize() > 0) {
String path = request.getSession().getServletContext().getRealPath("file");
String fileName = img.getOriginalFilename();
File file = new File(path, fileName);
filePaths.add("file/"+fileName);
img.transferTo(file);
}
}
request.setAttribute("filePaths", filePaths);
return "uploads";
}
文件下載
1.JSP,使用超鏈接,下載之前上傳的logo.jpg。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<a href="download?fileName=logo.jpg">下載圖片</a>
</body>
</html>
2.業務方法。
@RequestMapping("/download")
public void downloadFile(String fileName,HttpServletRequest request,
HttpServletResponse response){
if(fileName!=null){
//獲取file絕對路徑
String realPath = request.getServletContext().getRealPath("file/");
File file = new File(realPath,fileName);
OutputStream out = null;
if(file.exists()){
//設置下載完畢不打開文件
response.setContentType("application/force-download");
//設置文件名
response.setHeader("Content-Disposition", "attachment;filename="+fileName);
try {
out = response.getOutputStream();
out.write(FileUtils.readFileToByteArray(file));
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}