春天4表格帖子不起作用

时间:2014-08-28 22:36:54

标签: jsp spring-mvc

我有一个简单的更新表单,在表单上提交不保留更新的表单值。完全相同的代码适用于不同的portlet。

InvitationController.java

package connect.myaccount.invitation.controller;

import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.validation.Valid;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;

import connect.liferay.model.Invitation;
import connect.liferay.model.impl.InvitationImpl;
import connect.myaccount.map.InvitationMap;
import connect.myaccount.model.BasicInfo;
import connect.myaccount.service.InvitationService;


@Controller
@RequestMapping(value = "VIEW")
@SessionAttributes({ "basicInfo" })
public class InvitationController {

@Autowired
InvitationService invitationService;

@Autowired
MessageSource messageSource;

@Autowired
InvitationMap invtiationMap;

ModelMapper modelMapper;

@RequestMapping
public String updateIncorrectDetails(RenderRequest request, Model model) {

    if (!model.containsAttribute("basicInfo")) {
        String invitationId = (request.getParameter("id") != null) ? request
                .getParameter("id") : "1";
        Invitation invitation;
        try {
            invitation = this.invitationService.getInvitation(invitationId);
            modelMapper = new ModelMapper();
            BasicInfo info = modelMapper.map(invitation, BasicInfo.class);
            model.addAttribute("basicInfo", info);
        } catch (PortalException e) {
            e.printStackTrace();
        } catch (SystemException e) {
            e.printStackTrace();
        }
    }
    return "edit-invitation";
}


@RequestMapping(params = "action=changeInvitationDetails")
public void doUpdateIncorrectDetails(@Valid @ModelAttribute("basicInfo") BasicInfo basicInfo,
        BindingResult result, Model model, SessionStatus status, ActionResponse response) {

    if (result.hasErrors()) {
        model.addAttribute("basicInfo", basicInfo);
        response.setRenderParameter("action", "");

    } else {
        modelMapper = new ModelMapper();
        Invitation invitation = modelMapper.map(basicInfo, InvitationImpl.class);
        this.invitationService.updateIncorrectDetails(invitation);
        status.setComplete();
        String message = this.messageSource.getMessage("details.updated", null, "", null);
        model.addAttribute("message", message);
        response.setRenderParameter("action", "message");
    }
}
}

编辑invitation.jsp

<%@page session="false" contentType="text/html"
pageEncoding="ISO-8859-1" import="java.util.*,javax.portlet.*"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>



<portlet:defineObjects />

<portlet:actionURL var="changeInvitationDetails">
<portlet:param name="action" value="changeInvitationDetails" />
</portlet:actionURL>

<div class="register-container">

<div class="update-box col-xs-12 col-sm-12 col-md-6 col-lg-6">
    <div class="content-boxes">
        <div class="content-head">
            <h4>Update</h4>
        </div>
        <div class="content-text">
            <form:form action="${changeInvitationDetails}" method="post"
                commandName="basicInfo" role="form">
                <form:errors cssStyle="error-block" path="*" element="div"></form:errors>
                <div class="form-group">
                    <label for="first-name"
                        class="col-xs-12 col-sm-4 col-md-4 col-lg-4">First name</label>
                    <form:input id="first-name" path="firstName"
                        cssClass="form-control col-xs-12 col-sm-8 col-md-8 col-lg-8"
                        placeholder="First name" />
                    <form:errors path="firstName" cssClass="error" />
                </div>
                <div class="form-group">
                    <label for="last-name"
                        class="col-xs-12 col-sm-4 col-md-4 col-lg-4">Last name</label>
                    <form:input id="last-name" path="lastName"
                        cssClass="form-control col-xs-12 col-sm-8 col-md-8 col-lg-8"
                        placeholder="Last name" />
                </div>
                <div class="form-group">
                    <label for="company" class="col-xs-12 col-sm-4 col-md-4 col-lg-4">Company</label>
                    <form:input id="company" path="company"
                        cssClass="form-control col-xs-12 col-sm-8 col-md-8 col-lg-8"
                        placeholder="Company" />
                </div>
                <div class="form-group">
                    <label for="company" class="col-xs-12 col-sm-4 col-md-4 col-lg-4">Job Title</label>
                    <form:input id="jobTitle" path="jobTitle"
                        cssClass="form-control col-xs-12 col-sm-8 col-md-8 col-lg-8"
                        placeholder="Job Title" />
                </div>
                <div class="form-group">
                    <label for="Email" class="col-xs-12 col-sm-4 col-md-4 col-lg-4">Email</label>
                    <form:input id="email" path="email"
                        cssClass="form-control col-xs-12 col-sm-8 col-md-8 col-lg-8"
                        placeholder="Email" />
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-default btn-sm pull-right">Change Details</button>
                </div>
            </form:form>
        </div>
    </div>
</div>
</div>

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

<requires-namespaced-parameters>false</requires-namespaced-parameters>

我在liferay-portlet.xml中错过了这个