- 浏览: 268153 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
hongxingyaru_d5b032:
但是其中“这个Accept和Request Headers中的 ...
基于Spring MVC的Web应用开发(7) - Headers -
hongxingyaru_d5b032:
学习到了,感谢分享!
基于Spring MVC的Web应用开发(7) - Headers -
bo_hai:
总结的不错。
基于Spring MVC的Web应用开发(7) - Headers -
fendou3754:
那你也说一下spring-asm重新打包以后的源码怎么获取,尽 ...
spring将spring-asm重新打包原因 -
di1984HIT:
谢谢啊~学习了~~
JMS、AMQP实例讲解
上一篇文章介绍了Spring MVC如何处理静态资源文件,本文讲解如何使用Spring MVC做文件上传,附带深入一下Spring MVC的ModelAndView。增加一个Controller,叫FileUploadController:
package org.springframework.samples.mvc.fileupload; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/fileupload") public class FileUploadController { @RequestMapping(method=RequestMethod.GET) public void fileUploadForm() { } }
这个类和HelloWorld中的Controller类就有点差别了,首先类名上加入了@RequestMapping注解,这样在做HandlerMapping时,SpringMVC会将类名和方法名的@RequestMapping连接起来,而本例方法名前并没有具体路径(当然也可以写),因此最终映射的URL还是"/fileupload",另外一点就是在方法级@RequestMapping的注解增加了一个RequestMothod.GET,意思是只有以GET方式提交的"/fileupload"URL请求才会进入fileUploadForm()方法,其实根据HTTP协议,HTTP支持一系列提交方法(GET,POST,PUT,DELETE),同一个URL都可以使用这几种提交方式,事实上SpringMVC正是通过将同一个URL的不同提交方法对应到不同的方法上达到RESTful。
访问http://localhost:8080/web/fileupload,后台报错:
2012-03-20 19:12:44.557:WARN::/web/fileupload javax.servlet.ServletException: Circular view path [fileupload]: would dispatch back to the current handler URL [/web/fileupload] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.) at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:292) ...
正如Hint中指出的,没有指定一个View,大家可能会有疑问,HelloWorld中不是可以将结果返回到浏览器么?注意HelloWorld中的@ResponseBody跟View没有任何关系,HelloWorld中其实也是返回了一个ModelAndView,但这个View为null,并且在返回前,已经将结果通过HttpServletResponse发送给浏览器了。
那么怎么指定一个View呢?通常我们会想到一个默认的View,即如果没有写特殊的View,所有的结果都将转到这个默认的View上,最后将这个View推到浏览器展示。在servlet-context.xml中增加一个配置:
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean>
配置了一个Bean,对应的类为InternalResourceViewResolver,并设置了prefix属性为"/WEB-INF/views",suffix属性为".jsp",这个类为视图解析器(view resolver),支持InternalResourceView(比如Servlet和JSP)及其子类如JstlView,默认的是InternalResourceView,如果有JSTL API存在的话,就是JstlView。
现在有一个默认的View了,这个View将view名字解析到/WEB-INF/views/目录下对应的jsp文件。
哪里有view名字?当带有@RequestMapping的方法返回void时,@RequestMapping映射的URL路径即view名字。
所以当使用GET方式访问http://localhost:8080/web/fileupload时,最终会找WEB-INF/views/目录下有没有fileupload.jsp文件,有则将此jsp文件显示在浏览器上,没有则报如下错误:
ERROR: PWC6117: File "/Users/stephansun/Documents/workspace/samples/samples-web/src/main/webapp/WEB-INF/views/fileupload.jsp" not found
fileUploadForm.jsp文件为:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>fileupload | mvc-showcase</title> <link href="<c:url value="/resources/form.css" />" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="<c:url value="/resources/jquery/1.6/jquery.js" />"></script> <script type="text/javascript" src="<c:url value="/resources/jqueryform/2.8/jquery.form.js" />"></script> </head> <body> <div id="fileuploadContent"> <h2>File Upload</h2> <p> See the <code>org.springframework.samples.mvc.fileupload</code> package for the @Controller code </p> <form id="fileuploadForm" action="fileupload" method="POST" enctype="multipart/form-data" class="cleanform"> <div class="header"> <h2>Form</h2> <c:if test="${not empty message}"> <div id="message" class="success">${message}</div> </c:if> </div> <label for="file">File</label> <input id="file" type="file" name="file" /> <p><button type="submit">Upload</button></p> </form> </div> </body> </html>
上传页面正确显示后,我们需要一个方法处理上传请求,processUpload,现在FileUploadController看起来是这样的:
package org.springframework.samples.mvc.fileupload; import java.io.IOException; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; 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.multipart.MultipartFile; @Controller @RequestMapping("/fileupload") public class FileUploadController { @RequestMapping(method=RequestMethod.GET) public void fileUploadForm() { } @RequestMapping(method=RequestMethod.POST) public void processUpload(@RequestParam MultipartFile file, Model model) throws IOException { model.addAttribute("message", "File '" + file.getOriginalFilename() + "' uploaded successfully"); } }
这里出现了一个新注解@RequestParam,先看一下JSP页面的代码片段:
<input id="file" type="file" name="file" />
input框提交的参数名就是file,@RequestParam注解自动将POST提交的参数中名为file的封装到一个MultipartFile类中,一切都在开发人员眼皮底下做好了,大大减少了开发人员的代码量。现在我们在上传页面选择一个文件上传,后台报错,看报错日志:
java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured? at org.springframework.util.Assert.notNull(Assert.java:112)
说"是不是没配置MultipartResolver?",恩,还没有配,所有在servlet-context.xml中加上这么一段配置:
<!-- Only needed because we require fileupload in the org.springframework.samples.mvc.fileupload package --> <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
这个Bean也是一个视图解析器(view resolver)。
完整的XML:
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- Imports user-defined @Controller beans that process client requests --> <beans:import resource="controllers.xml" /> <!-- You have to add this because you had a <resources/> declare --> <mvc:annotation-driven/> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <!-- Only needed because we require fileupload in the org.springframework.samples.mvc.fileupload package --> <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> </beans:beans>
然后再次上传文件,页面显示:
File 'a.js' uploaded successfully
上传成功,最后我们分析一下FileUploadController中的processUpload方法:
@RequestMapping(method=RequestMethod.POST) public void processUpload(@RequestParam MultipartFile file, Model model) throws IOException { model.addAttribute("message", "File '" + file.getOriginalFilename() + "' uploaded successfully"); }
Model就是MVC模式中的M,Model层封装了要传递给View层显示的值。
最后更新一下pom.xml文件,添加两个依赖:
<!-- File Upload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.0.1</version> </dependency>
好像前面写的代码根本没有涉及到这两个jar包啊,为什么要引入?
之前我写道
底层真正由谁来做的呢?我们加入了multipartResolver这个Bean,它对应的类为
org.springframework.web.multipart.commons.CommonsMultipartResolver,
看看这个类的源代码,import中赫然写着:
import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileUpload; import org.apache.commons.fileupload.FileUploadBase; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.servlet.ServletFileUpload;
因此SpringMVC封装了commons-fileupload上传组件,真正起上传作用的还是commons-fileupload-1.2.2.jar这个jar包里面的类。
评论
谢谢!我很少上QQ,可以私信给我哇。
发表评论
-
《Pro Spring Integration》读书笔记
2012-06-02 11:30 0a -
基于Spring MVC的Web应用开发(12) - Form
2012-03-31 14:50 5860本节介绍SpringMVC中的表单,demo演示访问一个表单提 ... -
基于Spring MVC的Web应用开发(11) - Views
2012-03-31 13:13 8737在FileUpload一文中,我们初步了解了SpringMVC ... -
基于Spring MVC的Web应用开发(10) - Validation
2012-03-31 11:36 3108本文介绍SpringMVC的验证(validation),在C ... -
基于Spring MVC的Web应用开发(9) - Exceptions
2012-03-31 10:55 1713本文介绍SpringMVC中的异常处理,@Controller ... -
基于Spring MVC的Web应用开发(8) - Convert
2012-03-31 09:25 3607本文介绍SpringMVC中的Convert,Convert是 ... -
基于Spring MVC的Web应用开发(7) - Headers
2012-03-30 15:01 27264本文接上一篇文章,介 ... -
基于Spring MVC的Web应用开发(6) - Response
2012-03-30 10:56 36000本文讲解Spring MVC的Response,深入了解一下@ ... -
基于Spring MVC的Web应用开发(5) - Redirect
2012-03-26 15:10 17441本文介绍Spring MVC中的重定向(Redirect),先 ... -
基于Spring MVC的Web应用开发(3) - Resources
2012-03-20 16:22 6549上一篇介绍了在基于Spring MVC的Web项目中加入日志, ... -
基于Spring MVC的Web应用开发(2) - Log
2012-03-19 11:55 3534上一篇文章我们使用SpringMVC搭建了一个简单WEB项目 ... -
基于Spring MVC的Web应用开发(1) - HelloWorld
2012-03-16 07:21 18228MVC架构 MVC是模型(model),视图(view),控 ... -
spring-oxm中unmashaller疑似bug
2012-03-08 10:21 2488spring-integration中测试XML Transf ... -
spring将spring-asm重新打包原因
2012-03-06 13:41 8184以前就发现通过eclipse上面的m2e插件不能将spring ... -
Spring底层使用SAX解析XML配置文件
2012-02-29 01:28 6049最近在研究Spring源代码,在梳理spring-beans时 ... -
在Spring 3.1.0 M2中配置Hibernate事务失效?
2011-10-22 00:38 2126SpringSource与2011年6月8号发布了Spring ...
相关推荐
antlr-2.7.6rc1.jar(下面的略.jar) asm.jar asm-2.2.3.jar ...spring-webmvc sqljdbc struts velocity-1.5 velocity-tools-1.3 xapool xerces-2.6.2 xml-apis (我做毕业设计时,用到的。需要的话拿去用)
commons-fileupload-1.3.3.jar commons-logging-1.1.1.jar druid-1.0.9.jar fastjson-1.2.6.jar hamcrest-all-1.3.jar junit-4.12.jar log4j-1.2.16.jar mybatis-3.1.1.jar mybatis-spring-1.2.3.jar mysql-...
quartz-1.2.3.jar shiro-spring-1.2.3.jar shiro-web-1.2.3.jar slf4j-api-1.7.7.jar spring-aop-3.2.6.RELEASE.jar spring-beans-3.2.6.RELEASE.jar spring-context-3.2.6.RELEASE.jar spring-context-support-...
springmvc+spring+mybatis整合所需jar aopalliance-1.0.jar ...jdbc-3.1.1.RELEASE.jar spring-test-3.2.0.RELEASE.jar spring-tx-3.1.1.RELEASE.jar spring-web-3.2.0.RELEASE.jar spring-webmvc-3.2.0.RELEASE.jar
aspectjrt.jar aspectjweaver.jar ...spring-webmvc-struts.jar spring-webmvc.jar spring.jar standard.jar struts-spring-other-lib.rar struts.jar unittest.jar xercesImpl.jar xmlParserAPIs.jar
spring-web.jar(必须) :这个jar 文件包含Web 应用开发时,用到Spring 框架时所需的核心类,包括自动载入Web Application Context 特性的类、Struts 与JSF 集成类、文件上传的支持类、Filter 类和大量工具辅助类。...
spring-tomcat-weaver.jar spring-toplink.jar spring-web.jar spring-webmvc.jar standard.jar struts.jar swarmcache-1.0rc2.jar velocity-1.5.jar velocity-tools-view-1.3.jar xalan.jar xerces-2.6.2.jar xml-...
spring-expression-4.1.8.RELEASE.jar,spring-web-4.1.8.RELEASE.jar,spring-webmvc-4.1.8.RELEASE.jar,junit-3.8.1.jar,log4j-1.2.12.jar,logkit-1.0.1.jar,servlet-api-2.3.jar,aopalliance-1.0.jar,...
以下是jar包列表 activation-1.1.jar ...spring-webmvc-3.1.1.RELEASE.jar standard-1.1.2.jar stax-api-1.0-2.jar transaction-api-1.1.jar validation-api-1.0.0.GA.jar xmemcached-1.3.7.jar xml-apis-1.0.b2.jar
aopalliance-1.0.jar asm-3.3.1.jar aspectjweaver-1.7.1.jar c3p0-0.9.1.jar cglib-2.2.2.jar commons-dbcp-1.2.2.jar ...spring-webmvc-3.2.0.RELEASE.jar standard.jar validation-api-1.0.0.GA.jar
antlr-2.7.7.jar aopalliance-1.0.jar asm-3.3.jar asm-commons-3.3.jar commons-fileupload-1.2.2.jar commons-io-2.4.jar commons-lang3-3.1.jar commons-logging-1.1.1.jar dom4j-1.6.1.jar druid-1.0.1.jar ...
基于SpringMVC+Hibernate4的考勤管理系统+.zip 项目环境 系统:Windows7 X64位系统 ...spring-web-4.0.0.RELEASE.jar Web 应用开发时,用到Spring 框架时所需的核心类 spring-webmvc-4.0.0.RELEASE
commons-fileupload-1.3.1.jar commons-httpclient-3.1.jar commons-io-2.4.jar commons-lang-2.6.jar commons-lang3-3.3.2.jar commons-logging-1.1.1.jar commons-net-3.5.jar commons-pool-1.6.jar ...
spring-webmvc-4.1.9.RELEASE.jar standard-1.1.2.jar stax-api-1.0.1.jar thumbnailator-0.4.2.jar UserAgentUtils-1.13.jar validation-api-1.1.0.Final.jar xercesImpl-2.8.1.jar xml-apis-1.3.04.jar xml-apis-...
spring-webmvc-4.3.11.RELEASE.jar 【数据校验】 classmate-0.8.0.jar hibernate-validator-5.0.0.CR2.jar hibernate-validator-annotation-processor-5.0.0.CR2.jar jboss-logging-3.1.1.GA.jar validation...
教学-传智播客-项目视频经典之作巴巴运动网...\spring-framework-2.5.5\dist\modules\spring-webmvc-struts.jar MySQL: mysql-connector-java-5.1.16-bin.jar 运行: http://localhost:8090/control/center/main.do
教学-传智播客-项目视频经典之作巴巴运动网...\spring-framework-2.5.5\dist\modules\spring-webmvc-struts.jar MySQL: mysql-connector-java-5.1.16-bin.jar 运行: http://localhost:8090/control/center/main.do
webmvc.jar spring-portlet.jar struts.jar commons-fileupload.jar commons-httpclient.jar freemarker.jar jasperreports-1.3.3.jar commons-io.jar portlet-api.jar jxl.jar itext...
教学-传智播客-项目视频经典之作巴巴运动网...\spring-framework-2.5.5\dist\modules\spring-webmvc-struts.jar MySQL: mysql-connector-java-5.1.16-bin.jar 运行: http://localhost:8090/control/center/main.do
spring-webmvc-4.0.0.RELEASE.jar 【数据校验】 hibernate-validator-5.0.0.cr2.jar hibernate-validator-annotation-processor-5.0.0.cr2.jar classmate-0.8.0.jar jboss-logging-3.1.1.GA.jar validation-...