Java 使用thymeleaf从spring mvc应用程序中的数据库生成html选择选项
在使用hibernate的基于spring的web应用程序中,我有一个lists.html文件,其中的选择选项代码在许多视图中重复使用,现在选项将传输到数据库,我的任务是从db填充这些列表。此代码很少更新。这是列表及其用法的示例。Java 使用thymeleaf从spring mvc应用程序中的数据库生成html选择选项,java,spring,hibernate,spring-mvc,thymeleaf,Java,Spring,Hibernate,Spring Mvc,Thymeleaf,在使用hibernate的基于spring的web应用程序中,我有一个lists.html文件,其中的选择选项代码在许多视图中重复使用,现在选项将传输到数据库,我的任务是从db填充这些列表。此代码很少更新。这是列表及其用法的示例。 lists.html <select th:fragment="typeList"> <option selected value=""></option> <option th:value=
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>
用法
类型
从数据库字典构建此lists.html的最佳方法是什么?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;
}
}
看法
List test=newarraylist();
model.addAttribute(“test”,test);
List tests=testRepository.findAll();
model.addAttribute(“测试”,测试);
HTML
选择测试顺序
参考文献
@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;
}
}
List<Test> test = new ArrayList<>();
model.addAttribute("test", test);
List<Test> tests = testRepository.findAll();
model.addAttribute("tests", tests);
<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>