简体   繁体   中英

Spring MVC + Thymeleaf Post Single Object to Controller

I'm sort-of shocked that I can't find an example of how to do this. Every time I google it, I get info on how to post a collection of objects, or other unrelated stuff. The thymeleaf documentation (what I can find of it) seems to not explain much either, like there is a lot of assumed knowledge.

Getting back to my question, I just want to post a single object (bean) from a form. I would like my controller mapping method to bind to this "pojo" bean and not to a bunch of strings/integers.

The only thing that I have found that comes close is stuff on StackOverflow where half of the code is in the question, the other half is in the answer, and there are always a few comments from people saying it didn't work for them.

Can anyone offer any relief here with a plain old boring example?

Can find the below code snippet might helpful for you.

Controller GET/POST mapping:

@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 Content:

<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 class

public class Registration {

 protected String firstName;

 protected String lastName;

 protected String userName;

 protected String password;

 protected String email;

 //Setter and Getter

}       

Use @ModelAttribute annotation in the parameter.

Something like this.

@RequestMapping(value = "/someurl", method = RequestMethod.POST)
public String savePojo(@ModelAttribute PojoClass pojo, Model model) {
    //Code
}

Edit : This answer has very good info on this. What is @ModelAttribute in Spring MVC?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM