Answer the question
In order to leave comments, you need to log in
Spring MVC work with form?
Good afternoon!
It's no secret to anyone who has worked with Spring MVC that Spring has its own tag library. One of these tags will be my question. Namely, about the "form: form" tag, or rather about its modelAttribute attribute (analogous to the obsolete commandName).
Reading the documentation, at first I thought that this attribute is only used to fill the form with some initial data (ie, the basis for the form, backing object). And only for this, for nothing more.
But in the course of work, it turned out that this attribute also determines the object that will be sent by this form and will be received in the controller. But how does it happen? For what? Whereby? Through what mechanism? After all, as far as I know, HTML requests cannot pass objects - they can pass query parameters, but not objects.
If the question is not clear, then I will describe the details. If you understand, then you can not read further.
Just in case, the controller code:
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView main() {
User user = new User();
return new ModelAndView("index", "userObj", user);
}
@RequestMapping(value = "/check-user", method = RequestMethod.POST)
public String checkUser(@Valid @ModelAttribute(name = "userObj") User user, BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors()) return "index";
return "check-user";
}
<form:form class="box login" modelAttribute="userObj" method="POST" action="check-user">
<fieldset class="boxBody">
<label>Username</label>
<form:input path="name"/>
<form:errors path="name" cssClass="error" />
<label>Password</label>
<form:password path="password"/>
<form:errors path="password" cssClass="error"/>
</fieldset>
<footer>
<form:checkbox path="admin" />
<label>Admin?</label>
<input type="submit" class="btnLogin" value="Login" tabindex="4">
</footer>
</form:form>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question