2013-04-08 60 views
0
  1. 我有一个动态表单供用户输入他们的教育信息。一个用户可以有超过1个教育信息。我使用隐藏字段来存储用户的eduID。但它看起来像隐藏的字段不能映射到javabean项目。当我提交已有项目的表单时,服务器端的该项目的ID始终为0,而​​不是其实际的ID
  2. 我希望为用户提供删除和取消删除项目的功能。当用户删除一个项目时,我将所有元素设置为display: none,并将隐藏字段值设置为true(此隐藏字段旨在让我知道迭代列表时哪个项目被删除)。但是当我提交表单时,删除的项目在javabean列表中取空值。

那么我该如何将隐藏的元素映射到javabean对象,或者有另一种方法来实现我的概念?无法将隐藏项目映射到javabean struts2

这里是我的代码:

的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

请考虑使用较少的代码来说明您的问题。 – 2013-04-08 12:43:38

+0

对不起,实际上我不知道哪一部分是错的,所以我尽量给每个人尽可能多的信息 – 2013-04-08 12:58:00

回答

1

我建议你保持在一个名为indexesToRemove服务器端只是一个单独的字符串变量并将其映射在你的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 }"

+0

从理论上说,没关系,我会试一试。你对'问题1'有什么想法吗?每当我试图用已存在的物品提交表单时,该物品的“ID”始终为0. – 2013-04-08 09:41:20

+0

更新了答案 – prashant 2013-04-08 09:47:53

+0

哦,我的不好。我很笨拙,非常感谢你为我指出 – 2013-04-08 09:50:52