【问题标题】:How to get the values of a table generated in the jsp in the action in struts 2?struts 2中的action如何获取jsp中生成的表的值?
【发布时间】:2013-05-25 15:51:34
【问题描述】:

在我的 jsp 中,我从一个 Table1 中选择一些值,然后使用 Add 按钮将其添加到另一个表 Table2 中。这是我的jsp:

<%@page  contentType="text/html;charset=UTF-8" language="java" 
                                               pageEncoding="UTF-8"%>
<%@taglib prefix="s"uri="/struts-tags"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" 
                     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Insert title here</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" />
    <script type="text/javascript">
     $(document).ready(function(){
            $('#add').on("click", function(){
                $('#one tbody input:checked').parent().parent()
                                             .appendTo("#two tbody");
                return false;
            });

            $('#remove').on("click", function(){
                $('#two tbody input:checked').parent().parent()
                                             .appendTo("#one tbody");
                return false;
            });
        });


function GetCellValues() {
     var oTable = document.getElementById('two');
     var mycars = new Array();
     var mycars1 = new Array();
     var mycars2 = new Array();
        //gets table

        var rowLength = oTable.rows.length;
        //gets rows of table

        for (i = 0; i < rowLength; i++){
        //loops through rows

           var oCells = oTable.rows.item(i).cells;
           //gets cells of current row
           var cellLength = oCells.length;
               for(var j = 0; j < cellLength; j++){
               //loops through each cell in current row
                  //get your cell info here
                  var cellVal = oCells.item(j).innerHTML;
                  alert(cellVal);
                  if(j==3){
                  mycars[i] = cellVal;
                  }
                  if(j==1){
                      mycars1[i] = cellVal;
                      }
                  if(j==2){
                      mycars2[i] = cellVal;
                      }
               }
        }
        window.location ="DynamicTable?mytable="+mycars;

 }
     </script>

</head>
<body>
  <button onclick="GetCellValues()">Submit</button>
    <table id="one" style="border:1px solid red;">
        <caption>Table 1</caption>
    <thead>
    <tr>
    <th ></th>
    <th> Id</th>
    <th>Name</th>
    <th>Status</th>
    <th>Type</th>
    <th>RollUp Type</th>
    <th>System</th>
    </tr>
    </thead>
        <tbody>
        <s:iterator value="formList1">
            <tr>
            <td><s:checkbox theme="simple" ></s:checkbox>
    </td>
    <td><s:property value="id"/></td>
    <td><s:property value="named"/></td>
    <td><s:property value="status"/></td>
    <td><s:property value="type"/></td>
    <td><s:property value="rollup"/></td>
    <td><s:property value="unit"/></td>

            </tr>
            </s:iterator>
         </tbody>
    </table>

    <table id="two" style="border:1px solid green;">
        <caption>Table 2</caption>
    <thead>
    <tr>
    <th ></th>
    <th> Id</th>
    <th>Name</th>
    <th>Status</th>
    <th>Type</th>
    <th>RollUp Type</th>
    <th>System</th>
    </tr>
    </thead>
        <tbody>

        </tbody>

    </table>
    <br><hr><br>
    <button id="add">Add</button>
    <button id="remove">Remove</button>

</body>
</html>

当我点击 Struts2 中的提交按钮时,如何在我的操作中检索我在表 2 中添加的值?

这是我尝试过的,我在单击提交按钮时调用了 javascript GetCellValues(),我在数组 mycars、mycars1 等中设置了每一列。

在我的动作类中,我有这些数组的 getter 和 setter。但在我的操作中,我无法检索这些值。

如何在action中获取jsp中生成的表格的值?

更新: 我刚刚更正了window.location ="DynamicTable?mytable="+mycars; 行,现在我将操作中的值作为用逗号分隔的字符串获取。我想知道除了我用过的方法还有没有其他方法?

【问题讨论】:

  • 使用您的网络服务器支持的标准方法。
  • 我用过但是不行
  • 不,你没有!如果你这样做了,我会看到的。
  • +1 因为我没有看到 jquery 插件的任何用途,而且您似乎解决了您自己的大部分问题......几件事 jsps 生成 html 页面。请说“如何将页面中生成的表格的值用于操作”。我们在这里(struts2 社区)了解 struts2 标签,但有一个更大的社区了解 html 和 js/jQuery。在查看有关 JS 的问题时,最好只显示 html。要将数据取回服务器是另一个问题,请查看 jquery ajax/getJSON 方法。还要研究 struts2-json-plugin。
  • 我从来没有使用过 struts2-json-plugin 并且一直在努力使用其他 struts2-ajax 和 struts2-jQuery 插件,如果你能建议我一些使用这些插件的示例链接会很有帮助

标签: javascript jquery struts2 struts struts2-jquery


【解决方案1】:

是的,您可以使用隐藏的输入字段将值传递给操作类。 您可以在 java 脚本中设置这些字段,这些字段将被称为提交按钮的 onclick()。

【讨论】:

    猜你喜欢
    • 2013-04-10
    • 2016-08-29
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多