[英]Spring Webflow 2.4.5 multipart/form-data binding problems
I have the multipart resolver configured for the project: 我为项目配置了多部分解析器:
@EnableWebMvc
@ComponentScan({"com.dg"})
@Configuration
@SuppressWarnings("deprecation")
public class AppConfig extends AbstractFlowConfiguration {
@Bean
public CommonsMultipartResolver fileResolver() {
CommonsMultipartResolver cr = new CommonsMultipartResolver();
cr.setDefaultEncoding("UTF-8");
cr.setMaxUploadSize(26214400);
return cr;
}
}
I am using a serializable object as the model: 我使用可序列化对象作为模型:
public class NewRequestModel implements Serializable {
private static final long serialVersionUID = -5372803991673351564L;
private String entry;
private transient MultipartFile attachment;
... // getters and setters
}
When the page loads, I initialize the model: 页面加载后,我将初始化模型:
WEBFLOW 网络流量
<var name="requestModel" class="com.dg.model.request.NewRequestModel" />
<on-start>
<evaluate expression="newRequestAction.initTestRequest" />
</on-start>
ACTION 行动
public Event initTestRequest(RequestContext context) {
NewRequestModel model = (NewRequestModel)context.getFlowScope().get("requestModel");
if (model==null) {
model = new NewRequestModel();
}
context.getFlowScope().put("requestModel", model);
return new Event(this, "DONE");
}
I bind the values in the JSP: 我在JSP中绑定值:
<form:form id="actionForm" method="POST" modelAttribute="requestModel" enctype="multipart/form-data">
<div class="panel-heading">
Test Request
</div>
<div class="panel-body">
<div class="form-group">
<form:input path="entry" />
</div>
<div class="form-group">
<form:input type="file" path="attachment" />
</div>
<div>
<button type="button" class="btn btn-primary" onclick="app.submitEvent({formSelector: '#actionForm',event: 'submitAndValidate'});">
Submit Request
</button>
</div>
</div>
</form:form>
When the button is clicked, the form is submitted with event "submitAndValidate" 单击按钮后,将提交带有事件“ submitAndValidate”的表单
<view-state id="start" view="request/test/test-request" model="requestModel">
<transition on="submitAndValidate" to="submitAndValidate" />
</view-state>
<action-state id="submitAndValidate">
<evaluate expression="newRequestAction.testRequest" />
<transition to="start" />
</action-state>
When I debug into the Action class, testRequest method, The model does not retain the values submitted with the form. 当我调试Action类的testRequest方法时,模型不保留与表单一起提交的值。
public Event testRequest(RequestContext context) {
NewRequestModel model = (NewRequestModel)context.getFlowScope().get("requestModel");
String entry= model.getEntry();
MultipartFile file = model.getAttachment();
if (entry==null) {
// is null when enctype is included on form
// is populated when enctype is not included on form
}
if (file==null) {
// always null for some reason
}
return new Event(this, "DONE");
}
If I remove enctype="multipart/form-data" from the form, the binding works for the "entry" variable. 如果我从表单中删除enctype =“ multipart / form-data”,则该绑定适用于“ entry”变量。 Of course, this won't allow for uploading a document.
当然,这将不允许上传文档。 So I need to be able to include the enctype on the form.
因此,我需要能够在表单上包含enctype。
I've gone over this and looked up answers and read the guides and I just am not seeing why this would fail. 我已经看完了,查找了答案并阅读了指南,但我不明白为什么会失败。 I appreciate any help in finding the offending configuration/code.
感谢您在查找有问题的配置/代码方面的帮助。
try adding action to your form (crsf attribute are optional): 尝试向表单添加操作(crsf属性是可选的):
<form:form action="${flowExecutionUrl}&${_csrf.parameterName}=${_csrf.token}" modelAttribute="requestModel" enctype="multipart/form-data">
and using a simple input: 并使用简单的输入:
<input type="file" name="attachment" id="attachment"/>
and a simple submit: 和一个简单的提交:
<input type="submit" id="submitAndValidate" name="_eventId_ submitAndValidate" value="Submit Request">
The problem was in my MultipartResolver bean configuration. 问题出在我的MultipartResolver bean配置中。 Spring is looking specifically for a bean named "multipartResolver."
Spring专门在寻找名为“ multipartResolver”的bean。 Therefore I had 2 options to fix this:
因此,我有2个解决方案:
OPTION 1 - specify the name in the @Bean annotation 选项1-在@Bean批注中指定名称
@Bean(name="multipartResolver")
public CommonsMultipartResolver fileResolver() {
CommonsMultipartResolver cr = new CommonsMultipartResolver();
cr.setDefaultEncoding("UTF-8");
cr.setMaxUploadSize(26214400);
return cr;
}
OPTION 2 - rename the method 选项2-重命名方法
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver cr = new CommonsMultipartResolver();
cr.setDefaultEncoding("UTF-8");
cr.setMaxUploadSize(26214400);
return cr;
}
For most people, the above change will likely resolve the issue. 对于大多数人来说,以上更改可能会解决问题。 However, I also needed to add Apache Commons IO to my project due to ClassNotFoundException errors that appeared after making the above change.
但是,由于进行上述更改后出现ClassNotFoundException错误,因此我还需要向项目中添加Apache Commons IO。
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
With these updates in place, the form now binds properly. 完成这些更新后,表单现在可以正确绑定了。 My issues are resolved.
我的问题已解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.