简体   繁体   中英

spring thymeleaf combo box get selected value

I have this enum class,that has a set of values. I display the values in a combo box. How do I get the selected values?

@Entity
@Table(name = "accountant")
public class Accountant {

    @Column(name="country")
    private String country;

    public enum country{
        MALAYSA,GREECE
    }
}

In the controller I have already set it by doing modelAndView.addObject("currentAccountant", loggedInUser);

I list them all out by:

<form th:action="@{/updateAccountant}"  th:object="${currentAccountant}" class="form-inline"  method="post" id="companyProfileForm" name="companyProfileForm">

<select class="form-control selectpicker" data-live-search="true" style="width:300px;margin-left: 50px;"  >
    <option value="" selected disabled hidden>Please select a Country</option>
        <option th:each="country : ${T(Accountant.Country).values()}"
            th:value="${country}" 
            th:text="${country.getValue()}">
        </option>
    </select>
</form>

How do i get the selected value from the combo box in the form? And later set back the value when the form is loaded.

I tried this, but nothing gets listed in the combo box.:

<select th:field="*{country}" class="form-control selectpicker" data-live-search="true">

when i remove the th:field="*{country}" it displays the countries. <select class="form-control selectpicker" data-live-search="true">

So this took me some while to resolve, hack the solution.

  1. for the combo box, to get selected value , the th:field it should not be field name-column name variable country . It should be the enum list name Country Note the naming difference capital C

  2. To get all values of the combo box, you should do ${T(com.workspez.model.Accountant.States).getSortedValue()} You need to specify full package name .

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