1. 程式人生 > >第十章 SpringMVC應用Ⅱ

第十章 SpringMVC應用Ⅱ

SpringMVC攔截器

         1. 建立攔截類,實現HandlerInterceptor介面

package com.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/*
 * 	建立攔截器類,實現HandlerInterceptor
 */
public class TestInterceptor implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		System.out.println("處理請求之後.....");
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		System.out.println("處理請求中.....");
		
	}

	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2) throws Exception {
		System.out.println("處理請求之前.....");
		// true 是指將請求進行釋放傳遞
		return true;
	}

}

         2. 在配置檔案中配置攔截器

<!-- 配置攔截器 -->
	<mvc:interceptors>
		<!-- 注入攔截器類,攔截所有請求 -->
		<bean class="com.interceptor.TestInterceptor"></bean>
		<mvc:interceptor>
			<!-- 攔截指定路徑下的請求 -->
			<mvc:mapping path="/test/one.do"/>		
			<bean class="com.interceptor.TestInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>

SpringMVC上傳

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>檔案上傳</title>
  </head>
  
  <body>
  	<form action="test/upload.do" enctype="multipart/form-data" method="post">
  		說明: <input type="text" name="show" /><br /><br />
  		檔案: <input type="file" name="file" value="" /><br /><br />
  		<input type="submit" />
  	</form>
  </body>
</html>
<?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-3.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
	">

	<context:component-scan base-package="com.controller"></context:component-scan>
	
	<bean id="urlBasedViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- SpringMVC檔案上傳配置:id為固定值 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8"></property>
		<property name="maxUploadSize" value="1024000"></property>
	</bean>

	
</beans>
package com.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/test")
public class Test {
	@RequestMapping("/upload.do")
	public String upload(String show ,@RequestParam("file")MultipartFile file,HttpServletRequest request){
		// 獲取專案的絕對路徑拼接檔名字
		String filePath = request.getSession().getServletContext().getRealPath(File.separator)+file.getOriginalFilename();
		// 將檔案寫到指定路徑下的檔案中
		try {
			file.transferTo(new File(filePath));
		} catch (IllegalStateException e) {
			System.err.println("檔案引數錯誤");
		} catch (IOException e) {
			System.err.println("檔案操作流失敗");
		}
		// 獲取會話,寫入檔名字
		HttpSession session = request.getSession();
		session.setAttribute("file",file.getOriginalFilename());
		return "ok";
	}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>載入圖片</title>
  </head>
  
  <body>
  	<h1>檔案上傳成功</h1>
  	<!-- 根據URL路徑訪問圖片 -->
  	<img alt="檔案載入失敗" src="<%=basePath %>${file}" />
  </body>
</html>