java - Spring中使用Map进行绑定(bind) - Thymeleaf

标签 java spring spring-mvc thymeleaf

我可以使用 spring MVC 将字段绑定(bind)到 thymeleaf 中的 Map 吗?

例如,考虑这个例子。

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

@Data
public class SampleCommand {

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

我的示例.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>

我的 Controller 包含:

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

所以每当我点击 url http://host:port/contextName/sample 时,当我输入字段并点击提交按钮时,它会将我重定向到 example.html,因为下面的方法包含表单中的数据。

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

这就是我尝试向您展示如何将对象绑定(bind)到 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>

Controller 有点像这样。

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

有没有一种方法可以在不使用命令对象的情况下将 thymeleaf 中表单的数据绑定(bind)到 Controller 中的 map 。

最佳答案

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

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

关于java - Spring中使用Map进行绑定(bind) - Thymeleaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28362681/

相关文章:

java - Thymeleaf 和 Spring MVC 的表单参数为 null

json - Spring MVC - HttpMediaTypeNotAcceptableException

java - 执行 Broadleaf startadmin.bat 脚本时出错

java - Springfox 复制 Controller

java - 使用 java servlet 重定向响应中丢失 HTTP 自定义 header

java - 如何在 Spring MVC Web 应用程序中从 Tomcat 自定义目录读取文件

java - 如何根据条件应用Spring消息转换器?

java - Java 中使用原始类型方法的类型不匹配

java - 在java中读取输入

java - 链表的 getlast() 中出现 NullPointerException