我试图在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;
}
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)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>
Run Code Online (Sandbox Code Playgroud)
当我点击"添加估计"时,它会将元素添加到列表"估计"中.并且它正确打印尺寸.但是,当我点击"删除此估算值"时,它不会更改"估算值"列表.但它打印出列表的大小减少了一个.当我再次点击时,大小根本不会改变.它没有得到任何修改.
你能告诉我这段代码有什么问题吗?我可能会对这个框架的运作方式产生一些误解.如果您有任何问题或澄清,请询问
更新:
我通过JSP上的以下行解决了我的问题.但问题仍然是为什么我不能在我的行动中做到这一点.
<s:iterator value="estimates" var="estimate" status="status">
<s:if test="#status.index != removeIndex">
Run Code Online (Sandbox Code Playgroud)
我尝试使用你的代码,它对我来说工作得很好。虽然我不了解你的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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
<action name="remove" class="example.HelloWorld" method="remove">
<result>/example/HelloWorld.jsp</result>
</action>
Run Code Online (Sandbox Code Playgroud)
上面的代码完美地删除了列表值,我也检查了页面刷新,并且值没有恢复。
| 归档时间: |
|
| 查看次数: |
13482 次 |
| 最近记录: |