gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:57:11 25 4
gpt4 key购买 nike

我可以使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com