2

I'm trying to persist values in database from a dropdown list in Spring Boot, thymeleaf but the database is populated with empty values.

Apparently the controller doesn't pass the value.

While I can fetch and display the values in GET but I cannot persist them in POST.

I'm not able to get a clear example from anywhere and even the examples on Thymeleaf are not clear.

Please help with some good example or a solution.

The code is as below.

@Controller
public class HomeController {

    @RequestMapping(value = "/signup", method = RequestMethod.POST)
    public String signupPost(@ModelAttribute("user") User user,
        Model model, BindingResult result) {

        if (!result.hasErrors()) {
            userService.saveUser(user);
        }
        return "redirect:/";

    }

}

The User class is as below

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "user_id", nullable = false, updatable = false)
    private Long userId;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "award_partner_id")
    private AwardPartner awardPartner;

    getters and setters....
}

The HTML snippet is here:

<!--Award Partner-->
<div class="form-group">
    <label class="col-md-4 control-label">Award Partner</label>
    <div class="col-md-6 selectContainer">
        <div class="input-group">
            <span class="input-group-addon"><i
                class="glyphicon glyphicon-list"></i></span> 
                <select name="awardPartner" roleId="awardPartner" id="awardPartner"
                th:field="*{awardPartner}" class="form-control selectpicker">
                <option value="">Select Award Partner</option>
                <option th:each="awardPartner : ${awardPartners}"
                    th:value="${awardPartner.id}"
                    th:text="${awardPartner.title}"></option>
            </select>
        </div>
    </div>
</div>

<!-- end snippet -->
4
  • In your HTML, there is nothing about an user. Commented Oct 15, 2017 at 12:20
  • @SébastienTemprado apologies but I didn't get you. I've not posted the user part as it is persisting fine. Only the County and AwardPartner snippet are facing an issue. Commented Oct 15, 2017 at 13:45
  • What do you get in the user object under awardPartner in the post request? Commented Oct 15, 2017 at 14:18
  • I get null under AwardPartner in post request Commented Oct 15, 2017 at 14:27

1 Answer 1

2

In your form tag, I think there is th:object="user".

In your select tag, there is th:field="*{awardPartner}". It means that you will put the selected value (the value of the selected option tag) in the field awardPartner of the object user. This field awardPartner is of type AwardPartner

In the option values, you have got an id (int or string?) as value but not an object of type AwardPartner

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.