Spring mvc框架知識

  1. spring mvc框架與fileupload文件上傳
    項目需要導入的jar包
    項目jar包

fileupload需要配置的spring-servletxml文件信息

<!-- 處理文件上傳 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="gbk" /> <!-- 默認編碼 (ISO-8859-1) -->
        <property name="maxInMemorySize" value="10240" /> <!-- 最大內存大小 (10240)-->
        <property name="uploadTempDir" value="/upload/" /> <!-- 上傳後的目錄名 (WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE) -->
        <property name="maxUploadSize" value="-1" /> <!-- 最大文件大小,-1爲無限止(-1) -->
    </bean>

需要的代碼信息

import java.io.File;
import java.util.Date;

import javax.servlet.ServletContext;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.commons.CommonsMultipartFile;


//聲明這是一個bean:爲控制類的bean對象
@Controller
public class FileUploadController implements ServletContextAware {

    private ServletContext servletContext;

    public void setServletContext(ServletContext context) {
        this.servletContext  = context;
    }

    //表示如果form表單訪問的是upload.do並且爲post請求則使用這個方法
    @RequestMapping(value="/upload.do", method = RequestMethod.POST)
    //聲明成一個接受上傳的方法
    public String handleUploadData(String name,@RequestParam("file") CommonsMultipartFile file){
        if (!file.isEmpty()) {
               String path = this.servletContext.getRealPath("/tmp/");  //獲取本地存儲路徑
               System.out.println(path);
               //得到文件的初始名稱
               String fileName = file.getOriginalFilename();
               String fileType = fileName.substring(fileName.lastIndexOf("."));
               System.out.println(fileType); 
               File file2 = new File(path,new Date().getTime() + fileType); //新建一個文件
               try {
                    file.getFileItem().write(file2); //將上傳的文件寫入新建的文件中
               } catch (Exception e) {
                    e.printStackTrace();
               }
               //重定向到文件上傳成功頁面
               return "redirect:upload_ok.jsp";
            }else{
                //重定向到文件上傳失敗頁面
                return "redirect:upload_error.jsp";
            }
    }
}


上面的註解需要在springmvc-servlet.xml配置一些信息,用於服務器能夠解析收到

    <!-- 對web包中的所有類進行掃描,以完成Bean創建和自動依賴注入的功能 -->
    <context:component-scan base-package="com.xingyao" />


    <mvc:annotation-driven />  <!-- 支持spring3.0新的mvc註解 -->
    <!-- 啓動Spring MVC的註解功能,完成請求和註解POJO的映射 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="cacheSeconds" value="0" />
        <property name="messageConverters">
            <list>
                <bean
                    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
            </list>
        </property>
    </bean>




----------

spring與ajax
1. 導入的jar包見上面的圖片
2. jsp頁面的代碼
這裏寫圖片描述
3.servlet層的代碼
這裏寫圖片描述


spring與過濾器

  1. spring-servlet.xml的配置
    這裏寫圖片描述
  2. servelt層代碼的寫法
    這裏寫圖片描述

    還有一些方法

public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("最後執行!!!一般用於釋放資源!!");

    }

    public void postHandle(HttpServletRequest request,HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("Action執行之後,生成視圖之前執行!!");
    }

這就是spring的一些應用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章