关于Spring Webflow:Spring Webflow-使用multipart / form-data和文件上传时IllegalStateException

Spring Webflow - IllegalStateException when using multipart/form-data and file upload

我正在尝试将文件上传添加到我的Spring Webflog表单处理中。 只要表单enctype未设置为multipart / form-data,表单提交就可以正常工作。 但是,当我在Spring表单中添加enctype =" multipart / form-data"后,就会发生此异常:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
java.lang.IllegalStateException: A flow execution action URL can only be obtained in a RenderRequest or a ResourceRequest
    at org.springframework.webflow.context.portlet.PortletExternalContext.getFlowExecutionUrl(PortletExternalContext.java:215)
    at org.springframework.webflow.engine.impl.RequestControlContextImpl.getFlowExecutionUrl(RequestControlContextImpl.java:178)
    at org.springframework.webflow.mvc.view.AbstractMvcView.render(AbstractMvcView.java:189)
    at org.springframework.webflow.engine.ViewState.render(ViewState.java:293)
    at org.springframework.webflow.engine.ViewState.refresh(ViewState.java:242)
    at org.springframework.webflow.engine.ViewState.resume(ViewState.java:220)
    at org.springframework.webflow.engine.Flow.resume(Flow.java:537)
    at org.springframework.webflow.engine.impl.FlowExecutionImpl.resume(FlowExecutionImpl.java:259)
    at org.springframework.webflow.executor.FlowExecutorImpl.resumeExecution(FlowExecutorImpl.java:169)
    at org.springframework.webflow.mvc.portlet.FlowHandlerAdapter.handleAction(FlowHandlerAdapter.java:161)
    at org.springframework.web.portlet.DispatcherPortlet.doActionService(DispatcherPortlet.java:670)
    at org.springframework.web.portlet.FrameworkPortlet.processRequest(FrameworkPortlet.java:520)
    at org.springframework.web.portlet.FrameworkPortlet.processAction(FrameworkPortlet.java:461)
    at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:71)

我已经将CommonsMultipartResolver添加到我的spring上下文中:

1
2
3
4
5
<bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <!-- Limit uploads to one byte smaller than the server is allowed to handle -->
  <property name="maxUploadSize" value="100000" />
</bean>

并在我的pom.xml中包含commons-fileupload.jar:

1
2
3
4
5
 <dependency>
    <groupId>commons-fileupload</groupId>
    commons-fileupload</artifactId>
    <version>1.2.2</version>
 </dependency>

我的JSP如下所示:

1
2
3
4
5
6
7
8
9
<portlet:actionURL var="processFormAction">
    <portlet:param name="execution" value="${flowExecutionKey}"/>
</portlet:actionURL>
<form:form action="${processFormAction}" modelAttribute="customerModel" enctype="multipart/form-data" method="post">
    <form:input path="firstName" cssClass="input-size-1 valid-required" />
    <form:input path="lastName" cssClass="input-size-1  valid-required" />
    <input name="avatar" id="avatar" type="file"/>
    <input type="submit" name="_eventId_submit" id="send" value="Submit"/>
</form:form>

我的flow.xml定义:

1
2
3
4
5
6
7
8
9
10
<view-state id="state1" model="customerModel">
    ...
    <transition on="submit" to="submitFormActions"/>
</view-state>


    <evaluate expression="portletAction.processForm(customerModel, flowRequestContext)" />
    <transition on="success" to="state2"/>
    <transition on="error" to="state1" />
</action-state>

模型对象:

1
2
3
4
5
6
7
8
public class CustomerModel implements Serializable{
    private String firstName;
    private String lastName;
    private MutlipartFile avatar;

    ...
    //public getters and setters
}

有什么想法可能是错的吗? 如我所说,如果没有enctype =" multipart / form-data",则表单处理效果很好。

谢谢


您正在使用org.springframework.web.multipart.commons.CommonsMultipartResolver,它不了解Portlet上下文。
您需要将CommonsMultipartResolver更改为:

1
2
3
4
5
    <bean id="portletMultipartResolver"
        class="org.springframework.web.portlet.multipart.CommonsPortletMultipartResolver">
        <!-- one of the properties available; the maximum file size in bytes -->
        <property name="maxUploadSize" value="100000"/>
    </bean>

另外,为了使DispatcherPortlet能够识别此bean,您需要如上所述定义此bean id。 医生说:

1
2
3
    Any configured PortletMultipartResolver bean must have the following id (or name):"portletMultipartResolver".
    If you have defined your PortletMultipartResolver with any other name, then the DispatcherPortlet will not
    find your PortletMultipartResolver, and consequently no multipart support will be in effect.