3

アイテムを作成するユーザーがいるJSPがあります。

作成後、更新と削除という2つのボタンとともに、テーブルの同じページにそれらを表示しています。

ユーザーが値を変更して[更新]をクリックすると、値が変更され、最初の行の列の値+変更された値がコンマで区切られて追加が表示されます。

なぜそれが起こっているのか、そしてそれを解決する方法を教えてもらえますか?

以下のjspファイルを見つけてください

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<script type="text/javascript">

    function updateRow(id) {
var manufacturer = document.getElementById("manufacturer").value;   
var family = document.getElementById("family").value;
                var model = document.getElementById("model").value;

        var url = "id="+id+"&manufacturer="+manufacturer+"&model="+model+"&family="+family;
            document.globalForm.action= "http://localhost:8080/ContentInventory/update.action?id=" + id + "&manufacturer="+ manufacturer + "&model=" + model + "&family=" + family;
        document.globalForm.submit(); 
    }

    function deleteRow(id) {
        document.globalForm.action = "http://localhost:8080/ContentInventory/delete.action?id="+id;
        document.globalForm.submit();

    }

    function addDetails() {
    document.globalForm.action = "http://localhost:8080/ContentInventory/add.action";
alert(document.globalForm.action);
document.globalForm.submit();

    }
</script>

<html>

<body>

    <h1>Global Handset Compatibility</h1>
    <s:actionerror />

    <s:form id="globalForm" name="globalForm" method="post" theme="simple" >
        <table>

            <tr>
                <td><s:label value="Manufacturer" />
                </td>
                <td><s:textfield name="manufacturer" label="Manufacturer" />
                </td>
            </tr>
            <tr>
                <td><s:label value="Model" />
                </td>
                <td><s:textfield name="model" label="Model" />
                </td>
            </tr>
            <tr>
                <td><s:label value="Family" />
                </td>
                <td><s:textfield name="family" label="Family" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                <!--<s:submit value="Add Inventory" align="center" />-->
                <input type="button" value="Add Inventory" id="addButton" onclick="javascript:addDetails()" >
                </td>
            </tr>
        </table>





        <h2>Inventory List</h2>
    <table id="global" border="1" width="100%">
        <tr>
            <th>Manufacturer</th>
            <th>Model</th>
            <th>Family</th>
            <th>Delete</th>
            <th>Edit/Update</th>
        </tr>

        <s:if test="%{globalList.size() > 0}">
            <s:iterator value="globalList" id="globals">

                <tr>


                    <td align="center">
                    <s:textfield id="manufacturer" name="#globals.manufacturer" /></td>

                    <td align="center"><s:textfield id="model" name="#globals.model" /></td>



                    <td align="center"><s:textfield id="family" name="#globals.family" /></td>

                    <td align="center"><input type="button" value="Delete" id="deleteButton" onclick="javascript:deleteRow('<s:property value="id"/>')">
</td>
                    <td align="center">
                                        <input type="button"  value="Update" id="updateButton<s:property value="id"/>"  onclick="javascript:updateRow('<s:property value="id"/>')">

                    </td>
                </tr>
            </s:iterator>

        </s:if>
        <s:else>
            <tr>
                <td colspan="5">No Data Found</td>
            </tr>
        </s:else>

    </table>
        </s:form>
</body>
</html>

そして、アクションクラスでは、フィールドのgetterメソッドとsetterメソッドがあり、[更新]ボタンをクリックすると、jsを使用して更新された値を取得し、バックエンドに送信します。値は正しく挿入されていますが、ページを表示している間、最初と2番目の値で区切られたコンマ付きの値も表示されます。これは正しくありません...

ご回答ありがとうございます

4

2 に答える 2

0

ids の属性iteratorは非推奨ですvar。代わりに使用してください。

ここで何を求めているのかわかりませんが、リストを反復していて、そのリストの要素を一意に参照したい場合 (投稿するとき、または JavaScript を介して値を取得するとき)、インデックスを使用する必要があります:

<s:iterator value="globalList" var="globals" status="ctr">

   <s:textfield id="manufacturer" name="globalList[#ctr.index].manufacturer" />

   <s:textfield id="model" name="globalList[#ctr.index].model" />

   <s:textfield id="family" name="globalList[#ctr.index].family" />

等...

次のように、IteratorStatus を使用して一意の HTMLidフィールドを生成することもできます (そうしないと、有効な HTML になりません)。

<s:textfield id="manufacturer_num_%{#ctr.index}" 
             name="globalList[#ctr.index].manufacturer" />

このようにして、各フィールドを一意に参照できます。

var manufacturer = document.getElementById("manufacturer_num_" + counter).value;

counterもちろん、これを機能させるには、 var を javascript 関数に渡す必要があります。

最終的な考えとして、繰り返し要素内のインデックスを使用すると、値を更新するために JavaScript や単一の投稿はもう必要ありませんList<yourObject> 。アクションで a を宣言し、すべてのグリッドを投稿するだけで、リストに行が自動的に入力されます。 JSP から; セキュリティ上の理由から、削除メソッドに対してのみ単一の要素を参照する必要があります。

于 2013-03-11T09:35:26.967 に答える