【问题标题】:Spring Webflow - IllegalStateException when using multipart/form-data and file uploadSpring Webflow - 使用多部分/表单数据和文件上传时出现 IllegalStateException
【发布时间】:2014-09-29 10:48:06
【问题描述】:

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

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 上下文中:

<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" 表单处理效果很好。

谢谢

【问题讨论】:

  • 你是否通过debug检查它是否进入portletAction.processForm方法?发布错误的完整堆栈跟踪。
  • 嗨@Prasad,感谢您的回复。在我的表单中使用 multipart/form-data 时,永远不会输入 portletAction.processForm 方法。我用调试器检查了它。可以看到完整的堆栈跟踪here。在我看来,好像动作阶段会被跳过(调试器直接进入视图状态“state1”并尝试渲染视图),但我在流配置文件中找不到任何错误,正如我之前所说 - 没有multipart/form-data enctype,表单处理工作正常(提交后输入方法processForm)。

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


【解决方案1】:

您正在使用不了解 portlet 上下文的 org.springframework.web.multipart.commons.CommonsMultipartResolver。 您需要将 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.

【讨论】:

  • 谢谢@Prasad,这解决了我的问题。正如我所料 - 这是我的愚蠢错误:/ ...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-21
  • 1970-01-01
  • 2012-04-10
  • 1970-01-01
相关资源
最近更新 更多