Spring Webflow 2.4.5 multipart / form-data绑定问题

时间:2018-01-11 19:04:18

标签: java spring-webflow-2

我为项目配置了多部分解析器:

@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;
    }

}

我使用可序列化对象作为模型:

public class NewRequestModel implements Serializable {

    private static final long serialVersionUID = -5372803991673351564L;

    private String entry;

    private transient MultipartFile attachment;

    ... // getters and setters

}

当页面加载时,我初始化模型:

的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");
    }

我绑定了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>

单击该按钮时,将使用事件“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>

当我调试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");
    }

如果从表单中删除enctype =“multipart / form-data”,则绑定适用于“entry”变量。当然,这不允许上传文档。所以我需要能够在表单上包含enctype。

我已经过去了,查了解答案并阅读了指南,我只是看不出为什么会失败。我非常感谢您找到有问题的配置/代码。

2 个答案:

答案 0 :(得分:0)

尝试向表单添加操作(crsf属性是可选的):

<form:form action="${flowExecutionUrl}&${_csrf.parameterName}=${_csrf.token}" modelAttribute="requestModel" enctype="multipart/form-data">

并使用简单的输入:

<input type="file" name="attachment" id="attachment"/>

和一个简单的提交:

<input type="submit" id="submitAndValidate" name="_eventId_ submitAndValidate" value="Submit Request">

答案 1 :(得分:0)

问题出在我的MultipartResolver bean配置中。 Spring专门研究名为&#34; multipartResolver的bean。&#34;因此我有两个选项来解决这个问题:

选项1 - 在@Bean注释中指定名称

@Bean(name="multipartResolver")
public CommonsMultipartResolver fileResolver() {
    CommonsMultipartResolver cr = new CommonsMultipartResolver();
    cr.setDefaultEncoding("UTF-8");
    cr.setMaxUploadSize(26214400);
    return cr;
}

选项2 - 重命名方法

@Bean 
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver cr = new CommonsMultipartResolver();
    cr.setDefaultEncoding("UTF-8");
    cr.setMaxUploadSize(26214400);
    return cr;
}

对于大多数人来说,上述变化可能会解决问题。但是,由于在进行上述更改后出现ClassNotFoundException错误,我还需要将Apache Commons IO添加到我的项目中。

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>

通过这些更新,表单现在可以正确绑定。我的问题已经解决了。