我正在尝试在Struts2上构建一个向数据库表添加值的页面。问题是,页面必须允许用户向数据库表输入多行。当用户单击“提交”时,它必须读取和写入数据库表的行。用户可以在页面中添加或删除行
因此我尝试在页面上呈现List值。 Java代码如下所示:
List<Testimate> estimates;
private String removeIndex;
public String execute() throws Exception {
estimates = new ArrayList<Testimate>();
for(int i = 0; i < INITIAL_ESTIMATES; i++)
estimates.add(new Testimate());
return INPUT;
}
public String add() {
estimates.add(new Testimate());
System.out.println(estimates.size());
return INPUT;
}
public String remove() {
estimates.remove(Integer.parseInt(getRemoveIndex()));
System.out.println(estimates.size() + " " + getRemoveIndex());
return INPUT;
}
And the page looks something like this:
<script>
setRemoveIndex()
{
$('input[name="removeIndex"]').val(removeIndex);
return true;
}
</script>
<s:form theme="custom" onsubmit="setRemoveIndex()">
<s:submit action="CEST02_add" cssClass="ButtonSmall" value="Add estimate" />
<s:hidden name="removeIndex"/>
<table>
<s:iterator value="estimates" var="estimate" status="status">
<tr>
<td><s:textfield name="estimates[%{#status.index}].name"cssClass="product" /></td>
<td><s:textfield name="estimates[%{#status.index}].price"cssClass="product" /></td>
<td><s:textfield name="estimates[%{#status.index}].date"cssClass="product" /></td>
<td><s:textfield name="estimates[%{#status.index}].attr"cssClass="product" /></td>
<td><s:submit action="CEST02_remove" cssClass="ButtonSmall" value="Remove this estimate" onclick="removeIndex=%{#status.index}"/>
</td>
</tr>
</s:iterator>
</table>
</s:form>
当我点击“添加估计”时,它会将元素添加到“估计”列表中。并且它正确打印尺寸。但是,当我点击“删除此估算值”时,它不会更改“估算值”列表。但它打印出列表的大小减少了一个。当我再次点击时,大小根本不会改变。它没有得到任何修改。
请告诉我这段代码有什么问题。我可能会对这个框架的运作方式产生一些误解。如果您有任何问题或说明,请直接询问
更新
我在JSP上使用以下行解决了我的问题。但问题仍然是为什么我不能在我的行动中做到这一点。
<s:iterator value="estimates" var="estimate" status="status">
<s:if test="#status.index != removeIndex">
答案 0 :(得分:3)
我尝试使用你的代码并且它对我来说很好。虽然我不理解你Testimate
课程的内部,所以取代List<Testimate> estimates
我List<String> estimates;
。这是我的工作代码。
List<String> estimates;
private int removeIndex;
//there getters and setters
public String execute() throws Exception
{
estimates = new ArrayList<String>();
for(int i = 0; i < 5; i++){
estimates.add(""+i);
}
return SUCCESS;
}
public String remove() {
estimates.remove(getRemoveIndex());
System.out.println(estimates.size() + " " + getRemoveIndex());
return SUCCESS;
}
<body>
<script>
function setRemoveIndex(val)
{
alert(val);
document.getElementById("removeIndex").value=val;
//$('input[name="removeIndex"]').val(removeIndex);
document.myform.action="remove.action";
document.myform.submit();
return true;
}
</script>
<s:form theme="simple" id="myform" name="myform">
<s:submit action="CEST02_add" cssClass="ButtonSmall" value="Add estimate" />
<s:hidden name="removeIndex" id="removeIndex"/>
<table>
<s:iterator value="estimates" var="estimate" status="status">
<tr>
<td><s:textfield name="estimates[%{#status.index}]"cssClass="product" /></td>
<td>
<s:submit action="remove" cssClass="ButtonSmall" value="Remove this estimate" onclick="return setRemoveIndex('%{#status.index}')"/>
</td>
</tr>
</s:iterator>
</table>
</s:form>
</body>
<action name="remove" class="example.HelloWorld" method="remove">
<result>/example/HelloWorld.jsp</result>
</action>
以上代码完全删除了List值,我也检查了页面刷新,并且值没有回来。
答案 1 :(得分:2)
您是否尝试在删除项目后在remove()中刷新Hibernate会话?我认为您的更改未被提交。
答案 2 :(得分:1)
请参阅以下代码,该代码对我有用:
行动类
package com.mycompany;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author james
*/
public class EstimateAction extends ActionSupport {
private List<Testimate> estimates;
private String removeIndex;
private static final int INITIAL_ESTIMATES = 10;
public List<Testimate> getEstimates() {
return estimates;
}
public void setEstimates(List<Testimate> estimates) {
this.estimates = estimates;
}
public String getRemoveIndex() {
return removeIndex;
}
public void setRemoveIndex(String removeIndex) {
this.removeIndex = removeIndex;
}
public String execute() throws Exception {
estimates = new ArrayList<Testimate>();
for (int i = 0; i < INITIAL_ESTIMATES; i++) {
estimates.add(new Testimate());
}
return INPUT;
}
public String add() {
estimates.add(new Testimate());
System.out.println(estimates.size());
return INPUT;
}
public String remove() {
estimates.remove(Integer.parseInt(getRemoveIndex()));
System.out.println(estimates.size() + " " + getRemoveIndex());
return INPUT;
}
}
<强> struts.xml中强>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Configuration for the default package. -->
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default">
<action name="showEstimates" class="com.mycompany.EstimateAction">
<result name="input">/estimatePage.jsp</result>
</action>
<action name="removeEstimates" class="com.mycompany.EstimateAction" method="remove">
<result name="input">/estimatePage.jsp</result>
</action>
</package>
</struts>
<强> estimatePage.jsp 强>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
function setRemoveIndex(removeIndex)
{
document.myForm.action = "removeEstimates.action"; //First set the form to navigate to the specific action
$('input[name="removeIndex"]').val(removeIndex);
//alert("Here");
document.myForm.submit(); //Submit the form with the action attribute change
return false; //Cancel the form submission which was triggered earlier
}
</script>
</head>
<body>
<s:form name="myForm" onsubmit="setRemoveIndex()">
<s:submit action="add" cssClass="ButtonSmall" value="Add estimate" />
<s:hidden name="removeIndex"/>
<table>
<s:iterator value="estimates" var="estimate" status="status">
<tr>
<td><s:textfield name="estimates[%{#status.index}].name"cssClass="product" /></td>
<td><s:textfield name="estimates[%{#status.index}].price"cssClass="product" /></td>
<td><s:textfield name="estimates[%{#status.index}].date"cssClass="product" /></td>
<td><s:textfield name="estimates[%{#status.index}].attr"cssClass="product" /></td>
<td><s:submit action="remove" cssClass="ButtonSmall" value="Remove this estimate" onclick="return setRemoveIndex(%{#status.index})"/>
</td>
</tr>
</s:iterator>
</table>
</s:form>
</body>
</html>