Passing Table values to action class
In one of my web pages I have a HTML table. This table will have 0 or more rows and each row has 3 columns.
It looks like this:
<table>
<tr>
<td>Row1-Col1</td>
<td>Row1-Col2</td>
<td>Row1-Col1&l开发者_开发问答t;/td>
</tr>
<tr>
<td>Row2-Col1</td>
<td>Row2-Col2</td>
<td>Row3-Col1</td>
</tr>
</table>
I want to transfer the values of the columns (content of td's) to Action class.
Is there a way to
- get the number of rows in a table
- transfer the values using javascript and how will I get them in my action class
Struts 2 btw.
Thanks
One of possible solutions, I guess, that you have this rows in an iterator...
So the JSP would look like that:
<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 file:
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 class:
public MyAction extends ActionSupport {
private Integer lastIndex;
private String htmlValues;
public String execute() {
//here there should be values filled
System.out.println(getLastIndex);
}
}
I did not test this, so maybe there could be mistakes, but the main idea is shown. Of course, you will get in htmlValues
in action class in html form, but there are a lot of html parses out there.
精彩评论