gpt4 book ai didi

java - Spring MVC + Thymeleaf 将单个对象发送到 Controller

转载 作者:行者123 更新时间:2023-12-02 01:16:17 27 4
gpt4 key购买 nike

我有点震惊,因为我找不到如何执行此操作的示例。每次我用谷歌搜索时,我都会得到有关如何发布对象集合或其他不相关内容的信息。 thymeleaf 文档(我能找到的)似乎也没有解释太多,就像有很多假设的知识一样。

回到我的问题,我只想从表单中发布一个对象(bean)。 我希望我的 Controller 映射方法绑定(bind)到这个“pojo”bean,而不是绑定(bind)到一堆字符串/整数。

我发现唯一接近的是 StackOverflow 上的东西,其中一半代码在问题中,另一半在答案中,并且总是有一些人的评论说它不起作用对于他们来说。

有人可以用一个简单的、无聊的例子来缓解一下吗?

最佳答案

可以找到以下代码片段,可能对您有帮助。

Controller GET/POST 映射:

@RequestMapping(value = "/registration", method = RequestMethod.GET)
public String registartionPage(Model model) {

Registration registration = new Registration();

model.addAttribute("registration", registration);

return "registarion/registarion";
}

@RequestMapping(value = "/user/new-user-registrn", method = RequestMethod.POST)
public String newUserRegistrn(Model model, @ModelAttribute("registration")
Registration registration, RedirectAttributes redirectAttributes) {

try {

StarUser user = starSecurityService.findSysUserName(registration.getUserName());
if (user != null) {
throw new Exception("User Already Exist. Please try with different User Name");
}

user = (StarUser) starUtilService.save(setStarUser(registration));
model.addAttribute("registration", registration);
if (user != null) {

redirectAttributes.addAttribute("starMessage",
"Your Account is successfully created !! Login to Access the Application");

return "redirect:/";
}

} catch (Exception e) {

model.addAttribute(STAR_MESSAGE, e.getMessage());
}

return "registarion/registarion";
}

thymeleaf 含量:

<form class="form-horizontal col-sm-12" method="POST" th:action="@{/user/new-user-registrn}" th:object="${registration}">

<div class="row">

<div class="form-group col-md-12">
<div class="star-reg-header">New User Registration</div>
</div>

<div class="star-reg-body">

<div class="form-group col-sm-4">
<label class="required">First Name: </label>
<input type="text" class="form-control required" th:field="*{firstName}" required="required" />
</div>

<div class="form-group col-sm-4">
<label class="required">Last Name: </label>
<input type="text" class="form-control" th:field="*{lastName}" required="required" />
</div>

<div class="form-group col-sm-4">
<label class="required">User Name: </label>
<input type="text" class="form-control" th:field="*{userName}" required="required" />
</div>

<div class="form-group col-sm-4">
<label class="required">Password: </label>
<input type="password" class="form-control" th:field="*{password}" required="required" />
</div>

<div class="form-group col-sm-4">
<label class="required">Email: </label>
<input type="text" class="form-control" th:field="*{email}" required="required" />
</div>

</div>

</div>
<div class="form-group col-md-12">
<label class="col-sm-2"></label>
<div class="col-sm-10">
<button type="submit" class="btn btn-info">Submit</button>
</div>
</div>

Java Bean 类

public class Registration {

protected String firstName;

protected String lastName;

protected String userName;

protected String password;

protected String email;

//Setter and Getter

}

关于java - Spring MVC + Thymeleaf 将单个对象发送到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58496093/

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