如何使用百里香和春天填充下拉列表 [英] How do I populate a drop down with a list using thymeleaf and spring
本文介绍了如何使用百里香和春天填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
控制器类
pre>
@RequestMapping(value =/ generateIds,method = RequestMethod.GET)
public String launchPage(Model model){
List< Municipality>自治市= rCountryService.finaAllMunicipalities();
//假定列表中填充了值
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);
返回generateIds;
}
我不知道从哪里开始,所以我开始使用这个
< select th:field =* {countryName}>
< option value =default>选择国家< / option>
< option th:each =$ {countryName}th:value =$ {countryName}th:text =$ {countryName}/>
< / select>
我的html / controller应该如何将列表的所有元素添加为下拉选项?
预先感谢!
解决方案
下拉列表。我认为这可能有助于你对此有所了解。
控制器
列表与LT;操作> operators = operatorService.getAllOperaors()
model.addAttribute(operators,operators);
模型
@Entity
@Table(name =operator)
public class Operator {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =id)
@JsonIgnore
私人长ID;
@NotBlank(message =operator Name can not be empty)
@Column(name =operator_name,nullable = false)
private String operatorName;
getters and setters ...
}
查看
< div class =form-group blu-margin>
< option value =0th:text =select operator>< / option>
< option th:each =operator:$ {operators}th:value =$ {operator.id}th:text =$ {operator.operatorName}>< / option>
< / select>
< / div>
I need to populate a drop down with all the values within a list of strings.
Controller Class
@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";
}
I didn't know where to start for the HTML so I started with this
<select th:field="*{countryName }">
<option value="default">Select a country</option>
<option th:each="${countryName }" th:value="${countryName }"th:text="${countryName }"/>
</select>
What should my html/controller look like to add all of the elements of the list as drop down options?
Thanks in advance!
解决方案
This is how I populate the drop down list.I think it may help to you get some idea about it.
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" th:text="select operator" ></option>
<option th:each="operator : ${operators}" th:value="${operator.id}" th:text="${operator.operatorName}"></option>
</select>
</div>
这篇关于如何使用百里香和春天填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文