Question

How can I bind the following Bootstrap list selector given the following? I have the select list populated fine from passed in view model from controller to index.jsp. However, there seems to be a lot of variation on binding.

class viewModel {
  List<String> theList;      // <- populated in my GET controller code
  String selectedListItem;   // <- trying to figure out how to get selected list item?
  o o o
  // assume get/set
}

index.jsp:

(I get the postback fine on the submit button click and I can access the viewmodel but just need to figure out how I can get the selected item from the list.)

    <form class="form-horizontal" action="myController/indexSubmit" method="post">
        <select class="form-control" placeholder=".input-medium" height>
            <c:forEach items="${viewModel.gettheList()}" var="item" varStatus="count"> 
                <option value="${count.index}">${item }</option>
            </c:forEach>
        </select>   
        <button type="submit" class="btn btn-primary btn-medium">Submit</button>   
    </form> 

controller:

@RequestMapping(value="indexSubmit", method = RequestMethod.POST)
public String indexSubmit(@ModelAttribute("viewModel") viewModel viewModel, ModelMap model) {
    String item = viewModel.getselectedListItem(); // <- How can I pass this in?
    System.out.println("Selected Item: " + item);
    return "myController/index";  // <- want to add as query param: ?item=theItem  
}   
Was it helpful?

Solution

You don't need to create special class to bind list of elements. Simplest example of binding select with controller:

Controller:

@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(HttpServletRequest request, Model model) {  
    List<String> listString = new ArrayList<String>();
    listString.add("hello");
    listString.add("world");
    listString.add("from");
    listString.add("Belarus");
    model.addAttribute("listString", listString);
    return "index";
}

@RequestMapping(value = "test", method = RequestMethod.POST)
public String test(@RequestParam String selectedString, Model model) {
    System.out.println(selectedString);
    return "index";
}

index.jsp:

<form class="form-horizontal" action="${home}/test" method="post">
    <select name="listString" class="form-control">
        <c:forEach items="${listString}" var="item" varStatus="count"> 
            <option value="${count.index}">${item}</option>
        </c:forEach>
    </select>   
    <button type="submit" class="btn btn-primary btn-medium">Submit</button>   
</form> 

With that code after submitting form you will see selected items indexes in console.

If you want to transport also initial list (listItem) you can make another select with all items selected and attribute multiple="multiple" and set its style to display: none; After this declare another param @RequestParam List<String> listString and you'll get your initial array of strings.

About adding param:

return "myController/index";  // <- want to add as query param: ?item=theItem  

You can't pass parameter that way. Thats because you return name of view (tiles of simply name of jsp), but not url. If you want to go to url with param you can make:

return "forward:/index?item=theItem";

Hope it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top