2

I would like to send a list of values to a Spring controller in a submission. The list changes dinamically in the client. I looked at this but only works with input, because I can not put a name in the tag li.

<ul id="paramselected" class="paramsnav" class="dropdown">
    <li class="ui-widget-content selected">value1</li>
    <li class="ui-widget-content selected">value2</li>
    ...                 
</ul>

Are there anyway to achieve it?

Any help would be greatly appreciated,

Thanks.

Community
  • 1
  • 1
Esteban S
  • 1,599
  • 4
  • 18
  • 40

6 Answers6

3
<ul id="paramselected" class="paramsnav" class="dropdown">
  <li class="ui-widget-content selected"><input type="hidden" value="value1" name="liContent">value1</li>
  <li class="ui-widget-content selected"><input type="hidden" value="value2" name="liContent">value2</li>  
  <li class="ui-widget-content selected"><input type="hidden" value="value3" name="liContent">value3</li>              
</ul>

And you controller shuold be like :

@RequestMapping(value="/actionName")
  public String getLiValues(HttpServletRequest request){
    String[] liValues = request.getParameterValues("liContent");
  }

And your array is ready with the li values.

Joydeep
  • 86
  • 9
1

Note that <ul> data is not sent to the server because it is not a user input component.

check below link

Trying to get some kind of dynamic, unordered list to send the order to servlet

but if you want to send all values you can send it by javascript or ajax by fetching <li> values in javascript and send to servlet.

Community
  • 1
  • 1
Musaddique
  • 1,295
  • 1
  • 11
  • 28
1

You can Post the data using a javascript POST(with an XMLHttpRequest), retrieve the data from the list and send it.

Youssef NAIT
  • 1,040
  • 9
  • 22
1

Use jquery for getting the value of li tag , after that make a array and call the controller with array as a request parameter.

1

AFAIK, Normal behavior is that only input elements are posted to the servlet from <form> submit. You can rather post it through jquery

$(document).ready(function(){
    $("#button").click(function(){
        var arr = [];
        // get the text from li's
        $('#paramselected li').each(function(index) {
          arr.push($(this).text());
        });
        $.post("URLMapped",
        {
          liValues: arr; // arr contains your li texts
        },
        function(data,status){
            // your response here
        });
    });
});
Santhosh
  • 8,045
  • 2
  • 26
  • 54
0

you can use js or jquery ajax request for this. You can find all li value different at js, after that you describe var obj. which has li values and call ajax method :

$.ajax({
        type: 'POST',
        contentType: 'application/json',
        dataType: 'json',
        mimeType: 'application/json',
        async: true,
        url: url,
        data: obj,
        success: function (result) {
            console.log(result);

        }
    });
3vge
  • 235
  • 1
  • 2
  • 13