在Spring中使用Map进行绑定 - Thymeleaf

我可以使用spring MVC将字段绑定到thymeleaf中的Map .

例如,考虑这个例子 .

假设我有一个像这样的简单命令对象

@Data
public class SampleCommand {

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

我的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>

我的控制器包含:

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

因此,每当我点击网址http://host:port/contextName/sample时,它现在重定向到sample.html,当我输入字段并点击提交按钮时,由于绑定,下面的方法包含表单中的数据 .

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

所以这就是我尝试向您展示如何将对象绑定到Thymeleaf中的模板的方式 .

我的问题是有一些方法可以使用Map而不是我们的simpleObject中的对象 .

我怎么能这样做才能做到这一点

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

</form>

控制器会有点像这样 .

@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.

回答(1)

2 years ago

您可以使用文档中的 @RequestParam 注释

在Map或MultiValueMap参数上使用@RequestParam注释时,将使用所有请求参数填充 Map .

@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";
}