How to send javascript array to struts action by using jQuery Ajax

前端 未结 2 1451
北恋
北恋 2021-01-13 16:22

I am new to Struts 2. I want to send a javascript array to a Struts action class by using jQuery AJAX request.
The alert is working fine, the execute() is

相关标签:
2条回答
  • 2021-01-13 16:25

    To use JSON with Struts2, your best option is to import the Struts2 JSON plugin.

    Then,

    1. you'll get automatic JSON conversion when exposing data from the action to the JSP, by using the Json Result as described here, BUT
    2. you'll need to include the Json Interceptor in your interceptor stack to have the JSON conversion from the JSP to the action, as described here and here.
    0 讨论(0)
  • 2021-01-13 16:33

    Jquery serializes data sent as parameters using $.param internally when doing ajax request with $.ajax.

    The data should be set as array of integers or string with comma separated list of integers, so jQuery can correctly serialize it before sending with the request.

    You can send an array parameter to struts2 only with traditional setting because struts using type conversion to populate a property of the action using keys as parameter names.

    So, the array should be an array of primitive integers but your array contains other objects that are not primitive integers.

    To demonstrate you can see this demo to understand how to get parameter values and serialize it the same way like is doing $.ajax.

    Struts2 also can convert a string containing a comma separated values by default type conversion. For example you can see how checkbox list values are passed to struts action.

    0 讨论(0)
提交回复
热议问题