0

I have been doing school's project recently and started learning Java Web/Spring/Bootstrap and stuff only a week ago, so please do forgive and correct me if I got any idea wrong.

So I was working on some webpage following an online tutorial and it's really great that I can fetch user's input by using Thymeleaf tags like codes below

<div class="ip input-group" align="center">
    <input id="username" type="text" class="form-control" name="username" placeholder="Username" th:value="*{username}"/>
</div>

Though I'm having a hard time trying to fetch input from select box or radio button like codes below (am I doing anything wrong here?)

<div class="input-group">
    <select class="btn btn-default" th:value="*{sex}">
        <option>Male</option>
        <option>Fmale</option>
        <option>Other</option>
    </select>
</div>

Since it's able to fetch input from simple input area, I'm thinking that there should be a way to acquire input or data from select box or radio button by using Thymeleaf?

1 Answer 1

2
  1. th:field tag should be in <select> tag, but it does not exist.
  2. th:value tag should be in <option> tag, not in <select> tag.

As described in thymeleaf docs:

Select fields have two parts: the tag and its nested tags. When creating this kind of field, only the tag has to include a th:field attribute, but the th:value attributes in the nested tags will be very important because they will provide the means of knowing which is the currently selected option (in a similar way to non-boolean checkboxes and radio buttons).

Code snipped in source

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.