java 表单xml方式提交_SpringMVC(十四):SpringMVC 与表单提交(post/put/delete的用法);form属性设置encrypt='mutilpart/form-dat...

SpringMVC 与表单提交(post/put/delete的用法)

为了迎合Restful风格,提供的接口可能会包含:put、delete提交方式。在springmvc中实现表单以put、delete方式提交时,需要使用HiddenHttpMethodFilter过滤器。该过滤器的实现原理,默认在form表单内部定义一个hidden隐藏的标签,默认需要标签名为:_method,hidden标签的value为put或delete;过滤器会接收_method的hidden标签的value,如果发现存在_method的标签,并且value为put或delete时,会重新包装一个请求,请求类型会设置为_method标签的value值。进而实现将post方式提交表单转化为Delete或Put请求。

实现form post表当以put或delete方式提交的步骤包含以下:

idea下创建一个pom.xml工程:

3eb6e9f8e3542dcb20c84eb1ec60b75d.png

创建完后,工程结构如下:

561de977d0bb5b3225cb0439c9358ac4.png

设置web.xml配置如下:

/p>

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd">

springmvcdemo

/index

characterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

methodParam

_method

characterEncodingFilter

/*

myAppServletName

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

/WEB-INF/applicationContext.xml

1

myAppServletName

/

此时在WEB-INF下还需要新建一个applicationContext.xml配置文件,配置文件内容如下:

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

配置pom.xml引入依赖如下:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 <?xml version="1.0" encoding="UTF-8"?>

2

3

4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

5 4.0.0

6

7 com.dx.test

8 demo

9 1.0-SNAPSHOT

10 war

11

12 demo Maven Webapp

13

14 http://www.example.com

15

16

17 UTF-8

18 1.8

19 1.8

20

21 5.2.0.RELEASE

22

23 1.2

24 1.1.2

25

26

27

28

36

37 javax.servlet

38 javax.servlet-api

39 3.0.1

40 provided

41

42

43

44

45 commons-fileupload

46 commons-fileupload

47 1.4

48

49

50

51 commons-io

52 commons-io

53 2.5

54

55

56

57

58 org.springframework

59 spring-test

60 ${spring.version}

61 test

62

63

64

65

66 org.springframework

67 spring-webmvc

68 ${spring.version}

69

70

71

72

73 org.springframework

74 spring-core

75 ${spring.version}

76

77

78

79 org.springframework

80 spring-beans

81 ${spring.version}

82

83

84

85 org.springframework

86 spring-context

87 ${spring.version}

88

89

90

91 org.springframework

92 spring-context-support

93 ${spring.version}

94

95

96

97 org.springframework

98 spring-aop

99 ${spring.version}

100

101

102

103 org.springframework

104 spring-aspects

105 ${spring.version}

106

107

108

109 org.springframework

110 spring-tx

111 ${spring.version}

112

113

114

115 org.springframework

116 spring-web

117 ${spring.version}

118

119

120

121 org.springframework

122 spring-jdbc

123 ${spring.version}

124

125

126

127

128 org.aspectj

129 aspectjrt

130 1.8.13

131

132

133

134 org.aspectj

135 aspectjrt

136 1.8.13

137

138

139

140 cglib

141 cglib

142 3.2.5

143

144

145

146

147

148 org.codehaus.jackson

149 jackson-core-asl

150 1.5.2

151

152

153 org.codehaus.jackson

154 jackson-mapper-asl

155 1.5.2

156

157

158

159

160 javax.servlet

161 jstl

162 ${jstl.version}

163 runtime

164

165

166 taglibs

167 standard

168 ${standard.version}

169

170

171

172 junit

173 junit

174 4.11

175 test

176

177

178

179

180 demo

181

182

183

184 org.apache.tomcat.maven

185 tomcat7-maven-plugin

186 2.2

187

188 9090

189 /

190 UTF-8

191 tomcat7

192

193

194

195

196

197

198 maven-clean-plugin

199 3.1.0

200

201

202

203 maven-resources-plugin

204 3.0.2

205

206

207 maven-compiler-plugin

208 3.8.0

209

210

211 maven-surefire-plugin

212 2.22.1

213

214

215 maven-war-plugin

216 3.2.2

217

218

219 maven-install-plugin

220 2.5.2

221

222

223 maven-deploy-plugin

224 2.8.2

225

226

227

228

229

View Code

/WEB-INF/views/index.jsp内容如下:

Hello World!

/WEB-INF/views/article/list.jsp内容如下:

Spring MVC Demo

All Articles

Article IdArticle Name${article.id}${article.title}

/WEB-INF/views/article/show.jsp内容如下:

Spring MVC Demo

Post方式提交:

Id:Title:
Content:

Id:
Title:
Content:

Post包含上传文件提交:

Id:Title:
Content:
yourfile:

Id:
Title:
Content:
yourfile:

Put方式提交:

Id:Title:
Content:

Id:
Title:
Content:

Put包含上传文件提交:

Id:Title:
Content:
yourfile:

Id:
Title:
Content:
yourfile:

com.dx.test.model.ArticleModel.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.dx.test.model;importjava.io.Serializable;importjava.util.Date;/*** 文章内容*/

public class ArticleModel implementsSerializable {privateLong id;privateLong categoryId;privateString title;privateString content;privateDate createTime;privateString createUser;privateString createUserId;privateDate modifyTime;privateString modifyUser;privateString modifyUserId;publicArticleModel() {

}publicArticleModel(Long id, Long categoryId, String title, String content) {this.id =id;this.categoryId =categoryId;this.title =title;this.content =content;

}/***@returnthe id*/

publicLong getId() {returnid;

}/***@paramid the id to set*/

public voidsetId(Long id) {this.id =id;

}/***@returnthe categoryId*/

publicLong getCategoryId() {returncategoryId;

}/***@paramcategoryId the categoryId to set*/

public voidsetCategoryId(Long categoryId) {this.categoryId =categoryId;

}/***@returnthe title*/

publicString getTitle() {returntitle;

}/***@paramtitle the title to set*/

public voidsetTitle(String title) {this.title =title;

}/***@returnthe content*/

publicString getContent() {returncontent;

}/***@paramcontent the content to set*/

public voidsetContent(String content) {this.content =content;

}/***@returnthe createTime*/

publicDate getCreateTime() {returncreateTime;

}/***@paramcreateTime the createTime to set*/

public voidsetCreateTime(Date createTime) {this.createTime =createTime;

}/***@returnthe createUser*/

publicString getCreateUser() {returncreateUser;

}/***@paramcreateUser the createUser to set*/

public voidsetCreateUser(String createUser) {this.createUser =createUser;

}/***@returnthe createUserId*/

publicString getCreateUserId() {returncreateUserId;

}/***@paramcreateUserId the createUserId to set*/

public voidsetCreateUserId(String createUserId) {this.createUserId =createUserId;

}/***@returnthe modifyTime*/

publicDate getModifyTime() {returnmodifyTime;

}/***@parammodifyTime the modifyTime to set*/

public voidsetModifyTime(Date modifyTime) {this.modifyTime =modifyTime;

}/***@returnthe modifyUser*/

publicString getModifyUser() {returnmodifyUser;

}/***@parammodifyUser the modifyUser to set*/

public voidsetModifyUser(String modifyUser) {this.modifyUser =modifyUser;

}/***@returnthe modifyUserId*/

publicString getModifyUserId() {returnmodifyUserId;

}/***@parammodifyUserId the modifyUserId to set*/

public voidsetModifyUserId(String modifyUserId) {this.modifyUserId =modifyUserId;

}

@OverridepublicString toString() {return "ArticleModel{" +

"id=" + id +

", categoryId=" + categoryId +

", title='" + title + '\'' +

", content='" + content + '\'' +

'}';

}

}

View Code

com.dx.test.controller.HomeController.java

packagecom.dx.test.controller;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;

@Controllerpublic classHomeController {

@RequestMapping(value= "/", method =RequestMethod.GET)publicString index() {return "index";

}

}

com.dx.test.controller.ArticleController.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.dx.test.controller;2

3 importcom.dx.test.model.ArticleModel;4 importcom.dx.test.service.IArticleService;5 importorg.springframework.beans.factory.annotation.Autowired;6 importorg.springframework.http.MediaType;7 importorg.springframework.stereotype.Controller;8 importorg.springframework.ui.Model;9 importorg.springframework.ui.ModelMap;10 import org.springframework.web.bind.annotation.*;11 importorg.springframework.web.multipart.MultipartFile;12 importorg.springframework.web.multipart.MultipartHttpServletRequest;13 importorg.springframework.web.servlet.ModelAndView;14

15 importjavax.servlet.http.HttpServletRequest;16 importjavax.xml.ws.http.HTTPBinding;17 importjava.io.IOException;18 importjava.util.List;19 importjava.util.Map;20 importjava.util.Optional;21

22 @Controller23 @RequestMapping("/article")24 public classArticleController {25 @Autowired26 privateIArticleService articleService;27

28 @GetMapping("/list")29 public String queryList(Model model, @RequestHeader(value = "userId", required = false) String userId) {30 List articleModelList =articleService.queryList();31 model.addAttribute("articles", articleModelList);32 return "article/list";33 }34

35 @RequestMapping(value = "/{id}", method =RequestMethod.GET)36 public ModelAndView queryById(@PathVariable(value = "id", required = true) Long id) {37 ArticleModel articleModel =articleService.getById(id);38 ModelAndView mv = newModelAndView();39 mv.setViewName("article/show");40 mv.addObject("article", articleModel);41 returnmv;42 }43

44 @RequestMapping(value = "/update_with_post", method =RequestMethod.POST)45 public String update_with_post(@ModelAttribute(value = "article") ArticleModel article) {46 System.out.println(article);47 return "index";48 }49

50 @RequestMapping(value = "/update_with_post_file", method = RequestMethod.POST, consumes ={MediaType.MULTIPART_FORM_DATA_VALUE})51 public String update_with_post_file(@ModelAttribute(value = "article") ArticleModel article, @RequestBody MultipartFile file, HttpServletRequest request) {52 System.out.println(article);53 System.out.println(file);54 String id = request.getParameter("id");55 String title = request.getParameter("title");56 String content = request.getParameter("content");57 System.out.println(String.format("%s,%s,%s", id, title, content));58

59 return "index";60 }61

62 @RequestMapping(value = "/update_with_put", method =RequestMethod.PUT)63 public String update_with_put(@ModelAttribute(value = "article") ArticleModel article) {64 System.out.println(article);65 return "index";66 }67

68 @RequestMapping(value = "/update_with_put_file", method = RequestMethod.PUT, consumes ={MediaType.MULTIPART_FORM_DATA_VALUE})69 public String update_with_put_file(@ModelAttribute(value = "article") ArticleModel article, @RequestBody MultipartFile file, HttpServletRequest request) {70 System.out.println(article);71 System.out.println(file);72 String id = request.getParameter("id");73 String title = request.getParameter("title");74 String content = request.getParameter("content");75 System.out.println(String.format("%s,%s,%s", id, title, content));76

77 return "index";78 }79 }

View Code

给demo工程配置tomcat:

81dd9d3d8016efd7b739c0e15e9d4407.png

d8ed1c5d297514de79931845d4ee05fc.png

之后启动后可以从Console上打印信息,查看到端口,我这里端口是9090

1)Post(不含上传文件)方式提交

在http://localhost:9090/article/1页面中:

0d4f3ff349ba04efe87b66c27a9cefa5.png

对应show.jsp上的内容:

633de80863a57a2ef059e322fec92654.png

按照上边新建项目的web.xml、applicationContext.xml、ArticleController.java,提交'Post方式提交:,后台就能正常接收到参数,此时后台打印信息如下:

[DEBUG] POST "/article/update_with_post", parameters={masked}

[DEBUG] Mapped to com.dx.test.controller.ArticleController#update_with_post(ArticleModel)

ArticleModel{id=1, categoryId=null, title='算法与数据结构--综合提升篇(c++版)', content='文章内容', createTime=null, createUser='null', createUserId='null', modifyTime=null, modifyUser='null', modifyUserId='null'}

[DEBUG] View name'index', model {article=ArticleModel{id=1, categoryId=null, title='算法与数据结构--综合提升篇(c++版)', content='文章内容', createTime=null, createUser='null', createUserId='null', modifyTime=null, modifyUser='null', modifyUserId='null'}, org.springframework.validation.BindingResult.article=org.springframework.validation.BeanPropertyBindingResult: 0errors}

[DEBUG] Forwarding to [/WEB-INF/views/index.jsp]

[DEBUG] Completed200OK

[DEBUG] POST"/article/update_with_post", parameters={masked}

[DEBUG] Mapped to com.dx.test.controller.ArticleController#update_with_post(ArticleModel)

ArticleModel{id=1, categoryId=null, title='算法与数据结构--综合提升篇(c++版)', content='文章内容', createTime=null, createUser='null', createUserId='null', modifyTime=null, modifyUser='null', modifyUserId='null'}

[DEBUG] View name'index', model {article=ArticleModel{id=1, categoryId=null, title='算法与数据结构--综合提升篇(c++版)', content='文章内容', createTime=null, createUser='null', createUserId='null', modifyTime=null, modifyUser='null', modifyUserId='null'}, org.springframework.validation.BindingResult.article=org.springframework.validation.BeanPropertyBindingResult: 0errors}

[DEBUG] Forwarding to [/WEB-INF/views/index.jsp]

[DEBUG] Completed200 OK

备注:

1)post不含上传文件的意思是:form表单中不包含属性enctype="multipart/form-data"设置。

2)上边日志包含两次提交分别代表/WEB-INF/views/article/show.jsp页面中“Post方式提交:”下边的两个表单提交:第一个表单是jst方式的表单,第二个表单是纯html的表单。

2)Post(包含上传文件)方式提交

在http://localhost:9090/article/1页面中:

0020ba54475ab025ce058f75fb717a8c.png

对应show.jsp中内容:

2235c6f9f0767d61b94b04bbb2e477f7.png

因为是上传文件,而且handler方法使用HttpServletRequest参数,因此需要在pom.xml中引入上传文件依赖包,和servlet包:

javax.servlet

javax.servlet-api

3.0.1

provided

commons-fileupload

commons-fileupload

1.4

commons-io

commons-io

2.5

按照上边新建项目的web.xml、applicationContext.xml、ArticleController.java,提交'Post包含上传文件提交:',后台不能正常接收到参数,此时后台打印信息如下:

[DEBUG] POST "/article/update_with_post_file", parameters={}

[DEBUG] Mapped to com.dx.test.controller.ArticleController#update_with_post_file(ArticleModel, MultipartFile, HttpServletRequest)

ArticleModel{id=null, categoryId=null, title='null', content='null', createTime=null, createUser='null', createUserId='null', modifyTime=null, modifyUser='null', modifyUserId='null'}null

null,null,null[DEBUG] View name'index', model {article=ArticleModel{id=null, categoryId=null, title='null', content='null', createTime=null, createUser='null', createUserId='null', modifyTime=null, modifyUser='null', modifyUserId='null'}, org.springframework.validation.BindingResult.article=org.springframework.validation.BeanPropertyBindingResult: 0errors}

[DEBUG] Forwarding to [/WEB-INF/views/index.jsp]

[DEBUG] Completed200OK

[DEBUG] POST"/article/update_with_post_file", parameters={}

[DEBUG] Mapped to com.dx.test.controller.ArticleController#update_with_post_file(ArticleModel, MultipartFile, HttpServletRequest)

ArticleModel{id=null, categoryId=null, title='null', content='null', createTime=null, createUser='null', createUserId='null', modifyTime=null, modifyUser='null', modifyUserId='null'}null

null,null,null[DEBUG] View name'index', model {article=ArticleModel{id=null, categoryId=null, title='null', content='null', createTime=null, createUser='null', createUserId='null', modifyTime=null, modifyUser='null', modifyUserId='null'}, org.springframework.validation.BindingResult.article=org.springframework.validation.BeanPropertyBindingResult: 0errors}

[DEBUG] Forwarding to [/WEB-INF/views/index.jsp]

[DEBUG] Completed200 OK

备注:

1)post包含上传文件的意思是:form表单中包含属性enctype="multipart/form-data"设置。

2)上边日志包含两次提交分别代表/WEB-INF/views/article/show.jsp页面中“Post包含上传文件提交:”下边的两个表单提交:第一个表单是jst方式的表单,第二个表单是纯html的表单。

从上边日志可以看到,按照新建项目的方式提交“Post包含上传文件”的表单,后台是接收不到文件和article参数,而且request中也接收不到id,title,content参数。

此时,需要修改applicationContext.xml和web.xml才能实现后端接收到file和参数信息:

applicationContext.xml中需要注册bean“multipartResolver”:

web.xml需要加入以下filter“MultipartFilter”和引入listener“ContextLoaderListener”:

contextConfigLocation

/WEB-INF/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

MultipartFilter

org.springframework.web.multipart.support.MultipartFilter

multipartResolverBeanName

multipartResolver

MultipartFilter

/*

此时重新提交两个表单,后台打印日志如下:

[DEBUG] Using MultipartResolver 'multipartResolver' forMultipartFilter

[DEBUG] Part'file', size 9 bytes, filename='test.svg'[DEBUG] POST"/article/update_with_post_file", parameters={masked}

[DEBUG] Mapped to com.dx.test.controller.ArticleController#update_with_post_file(ArticleModel, MultipartFile, HttpServletRequest)

ArticleModel{id=1, categoryId=null, title='算法与数据结构--综合提升篇(c++版)', content='文章内容'}

org.springframework.web.multipart.commons.CommonsMultipartFile@7df79c2d1,算法与数据结构--综合提升篇(c++版),文章内容

[DEBUG] View name'index', model {article=ArticleModel{id=1, categoryId=null, title='算法与数据结构--综合提升篇(c++版)', content='文章内容'}, org.springframework.validation.BindingResult.article=org.springframework.validation.BeanPropertyBindingResult: 0errors}

[DEBUG] Forwarding to [/WEB-INF/views/index.jsp]

[DEBUG] Completed200OK

[DEBUG] Cleaning up part'file', filename 'test.svg'

[DEBUG] Using MultipartResolver'multipartResolver' forMultipartFilter

[DEBUG] Part'file', size 20098 bytes, filename='试用期考核报告-员工版.xlsx'[DEBUG] POST"/article/update_with_post_file", parameters={masked}

[DEBUG] Mapped to com.dx.test.controller.ArticleController#update_with_post_file(ArticleModel, MultipartFile, HttpServletRequest)

ArticleModel{id=1, categoryId=null, title='算法与数据结构--综合提升篇(c++版)', content='文章内容'}

org.springframework.web.multipart.commons.CommonsMultipartFile@448bda6e1,算法与数据结构--综合提升篇(c++版),文章内容

[DEBUG] View name'index', model {article=ArticleModel{id=1, categoryId=null, title='算法与数据结构--综合提升篇(c++版)', content='文章内容'}, org.springframework.validation.BindingResult.article=org.springframework.validation.BeanPropertyBindingResult: 0errors}

[DEBUG] Forwarding to [/WEB-INF/views/index.jsp]

[DEBUG] Completed200OK

[DEBUG] Cleaning up part'file', filename '试用期考核报告-员工版.xlsx'

3)Put(不含上传文件)方式提交

在http://localhost:9090/article/1页面中:

61c120d45c9f9af3fcefefc9a50e6862.png

对应show.jsp中内容:

cc2e0f80b59c1a75706e192ab4a64e67.png

按照上边新建项目的web.xml、applicationContext.xml、ArticleController.java,提交'Put方式提交:',后台不能正常接收到参数,此时提交后页面会跳转到405错误页面:

e66b0c4d9c1e31d596fbb02a95831b63.png

服务器打印日志信息如下:

DEBUG] POST "/article/update_with_put", parameters={masked}

[WARNING] Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

[DEBUG] Completed 405 METHOD_NOT_ALLOWED

[DEBUG] POST "/article/update_with_put", parameters={masked}

[WARNING] Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

[DEBUG] Completed 405 METHOD_NOT_ALLOWED

备注:

1)post不含上传文件的意思是:form表单中不包含属性enctype="multipart/form-data"设置。

2)上边日志包含两次提交分别代表/WEB-INF/views/article/show.jsp页面中“Put方式提交:”下边的两个表单提交:第一个表单是jst方式的表单,第二个表单是纯html的表单。

从错误页面和日志来查看,该错误原因是,springmvc默认不能以form post+方式实现put提交方式。

解决该错误的方案:

修改web.xml添加HiddenHtmlMethodFilter filter。

hiddenHttpMethodFilter

org.springframework.web.filter.HiddenHttpMethodFilter

hiddenHttpMethodFilter

myAppServletName

修改web.xml配置之后,重新提交两个表单,后台打印结果如下:

[DEBUG] PUT "/article/update_with_put", parameters={masked}

[DEBUG] Mapped to com.dx.test.controller.ArticleController#update_with_put(ArticleModel)

ArticleModel{id=1, categoryId=null, title='算法与数据结构--综合提升篇(c++版)', content='文章内容'}

[DEBUG] View name 'index', model {article=ArticleModel{id=1, categoryId=null, title='算法与数据结构--综合提升篇(c++版)', content='文章内容'}, org.springframework.validation.BindingResult.article=org.springframework.validation.BeanPropertyBindingResult: 0 errors}

[DEBUG] Forwarding to [/WEB-INF/views/index.jsp]

[DEBUG] Completed 200 OK

[DEBUG] PUT "/article/update_with_put", parameters={masked}

[DEBUG] Mapped to com.dx.test.controller.ArticleController#update_with_put(ArticleModel)

ArticleModel{id=1, categoryId=null, title='算法与数据结构--综合提升篇(c++版)', content='文章内容'}

[DEBUG] View name 'index', model {article=ArticleModel{id=1, categoryId=null, title='算法与数据结构--综合提升篇(c++版)', content='文章内容'}, org.springframework.validation.BindingResult.article=org.springframework.validation.BeanPropertyBindingResult: 0 errors}

[DEBUG] Forwarding to [/WEB-INF/views/index.jsp]

[DEBUG] Completed 200 OK

4)Put(包含上传文件)方式提交

在http://localhost:9090/article/1页面中:

1488fe1097024e9850b19b1e4fe1fdf3.png

对应show.jsp中内容:

0594b3551cfb645894c28a74467aa0f2.png

因为是上传文件,而且handler方法使用HttpServletRequest参数,因此需要在pom.xml中引入上传文件依赖包,和servlet包:

javax.servlet

javax.servlet-api

3.0.1

provided

commons-fileupload

commons-fileupload

1.4

commons-io

commons-io

2.5

按照上边新建项目的web.xml、applicationContext.xml、ArticleController.java,提交'Put方式提交:',后台不能正常接收到参数,此时提交后页面会跳转到405错误页面:

e66b0c4d9c1e31d596fbb02a95831b63.png

服务器打印日志信息如下:

DEBUG] POST "/article/update_with_put_file", parameters={masked}

[WARNING] Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

[DEBUG] Completed 405 METHOD_NOT_ALLOWED

[DEBUG] POST "/article/update_with_put_file", parameters={masked}

[WARNING] Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

[DEBUG] Completed 405 METHOD_NOT_ALLOWED

备注:

1)post不含上传文件的意思是:form表单中不包含属性enctype="multipart/form-data"设置。

2)上边日志包含两次提交分别代表/WEB-INF/views/article/show.jsp页面中“Put方式提交:”下边的两个表单提交:第一个表单是jst方式的表单,第二个表单是纯html的表单。

从错误页面和日志来查看,该错误原因是,springmvc默认不能以form post+方式实现put提交方式。

解决该错误的方案:

1)为了实现put方式提交修改web.xml,添加HiddenHtmlMethodFilter filter;

2)为了实现上传文件需要修改web.xml,添加filter“MultipartFilter”和引入listener“ContextLoaderListener”;

3)为了实现上传文件需要修改applicaitonContext.xml,注册bean“multipartResolver”。

web.xml修改后内容为:

/p>

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd">

springmvcdemo

/index

contextConfigLocation

/WEB-INF/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

MultipartFilter

org.springframework.web.multipart.support.MultipartFilter

multipartResolverBeanName

multipartResolver

MultipartFilter

/*

hiddenHttpMethodFilter

org.springframework.web.filter.HiddenHttpMethodFilter

hiddenHttpMethodFilter

myAppServletName

characterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

methodParam

_method

characterEncodingFilter

/*

myAppServletName

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

/WEB-INF/applicationContext.xml

1

myAppServletName

/

applicationContext.xml修改后内容为:

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

修改后,重新提交两个表单,此时服务端打印日志如下:

[DEBUG] Using MultipartResolver 'multipartResolver' for MultipartFilter

[DEBUG] Part 'file', size 1181 bytes, filename='ImageVO.java'

[DEBUG] PUT "/article/update_with_put_file", parameters={masked}

[DEBUG] Mapped to com.dx.test.controller.ArticleController#update_with_put_file(ArticleModel, MultipartFile, HttpServletRequest)

ArticleModel{id=1, categoryId=null, title='算法与数据结构--综合提升篇(c++版)', content='文章内容'}

org.springframework.web.multipart.commons.CommonsMultipartFile@62032272

1,算法与数据结构--综合提升篇(c++版),文章内容

[DEBUG] View name 'index', model {article=ArticleModel{id=1, categoryId=null, title='算法与数据结构--综合提升篇(c++版)', content='文章内容'}, org.springframework.validation.BindingResult.article=org.springframework.validation.BeanPropertyBindingResult: 0 errors}

[DEBUG] Forwarding to [/WEB-INF/views/index.jsp]

[DEBUG] Completed 200 OK

[DEBUG] Cleaning up part 'file', filename 'ImageVO.java'

[DEBUG] Using MultipartResolver 'multipartResolver' for MultipartFilter

[DEBUG] Part 'file', size 1732 bytes, filename='UploadImgParam.java'

[DEBUG] PUT "/article/update_with_put_file", parameters={masked}

[DEBUG] Mapped to com.dx.test.controller.ArticleController#update_with_put_file(ArticleModel, MultipartFile, HttpServletRequest)

ArticleModel{id=1, categoryId=null, title='算法与数据结构--综合提升篇(c++版)', content='文章内容'}

org.springframework.web.multipart.commons.CommonsMultipartFile@32d8a82d

1,算法与数据结构--综合提升篇(c++版),文章内容

[DEBUG] View name 'index', model {article=ArticleModel{id=1, categoryId=null, title='算法与数据结构--综合提升篇(c++版)', content='文章内容'}, org.springframework.validation.BindingResult.article=org.springframework.validation.BeanPropertyBindingResult: 0 errors}

[DEBUG] Forwarding to [/WEB-INF/views/index.jsp]

[DEBUG] Completed 200 OK

[DEBUG] Cleaning up part 'file', filename 'UploadImgParam.java'

备注:该web.xml和applicationContext.xml的配置方式,可以支持post方式提交、post+上传文件提交、put提交、put+上传文件提交。因此,推荐使用该配置。

总结:

1)form属性设置encrypt='mutilpart/form-data'时,如何正确配置web.xml才能以put方式提交表单?

1)为了实现put方式提交修改web.xml,添加HiddenHtmlMethodFilter filter;

2)为了实现上传文件需要修改web.xml,添加filter“MultipartFilter”和引入listener“ContextLoaderListener”;

3)为了实现上传文件需要修改applicaitonContext.xml,注册bean“multipartResolver”。

备注:

1)如果multipartResolver bean配置的是org.springframework.web.multipart.commons.CommonsMultipartResolver,因为CommonsMultipartResolver依赖commons-fileupload.jar和commons-io.jar,所以需要pom.xml引入这两个包。

2)handler方法使用HttpServletRequest request作为参数,则需要在pom.xml中引入servlet包。

2)留3个问题:

1)ContextLoaderListener在Web.xml中配置与不配置的区别:

为什么上边上传文件方式需要配置它,不配置会导致上传文件后端接收不到;

它在上传文件中是不是必须配置的吗?

2)web.xml 中的 ContextLoaderListener 是干什么用的?

3)web.xml 中的 DispatcherServlet 是如何加载的,它其什么作用?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值