如何使用thymeleaf和spring填充带有列表的下拉列表
我需要使用字符串列表中的所有值填充下拉列表。
控制器类
@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);
Model
@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 ...
}
View
<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>
以上是 如何使用thymeleaf和spring填充带有列表的下拉列表 的全部内容, 来源链接: utcz.com/qa/410575.html