简体   繁体   中英

Using Map for binding in Spring - Thymeleaf

Can I bind the fields to a Map in thymeleaf using spring MVC.

For example consider this example.

Suppose I have a simple command object like this

@Data
public class SampleCommand {

   @Email(message = "{invalidEmail}")
   private String email;
   private String password;
}

My sample.html

 <form role="form" th:action="@{/sample/saveSample}" th:method="post" th:object="${sampleCommand}">
    <div class="row">
        <div class="form-group col-md-6">
            <label class="control-label" for="emailaddress" th:text="#{email}"></label>
            <input type="email" class="form-control" name="emailAddress" id="emailaddress" placeholder="Enter email" th:field="${sampleCommand.email}"/>
        </div>
        <div class="form-group col-md-6">
            <label class="control-label" for="password">Password</label>
            <input type="password" class="form-control" name="password" id="password" placeholder="Password" th:field="${sampleCommand.password}"/>
        </div>
    </div>
    <div class="row text-center">
        <div class="col-lg-12">
            <button type="submit" class="btn btn-default align-center">Create User</button>
        </div>
    </div>
 </form>

My controller contains:

@RequestMapping(produces = "text/html")
public String sampleFormController(Model model) {
    System.out.println("Hello world");
    model.addAttribute("sampleCommand", new SampleCommand());
    return "/pla/sample/sample";
}

So whenever I hit the url http://host:port/contextName/sample , it redirects me to the sample.html now when i enter the fields and hit the submit button because of the binding the method below contains the data from the form.

 @RequestMapping(value = "/saveSample",method = RequestMethod.POST)
public String saveAgent(@Valid SampleCommand sampleCommand, 
                                      BindingResult   bindingResult){
    System.out.println(sampleCommand);
    return "pla/sample/sample";
}

So this is the way I tried showing you how to bind the object to templates in Thymeleaf.

My question is there some way to use Map instead of the object which is in our case simpleObject.

How can I do the above thing is this way

<form role="form" th:action="@{/sample/saveSample}" th:method="post" th:object="${someMapForTest}">
 //Map should define the binding to the controller.

</form>

And the controller would be somewhat like this.

@RequestMapping(produces = "text/html")
public String sampleFormController(Model model) {
    System.out.println("Hello sampleUsingMap");
    Map<String, Object> someMapForTest = Maps.newHashMap();
    model.addAttribute("someMapForTest", someMapForTest);
    return "/pla/sample/sampleUsingMap";
}

@RequestMapping(value = "/useMap",method = RequestMethod.POST)
public String saveAgent(@Valid Map<String,Object> someMapForTest){
    System.out.println("inside useMap");
    System.out.println(someMapForTest);
    return "pla/sample/sampleUsingMap";
}

So is there a way to bind the data from a form in thymeleaf to a map in controller without using the command object.

You can use the @RequestParam annotation, from the docs

When an @RequestParam annotation is used on a Map or MultiValueMap argument, the map is populated with all request parameters.

@RequestParam
@RequestMapping(value = "/useMap",method = RequestMethod.POST)
public String saveAgent(@RequestParam Map<String,Object> someMapForTest){
    System.out.println("inside useMap");
    System.out.println(someMapForTest);
    return "pla/sample/sampleUsingMap";
}      

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