0
  1. 我有一个动态表格供用户输入他们的教育信息。一个用户可以拥有多个教育信息。我使用隐藏字段来存储用户的 eduID。但看起来隐藏字段无法映射到 javabean 项目。当我提交带有现有项目的表单时,该项目ID在服务器端总是 0 而不是它的实际ID
  2. 我想为用户提供删除和取消删除项目的能力。当用户删除一个项目时,我设置所有元素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;          
        }       
    }
}
4

1 回答 1

1

我建议您在服务器端只保留一个名为 indexToRemove 的单独字符串变量,并将其映射到 jsp 中的一个隐藏值。它会将已删除的 EduId 的值用某个分隔符分隔。

例如: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 }"

于 2013-04-08T09:35:44.413 回答