将值从jsp中的javascript传递到struts2中的action类

问题描述 投票:0回答:4

如何将值从 javascript 传递到 struts2 中的 java 类? 这是与我想要的类似的链接。但是它对我不起作用。 我尝试过这样的事情。 jsp 中隐藏的 Struts 标签是

    function redirect(id)
    {
    window.alert("Hello"+id);
    document.getElementById('param_ID').value=id;
    document.forms["./ListAlgorithmAction"].submit();
    }

在我的操作类中,我已经为 param_ID 声明了 getter 和 setter。但它返回 null。

请帮忙。 谢谢你。

javascript jsp struts2
4个回答
0
投票

你应该这样使用

<s:hidden id = "param_ID" name="xyz" />

现在你的 struts 操作中应该有 xyz 属性的 getter 和 setter。

您应该始终将标签中的名称属性映射到 Struts 操作中的属性。

希望这有效。


0
投票

如果页面未提交您的

hidden
字段,则隐藏字段可能不在您的表单元素内。您可以尝试将其作为参数传递给您的网址。试试这个

function redirect(id)
{
     var form = document.forms[0];
     var url = './ListAlgorithmAction?param_ID=' + id;
     form.action = url;
     form.submit();
}

让我知道这是否有效。


0
投票

如果你想调用一个action并返回数据,你应该使用struts2-json插件JQuery脚本来调用action

    var abc = $("#abc").val();
            $.getJSON('youraction.action', {
                variable -name action class : abc,
                            value to be passed:value,

        //and so on
            }, function(data) {


        //your processing code
                }
            });
            return false;
        });

如果您只想调用操作,您可能需要使用 $.ajax 代替 $.getJson

如果您想使用 struts 2-json 那么您还需要更改 struts.xml 即

<result name="success" type="json"></result>

值得一去


0
投票

我遇到了类似的问题,并通过从 JavaScript 函数附加 Action 类并附加 URL 参数来解决它,如下所示:

<a href="(<%actionClassName%>.action?<%URLParms%>" />
© www.soinside.com 2019 - 2024. All rights reserved.