gpt4 book ai didi

java - Spring 包含在同一表单中的多个 jsp 在 Ajax 回调上给出错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:46:41 27 4
gpt4 key购买 nike

为什么我有一个带有 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");
}
});
},

教育看起来像workExperience 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

如果我在workExperience.jsp中放置一个带有commandName =“cv”的新表单,它工作正常,但如果我在教育中放入该表单,那么他第一次无法识别,在java中它为空。我不想将 commandName="workExperience"放入 workExperience.jsp 中,因为如果我想保存大简历,那么他不会将 workExperience.jsp 字段放入 cv 对象中。有什么解决办法吗?

最佳答案

我解决了它,它不太好,但工作正常,我添加一个 c:if 如果它是第一次使用大表单加载,则在没有 workExperience 表单的情况下显示它,如果它是从 java 返回的 jsp 页面,则使用 workExperience 表单显示。仅当从 java 返回 workExperience jsp 页面时,变量 ajax 才为 true。现在这是我的工作经历表:

<%@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>

关于java - Spring 包含在同一表单中的多个 jsp 在 Ajax 回调上给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30754914/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com