Springmvc關於圖片上傳,json資料,攔截器案例的詳解
阿新 • • 發佈:2019-01-25
圖片上傳
- 第一步上傳圖片的表單表達是如下,注意一下幾點 enctype=”mulitipart/form-data” 提交方式為post
<form id="itemForm"
action="${pageContext.request.contextPath }/updateitem.action"
method="post" enctype="multipart/form-data">
<input type="file" name="pictureFile"/>
2.在cotroller中建立一個方法用於圖片上傳:注意:一定要有MultipartFile型別的引數 pictureFile 而且一定要注意引數值一定要保證是pictureFile.同時在springmvc.xml檔案中建立一個對MultipartFile的例項化bean物件。
@RequestMapping(value = "/updateitem.action")
public String updateitem(QueryVo vo, MultipartFile pictureFile) throws IllegalStateException, IOException {
// 儲存圖片d:upload
String name = UUID.randomUUID().toString().replace("-", "");
// 獲取 字尾名
String ext = FilenameUtils.getExtension(pictureFile.getOriginalFilename());
//儲存檔案至指定的目錄。
pictureFile.transferTo(new File("D:\\pic\\" + name + "." + ext));
//將檔名稱儲存進資料庫
vo.getItems().setPic(name + "." + ext);
itemservice.updateItem(vo.getItems());
//重定向到該頁面
return "redirect:/item/toEdit.action?id=" + vo.getItems().getId();
}
配置上傳圖片的例項物件
<!-- 配置上傳檔案實現類 name屬性的值一定要是這個值負責,在容器中找不到這個物件 -->
<bean name="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 最大上傳大小 -->
<property name="maxUploadSize" value="5000000"></property>
</bean>
Json資料
json資料通過Ajax請求到伺服器然後回撥到頁面
**1.首先要引入jQuery
2.通過書寫ajax程式碼來發送json字串,這裡有一個標準而且嚴格的數字方式。**
$(function() {
//alert("a");
//宣告一個json字串。
var param = '{"id":1,"name":"測試商品資訊","price":660,"detail":"測試商品描述","pic":"123456.jpg"}';
$.ajax({
url : "${pageContext.request.contextPath }/json.action",
data : param,
contentType : "application/json",
type : "post",
dataType : "json",//回撥
success : function(data) {
//下邊這是回撥內容程式碼
alert(data.name);
}
});
});
3.通過在控制器中接受所傳送的資料。
//其中有方法返回至而且加上了@ResponseBody 這時對json字串的回撥。通過return返回物件,通過該註解可以將物件轉換成json格式字串,我們可以在前臺頁面重新獲得json字串陣列。
@RequestBody這是接收的請求物件將json陣列轉換為Items物件。
//json字串的形參
@RequestMapping(value = "/json.action")
public @ResponseBody Items json(@RequestBody Items items) {
System.out.println(items.getName());
return items;
}
使用Springmvc攔截器
- 首先建立一個類實現HandlerInterceptor介面。
package com.springmvc.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class Intrceptor1 implements HandlerInterceptor {
// 攔截之前
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
// TODO Auto-generated method stub
System.out.println("攔截器一前");
//返回true即可放行,否則不能通過
return true;
}
// 頁面渲染
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
// TODO Auto-generated method stub
System.out.println("攔截器後一");
}
// 攔截之後
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
System.out.println("頁面渲染一後");
}
}
- 這裡的攔截器需要在springmvc中進行配置
<mvc:interceptors>
<!-- 配置多個攔截器 -->
<mvc:interceptor>
<!--配置對映路徑,這裡兩個*指的是在根目錄下還有檔案需要攔截-->
<mvc:mapping path="/**" />
<!-- 自定義攔截器類 -->
<bean class="com.springmvc.interceptor.Intrceptor1" />
</mvc:interceptor>
<mvc:interceptor>
<!--攔截專案下的所有,兩個*表示如果有多個檔案路徑也可以進行攔截如:abd/def/efg.action-->
<mvc:mapping path="/**" />
<!-- 自定義攔截器類 -->
<bean class="com.springmvc.interceptor.Intrceptor2" />
</mvc:interceptor>
</mvc:interceptors>
- 一個專案中可以配置多個攔截器進行對專案進行攔截,我們在多個攔截器發揮的作用總結如下
總結:
preHandle按攔截器定義順序呼叫
postHandler按攔截器定義逆序呼叫
afterCompletion按攔截器定義逆序呼叫postHandler在攔截器鏈內所有攔截器飯或成功呼叫 afterCompletion只有preHandler返回true才呼叫