Spring MVC File Upload返回空值

时间:2014-12-25 15:09:05

标签: java spring forms

这是表格

        <form:form commandName="product" enctype="multipart/form-data" action="${pageContext.request.contextPath}/product/save">
            <!-- Image Input -->
            <div class="form-row">
                <div class="col-xs-12 form-group">
                    <label class="control-label">Product Image</label>
                    <div class="input-group image-preview">
                        <input type="text" class="form-control image-preview-filename" disabled="disabled">
                        <span class="input-group-btn">
                            <!-- image-preview-clear button -->
                            <button type="button" class="btn btn-default image-preview-clear" style="display:none;">
                                <span class="glyphicon glyphicon-remove"></span> Clear
                            </button>
                            <!-- image-preview-input -->
                            <div class="btn btn-default image-preview-input">
                                <span class="glyphicon glyphicon-folder-open"></span>
                                <span class="image-preview-input-title">Browse</span>
                                <form:input type="file" accept="image/png, image/jpeg, image/gif" path="productImage"/>
                            </div>
                        </span>
                    </div>
                </div>
            </div>
            <!-- Product Number -->
            <div class="form-row">
                <div class="col-xs-12 form-group">
                    <label class="control-label">Product Number</label>
                    <form:input path="code" type="text" class="form-control" />
                </div>
            </div>
            <!-- Product Name-->
            <div class="form-row">
                <div class="col-xs-12 form-group">
                    <label class="control-label">Product Name</label>
                    <form:input path="name" type="text" class="form-control"/>

                </div>
            </div>
            <!-- Product Description -->
            <div class="form-row">
                <div class="col-xs-12 form-group">
                    <label class="control-label">Product Description</label>
                    <form:textarea path="description" class="form-control" />
                </div>
            </div>
            <!-- Price, Cost and  Quantity -->
            <div class="form-row">
                <div class="col-xs-4 form-group required">
                    <label class="control-label">Suggested Retail Price</label>
                    <form:input path="srp" class="form-control" size="4" type="text"/>
                </div>
                <div class="col-xs-4 form-group required">
                    <label class="control-label">Item Cost</label>
                    <form:input    path="cost" class="form-control" type="text"/>
                </div>
                <div class="col-xs-4 form-group required">
                    <label class="control-label">Dealers Price</label>
                    <form:input path="dp" class="form-control" type="text"/>
                </div>
            </div>
            <!-- Button -->
            <div class="form-row">
                <div class="col-md-12 form-group">
                    <button class="form-control btn btn-primary submit-button" type="submit">Add »</button>
                </div>
            </div>
        </form:form>

表格支持对象

public class ProductForm {

    private MultipartFile productImage;

    private ProductDTO productDTO;

    ..Setters and getters

}

控制器

@RequestMapping(value="/save",method = RequestMethod.POST,consumes="multipart/form-data")
public ModelAndView saveProductForm(@ModelAttribute("product") ProductForm productForm,BindingResult result){
    productService.saveProduct(productForm.getProductDTO());
    return MavBuilder.render(ADD_PRODUCT_VIEW).toMav();
}

保存后,表单支持对象会为产品图像和输入的表单值eceives null值,但是如果我删除了enctype,并选择要上载的图像。它只是表单支持对象检索的表单值。我在这里错过了什么?

1 个答案:

答案 0 :(得分:1)

管理修复我的问题,默认情况下spring不是多部分解析器。所以我添加了一个

<bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>
</bean>