包含在同一表单中的Spring多个jsp会给Ajax回调带来错误

时间:2015-06-10 11:27:01

标签: java ajax spring jsp spring-mvc

Hy我有一个带有cv形式的jsp和他的字段:

<form:form commandName="cv" action="${pageContext.request.contextPath}/site/my-account/cv/save" id="cv-template-form" method="POST">
<h3><spring:message code="view.candidates.form.workExperience" /></h3>
    <div id="workExperienceDiv">
        <jsp:include page="/WEB-INF/views/site/commons/cv/workExperience.jsp"/> 
    </div>
    <a href="javascript:void(0);" onclick="AjaxUtils.submitAttribute('${pageContext.request.contextPath}/site/my-account/cv/save/workExperience','workExperienceDiv');" class="btn btn-large btn-block btn-lightgray">
        <spring:message code="button.labe.add.more"/>
    </a>
<h3><spring:message code="view.candidates.form.education" /></h3>
    <div id="educationDiv">
        <jsp:include page="/WEB-INF/views/site/commons/cv/education.jsp"/>  
    </div>
    <a href="javascript:void(0);" onclick="AjaxUtils.submitAttribute('${pageContext.request.contextPath}/site/my-account/cv/save/education','educationDiv');" class="btn btn-large btn-block btn-lightgray">
        <spring:message code="button.labe.add.more"/>
    </a>

工作经历jsp:

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

    <c:forEach var="workExperience" items="${cv.workExperiences}" >
        <div class="row">
            <label class="col-md-4"><spring:message code="view.candidates.form.workExperience.employer" /></label>
            <div class="col-md-8">
                <input value="${workExperience.employer}" id="employer" class="text block"/>
            </div>
        </div>

        <div class="row">
            <label class="col-md-4"><spring:message code="view.candidates.form.workExperience.period" /></label>
            <div class="col-md-4">
                <div class="datepicker">
                    <i class="fa fa-calendar"></i>
                    <input value="${workExperience.dateFrom}" class="text block date"/>
                </div>
            </div>
            <div class="col-md-4">
                <div class="datepicker">
                    <i class="fa fa-calendar"></i>
                    <input value="${workExperience.dateTo}" class="text date block"/>
                </div>
            </div>
        </div>

        <div class="row">
            <label class="col-md-4"><spring:message code="view.candidates.form.workExperience.position" /></label>
            <div class="col-md-8">
                <input value="${workExperience.position}" id="position" type="text" class="text block"/>
            </div>
        </div>

        <div class="row">
            <label class="col-md-4"><spring:message code="view.candidates.form.workExperience.activities" /></label>
            <div class="col-md-8">
                <textarea class="textarea block">${workExperience.position}</textarea>
            </div>
        </div>  

        <div class="row">
            <a class="editor_remove" href="#" onclick="AjaxUtils.submitAttribute('${pageContext.request.contextPath}/site/my-account/cv/delete/workExperience/${workExperience.tempId}');">
                <i class="fa fa-times"></i>Remove
            </a>
        </div>
    </c:forEach>

    <div class="row">
        <label class="col-md-4"><spring:message code="view.candidates.form.workExperience.employer" /></label>
        <div class="col-md-8">
            <form:errors id="employerError" path="workExperience.employer" cssClass="error"/>
            <form:input path="workExperience.employer" id="employer" class="text block" cssErrorClass="text block error"/>
        </div>
    </div>

    <div class="row">
        <label class="col-md-4"><spring:message code="view.candidates.form.workExperience.period"/></label>
        <div class="col-md-4">
        <form:errors path="workExperience.dateFrom" cssClass="error"/>
            <div class="datepicker">
                <i class="fa fa-calendar"></i>
                <form:input path="workExperience.dateFrom" cssClass="text block date" cssErrorClass="text block date error" placeholder="From"/>
            </div>
        </div>
        <div class="col-md-4">
            <div class="datepicker">
                <form:errors path="workExperience.dateTo" cssClass="error"/>
                <i class="fa fa-calendar"></i>
                <form:input path="workExperience.dateTo" cssClass="text date block" cssErrorClass="text date block error" placeholder="To"/>
            </div>
        </div>
    </div>

    <div class="row">
        <label class="col-md-4"><spring:message code="view.candidates.form.workExperience.position" /></label>
        <div class="col-md-8">
            <form:errors path="workExperience.position" cssClass="error"/>
            <form:input path="workExperience.position" id="position" type="text" class="text block" cssErrorClass="text block error"/>
        </div>
    </div>

    <div class="row">
        <label class="col-md-4"><spring:message code="view.candidates.form.workExperience.activities" /></label>
        <div class="col-md-8">
            <form:errors path="workExperience.activities" cssClass="error"/>
            <form:textarea path="workExperience.activities" cssClass="textarea block" cssErrorClass="textarea block error"/>
        </div>
    </div>  

ajax功能:

submitAttribute: function (url1,div){
        $.ajax({
            type: "POST",
            url: url1,
            data: $('#cv-template-form').serialize(),
            cache: false,
            success: function(data) {
                $('#'+div).html(data);
                Utils.Pikaday.init("input.date");
            }
        }); 
    },

教育看起来像工作经验jsp。理论上我想填写workExperience字段发送它与ajax并将其放入workExperienceList并发回列表和新的workExperience。问题是在ajax回调中给我一个错误,因为他没有认识到workExperience.jsp中的workExperience。 这是错误:

 SEVERE: Servlet.service() for servlet [spring-mvc-dispatcher] in context with path [/] threw exception [Request processing failed; nested exception is org.apache.tiles.request.render.CannotRenderException: ServletException including path '/WEB-INF/views/site/commons/cv/workExperience.jsp'.] with root cause
    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'workExperience' available as request attribute

如果我使用commandName =&#34; cv&#34;在workExperience.jsp它工作正常,但如果我把形式的教育,那么他不认识第一次,在java中它是空的。我不想把commandName =&#34; workExperience&#34;在workExperience.jsp中,因为如果我想保存大型cv,那么他就不会将workExperience.jsp字段放在cv对象中。任何解决方案?

1 个答案:

答案 0 :(得分:0)

我解决了它,它不太好但它工作正常我添加ac:如果它是第一次加载大表单然后显示它没有workExperience表单如果它从java返回jsp页面然后显示与workExperience形成。当从java返回workExperience jsp页面时,变量ajax是正确的。现在这是我的工作经历表格:

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<c:set var="index" value="0"/>
<c:if test="${ajax == true}">

    <form:form commandName="cv">
        <c:forEach var="workExperience" items="${cv.workExperiences}" >
            <div class="row">
                <label class="col-md-4"><spring:message code="view.candidates.form.workExperience.employer" /></label>
                <div class="col-md-8">
                    <form:errors id="employerError" path="workExperiences[${index}].employer" cssClass="error"/>
                    <form:input path="workExperiences[${index}].employer" id="employer" class="text block" cssErrorClass="text block error"/>
                </div>
            </div>

            <div class="row">
                <label class="col-md-4"><spring:message code="view.candidates.form.workExperience.period"/></label>
                <div class="col-md-4">
                <form:errors path="workExperiences[${index}].dateFrom" cssClass="error"/>
                    <div class="datepicker">
                        <i class="fa fa-calendar"></i>
                        <form:input path="workExperiences[${index}].dateFrom" cssClass="text block date" cssErrorClass="text block date error" placeholder="From"/>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="datepicker">
                        <form:errors path="workExperiences[${index}].dateTo" cssClass="error"/>
                        <i class="fa fa-calendar"></i>
                        <form:input path="workExperiences[${index}].dateTo" cssClass="text date block" cssErrorClass="text date block error" placeholder="To"/>
                    </div>
                </div>
            </div>

            <div class="row">
                <label class="col-md-4"><spring:message code="view.candidates.form.workExperience.position" /></label>
                <div class="col-md-8">
                    <form:errors path="workExperiences[${index}].position" cssClass="error"/>
                    <form:input path="workExperiences[${index}].position" id="position" type="text" class="text block" cssErrorClass="text block error"/>
                </div>
            </div>

            <div class="row">
                <label class="col-md-4"><spring:message code="view.candidates.form.workExperience.activities" /></label>
                <div class="col-md-8">
                    <form:errors path="workExperiences[${index}].activities" cssClass="error"/>
                    <form:textarea path="workExperiences[${index}].activities" cssClass="textarea block" cssErrorClass="textarea block error"/>
                </div>
            </div>

            <c:if test="${index < cv.workExperiences.size()-1 || cv.workExperiences[index].id != null}">
                <a class="editor_remove" href="javascript:void(0);" onclick="AjaxUtils.submitAttribute('${pageContext.request.contextPath}/site/my-account/cv/delete/workExperience/${index}','workExperienceDiv');">
                    <i class="fa fa-times"></i><spring:message code="button.label.remove"/>
                </a>
            </c:if>
            <c:set var="index" value="${index+1}"/>
        </c:forEach>
    </form:form>
</c:if>

<c:if test="${ajax != true}">

    <c:forEach var="workExperience" items="${cv.workExperiences}" >
        <div class="row">
            <label class="col-md-4"><spring:message code="view.candidates.form.workExperience.employer" /></label>
            <div class="col-md-8">
                <form:errors id="employerError" path="workExperiences[${index}].employer" cssClass="error"/>
                <form:input path="workExperiences[${index}].employer" id="employer" class="text block" cssErrorClass="text block error"/>
            </div>
        </div>

        <div class="row">
            <label class="col-md-4"><spring:message code="view.candidates.form.workExperience.period"/></label>
            <div class="col-md-4">
            <form:errors path="workExperiences[${index}].dateFrom" cssClass="error"/>
                <div class="datepicker">
                    <i class="fa fa-calendar"></i>
                    <form:input path="workExperiences[${index}].dateFrom" cssClass="text block date" cssErrorClass="text block date error" placeholder="From"/>
                </div>
            </div>
            <div class="col-md-4">
                <div class="datepicker">
                    <form:errors path="workExperiences[${index}].dateTo" cssClass="error"/>
                    <i class="fa fa-calendar"></i>
                    <form:input path="workExperiences[${index}].dateTo" cssClass="text date block" cssErrorClass="text date block error" placeholder="To"/>
                </div>
            </div>
        </div>

        <div class="row">
            <label class="col-md-4"><spring:message code="view.candidates.form.workExperience.position" /></label>
            <div class="col-md-8">
                <form:errors path="workExperiences[${index}].position" cssClass="error"/>
                <form:input path="workExperiences[${index}].position" id="position" type="text" class="text block" cssErrorClass="text block error"/>
            </div>
        </div>

        <div class="row">
            <label class="col-md-4"><spring:message code="view.candidates.form.workExperience.activities" /></label>
            <div class="col-md-8">
                <form:errors path="workExperiences[${index}].activities" cssClass="error"/>
                <form:textarea path="workExperiences[${index}].activities" cssClass="textarea block" cssErrorClass="textarea block error"/>
            </div>
        </div>

        <c:if test="${index < cv.workExperiences.size()-1 || cv.workExperiences[index].id != null}">
            <a class="editor_remove" href="javascript:void(0);" onclick="AjaxUtils.submitAttribute('${pageContext.request.contextPath}/site/my-account/cv/delete/workExperience/${index}','workExperienceDiv');">
                <i class="fa fa-times"></i><spring:message code="button.label.remove"/>
            </a>
        </c:if>
        <c:set var="index" value="${index+1}"/>
    </c:forEach>
</c:if>