ID
始终为0而不是实际ID
display: none
并将隐藏字段值设置为true
(此隐藏字段旨在让我知道在迭代列表时删除了哪个项目)。但是当我提交表单时,javabean列表中的已删除项目将取空值。那么我如何将隐藏元素映射到javabean对象,或者有另一种方法来实现我的概念?
这是我的代码:
JSP:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s" %>
<%@ taglib uri="/struts-dojo-tags" prefix="sx"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<script language="javascript" src="js/jquery-1.9.1.min.js"></script>
<script language="javascript" src="js/common.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Education List</title>
</head>
<body>
<s:form action="/save" method="POST">
<div class="educationForm">
<c:if test="${ (not empty educations) }">
<c:if test="${ fn:length(educations) ge 1 }">
<c:forEach items="${educations}" var="edu" varStatus="status">
<div class="educations">
<input type="hidden" name="education[${ status.index }].eduID" value="${ educations[status.index].index }" />
<label>Position</label><input type="text" name="educations[${ status.index }].index" value="${ educations[status.index].index }" /> <a href="" class="delete">Delete</a><br/>
<label>School</label><input type="text" name="educations[${ status.index }].school" value="${ educations[status.index ].school }" /><br/>
<label>Degree</label><input type="text" name="educations[${ status.index }].degree" value="${ educations[status.index ].degree }" /><br/>
<label>GPA</label><input type="text" name="educations[${ status.index }].scored" value="${ educations[status.index ].scored }" /><br/>
<label>Start Date</label><input type="text" name="educations[${ status.index }].startDate" value="${ educations[status.index].startDate }" /><br/>
<label>End Date</label><input type="text" name="educations[${ status.index }].endDate" value="${ educations[status.index].endDate }" /><br/>
<input type="hidden" name="educations[${ status.index }].deleted" value="${ educations[status.index].deleted }" />
</div>
</c:forEach>
</c:if>
</c:if>
<div class="educations">
<label>Position</label><input type="text" name="educations[${fn:length(educations)}].index" value="${fn:length(educations) + 1}" /><a href="" class="delete">Delete</a><br/>
<label>School</label><input type="text" name="educations[${fn:length(educations)}].school" /><br/>
<label>Degree</label><input type="text" name="educations[${fn:length(educations)}].degree" /><br/>
<label>GPA</label><input type="text" name="educations[${fn:length(educations)}].scored" /><br/>
<label>Start Date</label><input type="text" name="educations[${fn:length(educations)}].startDate" /><br/>
<label>End Date</label><input type="text" name="educations[${fn:length(educations)}].endDate" /><br/>
<input type="hidden" name="educations[${fn:length(educations)}].deleted" value="false" />
</div>
</div>
<a href="" id="addButton">Add new Edu</a>
<input type="submit" value="Save" />
</s:form>
<div class="template_educations" style="display:none">
<div class="educations">
<label>Position</label><input type="text" name="educations[_X_].index" value="_Y_" /><a href="" class="delete">Delete</a><br/>
<label>School</label><input type="text" name="educations[_X_].school" /><br/>
<label>Degree</label><input type="text" name="educations[_X_].degree" /><br/>
<label>GPA</label><input type="text" name="educations[_X_].scored" /><br/>
<label>Start Date</label><input type="text" name="educations[_X_].startDate" /><br/>
<label>End Date</label><input type="text" name="educations[_X_].endDate" /><br/>
<input type="hidden" name="ducations[_X_].deleted" value="false" />
</div>
</div>
</body>
</html>
jquery的:
$(document).ready(function(){
//handle add new education
$("#addButton").click(function(event){
event.preventDefault();
//append html inside template_educations div into educationForm div
$(".educationForm").append($(".template_educations").html());
//loop through input tag inside educations div
$(".educationForm").children(".educations").last().children("input").each(function(){
var count = $(".educationForm").children(".educations").length;
//replace value of position textfield with current position
var value = $(this).attr("value");
if(typeof value !== 'undefined' && value !== false)
{
value = value.replace("_Y_", count);
$(this).attr("value", value);
}
//replace educations list index in textfield
var name = $(this).attr("name");
name = name.replace("_X_", count);
$(this).attr("name", name);
});
});
//handle delete education
$("body").on("click", ".delete", function(event){
event.preventDefault();
//hide all tag in education and set deleted to true
var parent = $(this).parents(".educations");
var hidden = parent.find("input[type=hidden]");
hidden.val("true");
parent.children().each(function(){
if($(this) !== hidden)
{
$(this).hide();
}
});
//display undelete button
parent.append("<a class='undelete' href=''>undelete</a>");
});
//handle undelete education
$("body").on("click", ".undelete", function(event){
event.preventDefault();
//unhide all tag in parent and set deleted to false
var parent = $(this).parents(".educations");
var hidden = parent.find("input[type=hidden]");
hidden.val("false");
parent.children().each(function(){
if($(this) !== hidden)
{
$(this).show();
}
});
//delete undelete button
$(this).remove();
});
});
动作:
package com.education.actions;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.education.bean.Education;
import com.education.dao.DataConnectDao;
import com.opensymphony.xwork2.ActionSupport;
public class SaveEdu extends ActionSupport
{
/**
*
*/
private static final long serialVersionUID = 1L;
private List<Education> educations;
public List<Education> getEducations() {
return educations;
}
public void setEducations(List<Education> educations) {
this.educations = educations;
}
@Action(value="/save", results={
@Result(name="success", type="redirect", location="/list.action"),
@Result(name="input", type="redirect", location="/list.action")
})
public String execute()
{
DataConnectDao connect = new DataConnectDao();
connect.insertDetailDao(this.educations);
return SUCCESS;
}
}
的JavaBean:
package com.education.bean;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
public class Education {
@GeneratedValue(strategy=GenerationType.AUTO)
private int eduID;
private String school;
private String degree;
private float scored;
private String startDate;
private String endDate;
private int index;
private boolean deleted;
public Education()
{
deleted = false;
}
public int getEduID() {
return eduID;
}
public void setEduID(int eduID) {
this.eduID = eduID;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
public float getScored() {
return scored;
}
public void setScored(float scored) {
this.scored = scored;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public boolean isDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
}
插入数据:
package com.education.serivces;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.education.bean.Education;
import com.education.utils.HibernateUltils;
public class DataConnect {
Session sess;
Transaction transaction;
List<Education> educations;
private Logger logger = Logger.getLogger(this.getClass());
public void inserEducation(List<Education> edu)
{
try
{
sess = HibernateUltils.getSession();
transaction = sess.beginTransaction();
for(Iterator<Education> educations = edu.iterator(); educations.hasNext();)
{
Education education = educations.next();
sess.saveOrUpdate(education);
}
transaction.commit();
}
catch (Exception e)
{
transaction.rollback();
logger.error(e);
}
finally
{
sess.close();
}
}
@SuppressWarnings({ "unchecked", "finally" })
public List<Education> getEducation()
{
try
{
sess = HibernateUltils.getSession();
sess.beginTransaction();
Query query = sess.createQuery("from Education");
this.educations = (List<Education>) query.list();
}
catch(Exception e)
{
logger.error(e);
}
finally
{
sess.close();
return educations;
}
}
}
答案 0 :(得分:1)
我建议您在服务器端只保留一个名为indexesToRemove的字符串变量,并将其映射到jsp中的一个隐藏值。 它将删除的EduId的值由某个分隔符分隔。
Ex:2-5-8意味着必须从列表中删除EduID 2,5和8。 现在,每次用户删除/取消删除时,您都可以使用JS代码修改它的值。
使用此字符串值,您可以根据需要处理提交或某些服务器端操作的列表。
关于第一部分: 我不知道隐藏值无法映射到java bean属性的任何限制。我猜这个问题是你有:
<input type="hidden" name="education[${ status.index }].eduID" value="${ educations[status.index].index }" />
为什么不是:value="${ educations[status.index].eduID }"
?