简体   繁体   English

将表值传递给动作类

[英]Passing Table values to action class

In one of my web pages I have a HTML table. 在我的一个网页中,我有一个HTML表。 This table will have 0 or more rows and each row has 3 columns. 该表将具有0行或更多行,每行具有3列。

It looks like this: 看起来像这样:

<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>

I want to transfer the values of the columns (content of td's) to Action class. 我想将列的值(td的内容)传输到Action类。

Is there a way to 有没有办法

  1. get the number of rows in a table 获取表中的行数
  2. transfer the values using javascript and how will I get them in my action class 使用javascript传输值,以及如何在动作类中获取它们

Struts 2 btw. 支柱2 btw。

Thanks 谢谢

One of possible solutions, I guess, that you have this rows in an iterator... 我猜,一种可能的解决方案是您在迭代器中有此行...

So the JSP would look like that: 因此,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 file: 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 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. 当然,您将以html形式在action类中获取htmlValues ,但是那里有很多html htmlValues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM