简体   繁体   English

如何在Struts2自动完成操作中传递javascript对象?

[英]How to pass javascript object in Struts2 autocompleter action?

I am trying to send javascript object to Struts2 autocompleter action through parameter. 我试图通过参数发送JavaScript对象到Struts2自动完成操作。 But it doesn't work. 但它不起作用。

Here is my code: 这是我的代码:

<script>
var nameList = new Array();
    function func1(element)
    {       
        console.log(element.value);
        var index = nameList.indexOf(element.value);
        element.checked ? nameList.push(element.value) : nameList.splice(index,1);      
        console.log(nameList.length+"---- "+nameList);  
    }   
</script>

<s:form name="baseForm" id="baseForm">

    <table width="100%" >
        <tr> 
            <td ><s:text name="Units" /></td>
            <td>
                <s:iterator value="units" status="itr" id="un">
                    <input type="checkbox" onclick="func1(this)"
                        name="unitList[<s:property value="#itr.index"/>]"
                        value="<s:property value="#un" />" /> 
                </s:iterator>    
            </td>                
        </tr>       
        <tr>
            <td ><s:text name="Groups"/></td>
            <td width="80%">
                <sd:autocompleter href='get_groups.action?unitList=%{nameList}' id='unitAutoComplete' name='unitGroup' loadOnTextChange='true' 
                    loadMinimumCount='1' showDownArrow='false' autoComplete='false' />
            </td>
        </tr>
    </table>
</s:form>

Based on the Units selected, Groups should get populated in autocompleter. 根据选定的单位,应在自动填充程序中填充组。

So my first question is Is it possible to send javascript object into struts2 action through paramaters? 所以我的第一个问题是可以通过参数将javascript对象发送到struts2动作吗?

If it is yes, please suggest me how to pass the javascript object as parameters in action. 如果是,请建议我如何传递javascript对象作为参数。

You can't do it in the same request. 您无法在同一请求中执行此操作。 The autocompleter tag is rendered on the server-side but you want to modify it on the client-side using JavaScript when it's already rendered. autocompleter标记在服务器端呈现,但是您希望在已经呈现时使用JavaScript在客户端修改它。

Your best bet is to make ajax call to the server and pass the variable to the action where it's performed to modify the list returned by the autocompleter action. 最好的办法是对服务器进行ajax调用,并将变量传递给执行该操作的操作,以修改autocompleter操作返回的列表。

How to pass JavaScript object to server you should know that you need JSON format. 如何将JavaScript对象传递给服务器,您应该知道您需要JSON格式。 You can read Action to accept dynamic json data from user interface to get more information how to convert JavaScript object to JSON and send it to the action. 您可以阅读Action以接受来自用户界面的动态json数据,以获取有关如何将JavaScript对象转换为JSON并将其发送到操作的更多信息。

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

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