Spring boot 未使用thymeleaf复选框正确设置所选元素的列表
在我的网页中,我想显示一个包含所有性质的列表,并希望在列表中设置选定的性质 我的问题是,在我显示的列表中正确设置了列表,但在所选元素列表中未正确设置:自然。实际上,存储的不是我的属性字符串“nom”,而是它的引用:Spring boot 未使用thymeleaf复选框正确设置所选元素的列表,spring-boot,thymeleaf,Spring Boot,Thymeleaf,在我的网页中,我想显示一个包含所有性质的列表,并希望在列表中设置选定的性质 我的问题是,在我显示的列表中正确设置了列表,但在所选元素列表中未正确设置:自然。实际上,存储的不是我的属性字符串“nom”,而是它的引用:fr.cnamts.navigo.domain。Nature@19c4a02按实例 以下是复选框显示: 我想要的:当我通过实例选中2 fist复选框时,我想在我的控制器中设置列表属性的值 natures.get(0).nom = "bcmc_envtraco_out" natures
fr.cnamts.navigo.domain。Nature@19c4a02
按实例
以下是复选框显示:
我想要的:当我通过实例选中2 fist复选框时,我想在我的控制器中设置列表属性
的值
natures.get(0).nom = "bcmc_envtraco_out"
natures.get(0).routage = INSTANCE
natures.get(1).nom = "bcmc_medtab_out"
natures.get(1).routage = INSTANCE
以前它工作时,复选框是一个列表
,我认为出现问题是因为我使用列表
做了一些错误的事情
以下是我的相关网页代码:
<form action="#" th:action="@{/bus/topologie}"
th:object="${topologie}" method="post" class="form-horizontal">
<div class="col-sm-10" th:if="!${#lists.isEmpty(allNature)}">
<div th:each="nature : ${allNature}" class="checkbox">
<label th:for="${#ids.next('nature')}">
<input type="checkbox" th:field="*{natures}" th:value="${nature}" class="checkboxNature" />
<span th:text="${nature.nom}" class="col-sm-5">...</span>
<span th:text="${nature.routage.nom}" class="col-sm-5">...</span>
</label>
</div>
尝试将您的th:value属性值更改为${nature.nom} 就这样,
<input type="checkbox" th:field="*{natures}" th:value="${nature.nom}" class="checkboxNature" />
谢谢您的回答,但它不会改变行为。好的,我检查了它,Thymeleaf应该自己处理th:checked属性。然后尝试将th:value更改为th:value=“${nature.nom}”。非常感谢。您可以使用此选项更改您的答案,我将验证它;)我很高兴能帮助你。
@ModelAttribute("allNature")
public List<Nature> getAllNatures(Topologie topologie) throws Exception
{
return natureService.getNaturesByVersionCadre(topologie.getCadre(), topologie.getVersionCadre());
}
public class Topologie {
private List<Nature> natures = new ArrayList<Nature>();
public class Nature {
@NotBlank
private String nom;
@NotNull
private Routage routage;
// @NotNull
// private String typeCl;
public enum Routage {
INSTANCE("Routage sur instance", "^[A-Za-z0-9]{2}$ (instance)"), UCANSS(
"Routage sur code UCANSS", "^[A-Za-z0-9]{2}$ (Code UCANSS)"), ACOSS(
"Routage sur code ACOSS", "^[A-Za-z0-9]{2}$ (Code ACOSS)"), INSTANCE_MIAM(
"Routage sur code instance MIAM", "^[A-Za-z0-9]{2}$ (instance)"), CODEREGIME_CODECAISSE(
"Routage sur coderégime+code caisse",
"^[0-9]{2}[0-9]{3}$ (Code régime code caisse)");
private final String nomRoutage;
// private final String codecle;
private final String regExp;
Routage(String nom, String regexp) {
this.nomRoutage = nom;
this.regExp = regexp;
}
public String getNom() {
return nomRoutage;
}
public String getRegExp() {
return regExp;
}
}
public Nature(String nomNature) {
nom = nomNature;
// TODO à modifier une fois récup faite dans fichier zk
routage = Routage.INSTANCE;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public Routage getRoutage() {
// TODO à modifier une fois récup faite dans fichier zk
if (routage == null) {
routage = Routage.INSTANCE;
}
return routage;
}
public void setRoutage(Routage routage) {
// TODO à modifier une fois récup faite dans fichier zk
if (routage == null) {
routage = Routage.INSTANCE;
}
this.routage = routage;
}
}
<input type="checkbox" th:field="*{natures}" th:value="${nature.nom}" class="checkboxNature" />