Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

java - How to post values from checkboxes generated from an ArrayList to another objects ArrayList with form in Thymeleaf and Spring?

I have a form that takes several inputs from the user to create an object and one of these inputs are a set of checkboxes.

The checkboxes are generated from a Model with an ArrayList with Grape objects called "GrapeList" but I can't manage to figure out how to add them to the object that is created when submitting the form.

My form looks like this:

<form class="col-12" action="#" th:action="@{/admin}" th:object="${Wine}" method="post">
        <div class="row">
            <div class="col-1">
                <label for="nameInput">Name: </label>
            </div>
            <div class="col-11">
                <input class="w-100" id="nameInput" type="text" th:field="*{name}">
            </div>
        </div>
            <div class="col-1">
                <label for="descriptionInput">Description: </label>
            </div>
            <div class="col-11">
                <textarea class="w-100" id="descriptionInput" type="text" th:field="*{description}"></textarea>
            </div>
        </div>
====================PROBLEM=======================
        <div class="row" th:each="grape: ${GrapeList}">
            <div class="col">
                <label th:for="'grape' + ${grape.name}" th:text="${grape.name}"></label>
            </div>
            <div class="col">
                <input type="checkbox" th:id="'grape' + ${grape.name}" th:value="${grape.id}" th:field="*{grapes.add()}">
            </div>
        </div>
====================PROBLEM=======================

        <button class="btn btn-outline-success" type="submit" value="Submit">Save</button>
    </form>

The form is creating a Wine Object that has an ArrayList with Grape Objects called "grapes". The other input fields works fine but I dont know how to bind the arraylist to add a grape object for every checked box.. The th:field="*{grapes.add()}" is just there to describe what I'm trying to do..

Any help is appreciated!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. Add name attribute to your generated checboxes, for example "grape". Therefore they all will be generated with same name. When form with such checkboxes is submitted browser sends values for chosen name repeatedly for every selected checkbox, like

    grape=first&grape=third&grape=extra

  2. Capture this data in your controller. This can be done in several ways.

    • Get parameters directly from HttpServletRequest:

      @RequestMapping(path="/admin", method=RequestMethod.POST)
      public void formSubmitted(HttpServletRequest request){
        Map<String, String[]> allParams = request.getParameterMap(); // fetch all params 
        String[] grapes = request.getparameterValues("grape");       // or just grapes
      }
      
    • Use @RequestParam annotation

      @RequestMapping(path="/admin", method=RequestMethod.POST)
      public void formSubmitted(@RequestParam MultiValueMap<String, String> allParams,
                                @RequestParam(name="grape", optional=true) List<String> grapes){
        // your code
      }
      
    • Not absolutely sure if it's possible to map this construct directly to object's field, but it seems you can tweak Spring MVC to do this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...