我对于找不到如何执行此操作的示例感到非常震惊。每次我搜索它时,我都会获得有关如何发布对象或其他无关内容的信息。百里香叶文档(我能找到的东西)似乎也没有太多解释,就像有很多假定的知识一样。
回到我的问题,我只想发布表单中的单个对象(bean)。 我希望我的控制器映射方法绑定到此“ pojo” bean,而不是一堆字符串/整数。
我发现唯一接近的是StackOverflow上的内容,其中一半代码在问题中,另一半在答案中,总是有一些人说它不起作用为他们。
有人可以用一个古老而乏味的例子在这里提供任何救济吗?
答案 0 :(得分:1)
在参数中使用@ModelAttribute
注释。
这样的事情。
@RequestMapping(value = "/someurl", method = RequestMethod.POST)
public String savePojo(@ModelAttribute PojoClass pojo, Model model) {
//Code
}
编辑:此答案对此有很好的信息。 What is @ModelAttribute in Spring MVC?
答案 1 :(得分:1)
可以找到下面的代码段可能对您有所帮助。
控制器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";
}
胸腺内容:
<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
}