在我的一个网页中,有一个 HTML 表格。该表将有 0 行或更多行,每行有 3 列。
看起来像这样:
<table>
<tr>
<td>Row1-Col1</td>
<td>Row1-Col2</td>
<td>Row1-Col1</td>
</tr>
<tr>
<td>Row2-Col1</td>
<td>Row2-Col2</td>
<td>Row3-Col1</td>
</tr>
</table>
我想将列的值(td 的内容)传输到 Action 类。
有没有办法
- 获取表格中的行数
- 使用 JavaScript 传输值以及如何在我的操作类中获取它们
顺便说一句,Struts 2。
谢谢
最佳答案
我猜,可能的解决方案之一是,您在迭代器中拥有这些行......
所以 JSP 看起来像这样:
<s:form action="myAction">
<table>
<s:iterator value="someCollection" status="stat">
<!-- set id of column -->
<tr id="myTd<s:property value="#stat.index" />">
<td>some html</td>
</tr>
</s:iterator>
</table>
<s:hidden name="lastIndex" />
<s:hidden name="htmlValues" />
<s:submit onclick="submitValues();">
</s:form>
JS文件:
function submitValues() {
var htmlValue;
int i = 0;
while(document.getElementById('myId'+i)) {
htmlValue += document.getElementById('myId'+i).innerHTML;
i++;
}
document.getElementyById('lastIndex').value = i;
document.getElementyById('htmlValues').value = htmlValue;
}
Action 类:
public MyAction extends ActionSupport {
private Integer lastIndex;
private String htmlValues;
public String execute() {
//here there should be values filled
System.out.println(getLastIndex);
}
}
我没有对此进行测试,所以可能会有错误,但主要思想已显示。当然,你会以html形式进入action类中的htmlValues
,但是那里有很多html解析。
关于java - 将表值传递给操作类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1653829/