0

In spring based web app using hibernate I have a lists.html file where are select options codes reused in many views, now options will be transferred to database and my task is to populate those lists from db. This code will be rarely updated.This is example of list and its usage.
lists.html

 <select th:fragment="typeList">
        <option selected value=""></option>
        <option th:value="|1|" th:text="|Type #1|"></option>
        <option th:value="|2|" th:text="|Type #2|"></option>
        <option th:value="|3|" th:text="|Type #3|"></option>
        <option th:value="|4|" th:text="|Type #4|"></option>
 </select>

usage

<li>
      <label for="type" th:text="#{type.label}">type</label>
      <select th:field="*{type}" th:include="html/fragments/lists :: typeList"></select>
</li>

What is the best method to build this lists.html from database dictionary?

jaba_2121
  • 1
  • 1

1 Answers1

1

My DTO

@Entity
@Table(name = "TableName")
public class Test {

    @Id
    private String Code;
    private String Name;
    private int price;

    public Test() {}

    public Test(String Code, String Name, int price) {
        this.Code = Code;
        this.Name = Name;
        this.price = price;
    }

    public String getCode() {
        return Code;
    }

    public String getName() {
        return Name;
    }

    public int getPrice() {
        return price;
    }
}

View

List<Test> test = new ArrayList<>();
    model.addAttribute("test", test);
    List<Test> tests = testRepository.findAll();
    model.addAttribute("tests", tests);

HTML

    <select class="form-control" id="Order" name="Order">
        <option value="">Select Test Order</option>
        <option th:each="test : ${tests}"
                th:value="${test.Code}"
                th:text="${test.Code}+' : '+${test.Name}"></option>
    </select>
</div>

Reference Reference link

Pratham
  • 259
  • 1
  • 8