gpt4 book ai didi

java - 如何使用 thymeleaf 和 spring 用列表填充下拉列表

转载 作者:IT老高 更新时间:2023-10-28 13:54:34 25 4
gpt4 key购买 nike

我需要用字符串列表中的所有值填充下拉列表。

Controller 类

@RequestMapping(value = "/generateIds", method = RequestMethod.GET)
public String launchPage(Model model) {
List<Municipality> municipalities = rCountryService.finaAllMunicipalities();
//assume the list is populated with values
List<String> countryName = new ArrayList<String>();
for(int i=0; i<municipalities.size(); i++) {
countryName.add(municipalities.get(i).getCountry().toString());
}
model.addAttribute("countryName ", countryName );

return "generateIds";
}

我不知道从哪里开始编写 HTML,所以我从这个开始

<select th:field="*{countryName }">
<option value="default">Select a country</option>
<option th:each="${countryName }" th:value="${countryName }"th:text="${countryName }"/>
</select>

将列表的所有元素添加为下拉选项,我的 html/ Controller 应该是什么样子?

提前致谢!

最佳答案

这就是我填充下拉列表的方式。我认为这可能有助于您对此有所了解。

Controller

List<Operator> operators =  operatorService.getAllOperaors()
model.addAttribute("operators", operators);

型号

  @Entity
@Table(name = "operator")
public class Operator {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@JsonIgnore
private Long id;

@NotBlank(message="operator Name cannot be empty")
@Column(name = "operator_name", nullable = false)
private String operatorName;

getters and setters ...

}

查看

<div class="form-group blu-margin">
<select class="form-control" th:field="${operator.opeId}" id="dropOperator">
<option value="0">select operator</option>
<option th:each="operator : ${operators}" th:value="${operator.id}" th:text="${operator.operatorName}"></option>
</select>
</div>

关于java - 如何使用 thymeleaf 和 spring 用列表填充下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37715276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com