Spring Webflow - 使用multipart / form-data和文件上载时出现IllegalStateException

时间:2014-08-06 13:15:02

标签: file-upload multipartform-data spring-webflow-2

我正在尝试将文件上传添加到我的Spring Webflog表单处理中。只要表单enctype没有设置为multipart / form-data,表单submition就可以了。但是在我将Spring输入enctype =“multipart / form-data”后,会发生以下异常:

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添加到我的春季语境中:

<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:

 <dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2.2</version>
 </dependency>

我的JSP看起来像这样:

<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定义:

<view-state id="state1" model="customerModel">
    ...
    <transition on="submit" to="submitFormActions"/>
</view-state>

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

模型对象:

public class CustomerModel implements Serializable{
    private String firstName;
    private String lastName;
    private MutlipartFile avatar;

    ...
    //public getters and setters
}

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

由于

1 个答案:

答案 0 :(得分:2)

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

    <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。 doc说:

    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.