使用ModelAttribute Liferay上传图片

时间:2015-02-06 17:25:34

标签: spring-mvc upload liferay

我想发送一个表单来更新我的用户,该表单包含一个modelAttribute和一个要上传的图片。

如果我使用enctype =" multipart / form-data"图像已上传,但modelAttribute中的所有字段均为空。

如果我删除了enctype =" multipart / form-data"我的modelAttribute已正确填充,但图片未上传...

你有解决方案吗?

我的代码如下:

jsp:

<portlet:actionURL name="submitMyInfosForm" var="submitMyInfosFormUrl">
     <portlet:param name="submitMyInfosForm" value="submitMyInfosForm"/>
</portlet:actionURL>

<form:form class="fm" modelAttribute="myInfosForm" action='${actionFormUrl}' enctype="multipart/form-data" method="post" id='myInfosForm'>      
                    <div class="fm_c">      
                        <label class="fm_c_n">
                            <p><spring:message code="portlet.moncompte.infos.form.firstname"/></p>
                            <input type="text" name="firstName" value="${myInfosForm.firstName}" <c:if test="${not isUpdateInfos}"> class="disabled" disabled</c:if> />
                            <div id="error_infos_firstName" class="error"></div>
                        </label>

                        <label class="fm_c_n">
                            <p><spring:message code="portlet.moncompte.infos.form.lastname"/></p>
                            <input type="text" name="lastName" value="${myInfosForm.lastName}" <c:if test="${not isUpdateInfos}"> class="disabled" disabled</c:if> />
                            <div id="error_infos_lastName" class="error"></div>
                        </label>

                        <label class="fm_c_n">
                            <p><spring:message code="portlet.moncompte.infos.form.email"/></p>
                            <input type="text" name="emailAddress" value="${myInfosForm.emailAddress}" <c:if test="${not isUpdateInfos}"> class="disabled" disabled</c:if> />
                             <div id="error_infos_emailAddress" class="error"></div>
                        </label>

                        <label class="fm_c_n">
                            <p><spring:message code="portlet.moncompte.infos.form.phone"/></p>
                            <input type="text" name="phone" value="${myInfosForm.phone}" <c:if test="${not isUpdateInfos}"> class="disabled" disabled</c:if> />
                            <div id="error_infos_phone" class="error"></div>
                        </label>

                        <label class="fm_c_n">
                            <p class="fm_c_n add"><spring:message code="portlet.moncompte.infos.form.address"/></p>
                            <div class="fm_c_n address">
                                <div id="error_infos_street1" class="error"></div>
                                <input type="text" name="street1" value="${myInfosForm.street1}" <c:if test="${not isUpdateInfos}"> class="disabled" disabled</c:if> />
                                <div id="error_infos_street2" class="error"></div>
                                <input type="text" name="street2" value="${myInfosForm.street2}" <c:if test="${not isUpdateInfos}"> class="disabled" disabled</c:if> />
                            </div>
                        </label>

                        <label class="fm_c_n">
                            <p><spring:message code="portlet.moncompte.infos.form.postalCode"/></p>
                            <input type="text" name="zip" value="${myInfosForm.zip}" <c:if test="${not isUpdateInfos}"> class="disabled" disabled</c:if> />
                            <div id="error_infos_zip" class="error"></div>
                        </label>

                        <label class="fm_c_n">
                            <p><spring:message code="portlet.moncompte.infos.form.city"/></p>
                            <input type="text" name="city" value="${myInfosForm.city}" <c:if test="${not isUpdateInfos}"> class="disabled" disabled</c:if> />
                            <div id="error_infos_city" class="error"></div>
                        </label>
                    </div>
                    <aside class="fm_photo">
                        <label class="fm_n">
                                    <p><spring:message code="portlet.moncompte.infos.form.portrait"/></p>
                                    <div id="error_infos_portrait" class="error"></div>
                                    <input name="portrait" size="50" type="file" />
                        </label>
                        <!-- p>
                            <img alt="<%=HtmlUtil.escape(currentUser.getFullName()) %>" class="user-logo" src="<%= currentUser.getPortraitURL(themeDisplay) %>" />
                        </p -->
                        <img src="<%= currentUser.getPortraitURL(themeDisplay) %>" alt="<%=HtmlUtil.escape(currentUser.getFullName()) %>" />
                    </aside>
                    </form:form>

控制器:

 @ActionMapping(params = "javax.portlet.action=submitMyInfosForm")
    public void updateMyInfos(@ModelAttribute MyInfosForm infosForm, ActionRequest request, ActionResponse response,
            Model model) {

modelAttribute:

public class MyInfosForm {

    private String firstName;

    private String lastName;

    private String emailAddress;

    private String phone;

    private String street1;

    private String street2;

    private String zip;

    private String city;

    private MultipartFile portrait;
...
}

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

使用UploadPortletRequest

时,您需要使用ActionRequest代替enctype="multipart/form-data"

我不熟悉Spring Framework,但您可以使用UploadPortletRequest获取ActionRequest

UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);

您可以获取内容并使用它,

File file = uploadRequest.getFile("fileName");
String someData = ParamUtil.getString(uploadRequest, "someData");