简体   繁体   中英

How to pass javascript object in Struts2 autocompleter action?

I am trying to send javascript object to Struts2 autocompleter action through parameter. 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?

If it is yes, please suggest me how to pass the javascript object as parameters in action.

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.

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.

How to pass JavaScript object to server you should know that you need JSON format. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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