gpt4 book ai didi

java - 无法将隐藏项映射到 javabean struts2

转载 作者:行者123 更新时间:2023-12-01 14:41:50 24 4
gpt4 key购买 nike

  1. 我有一个动态表单供用户输入他们的教育信息。一个用户可以拥有多于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;
}
}
}

最佳答案

我建议您在服务器端仅保留一个名为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 }"

关于java - 无法将隐藏项映射到 javabean struts2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15875313/

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