在看springMVC之前,就聽說現在很多公司用SSH的並不多,但是springMVC的不少,感覺springMVC應該是一個比較好用的架構,但是具體怎麼用多好用還是沒有概念,通過這次簡單的學習,發現的確有他的很多優勢,下面以springMVC上次為例,寫一個簡單的springMVC執行個體,稍微感受一下springMVC 的好處。
首先配置web.xml檔案:
<servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath*:/config/springAnnotation-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup> </servlet> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
springMVC的配置相對比較簡單,和之前的配置spring區別不大。
下面配置springMVC檔案:
<!-- 註解掃描包 --><context:component-scan base-package="com.tgb.web.controller.annotation.upload"/><!-- 開啟註解 --><mvc:annotation-driven/><!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean><bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>--><!-- 靜態資源訪問 --><mvc:resources location="/img/" mapping="/img/**"/><mvc:resources location="/js/" mapping="/js/**"/><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean> <!-- 上傳攔截,如最大上傳值及最小上傳值 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- set the max upload size100MB --> <property name="maxUploadSize"> <value>800000</value> </property> <property name="maxInMemorySize"> <value>4096</value> </property> </bean> </beans>
因為web裡設定springMVC攔截的是所有的網址,所以,這裡需要對圖片和js等靜態檔案進行配置,不然無法識別。
同時,配置需要上傳檔案的最大值和最小值,當然,在程式裡也可以用代碼配置,但是,不提倡。
這裡配置註解,個人認為springMVC的註解很好用,簡單易用。
配置好之後,我們開始寫代碼:
@RequestMapping("/upload1")public String upload2(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException{//建立一個通用的多部分解析器. CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); //判斷 request 是否有檔案上傳,即多部分請求... if(multipartResolver.isMultipart(request)){ //判斷 request 是否有檔案上傳,即多部分請求... MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)(request);Iterator<String> iter = multiRequest.getFileNames();while(iter.hasNext()){MultipartFile file = multiRequest.getFile(iter.next());String fileName = "demoUpload" +file.getOriginalFilename();String path ="F:/" +fileName;File localFile = new File(path);file.transferTo(localFile);}}return "/success";}
最後是前台介面:
<h>上傳檔案</h><form name="userForm" action="/springMVC1/file/upload1" method="post" enctype="multipart/form-data">選擇檔案:<input type="file" name="file"><input type="submit" value="上傳"></form>
至此,一個簡單上傳檔案就完成了,