【问题标题】:Passing Table values to action class将表值传递给操作类
【发布时间】:2009-10-31 09:00:18
【问题描述】:

在我的一个网页中,我有一个 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 类。

有没有办法

  1. 获取表中的行数
  2. 使用 javascript 传输值以及如何在我的操作类中获取它们

Struts 2 顺便说一句。

谢谢

【问题讨论】:

  • Struts 1 还是 2?列的值 - 你是什么意思:td 标签内的 html 值或者你有输入字段?
  • 我正在使用 struts 2,我的意思是 td 标签内的 html 值....我没有使用输入字段

标签: java javascript struts


【解决方案1】:

一种可能的解决方案,我猜,你在迭代器中有这些行...

所以 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;
}

动作类:

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 解析。

【讨论】:

    猜你喜欢
    • 2013-08-07
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多