【问题标题】:Using Map for binding in Spring - Thymeleaf在 Spring 中使用 Map 进行绑定 - Thymeleaf
【发布时间】:2015-04-06 09:54:41
【问题描述】:

我可以使用 spring MVC 将字段绑定到 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>

我的控制器包含:

@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 时,现在当我输入字段并点击提交按钮时,它会将我重定向到 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";
}

那么有没有一种方法可以在不使用命令对象的情况下将 thymeleaf 中的表单中的数据绑定到控制器中的地图。

【问题讨论】:

    标签: java spring spring-mvc thymeleaf


    【解决方案1】:

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

    当 @RequestParam 注解用于 Map 或 MultiValueMap 参数,地图填充了所有 请求参数。

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

    【讨论】:

    • 你能告诉我如何复制模板吗?
    • 也许我错过了你的问题。通过使用@RequestParam,您的表单将绑定到地图而无需任何更改。尽管您失去了验证,但它会将请求参数绑定到地图。
    • 来补偿你的意思的验证?您将不得不编写一个自定义验证器。因此,您在动态绑定方面获胜,但在验证方面松散,这是一种权衡
    猜你喜欢
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多