Spring mvc 提交时字段为空,但在控制器端填充了对象
当我第一次显示我的页面时,字段的值是正确的。但是当我在下面的方法Spring mvc 提交时字段为空,但在控制器端填充了对象,spring-mvc,spring-boot,thymeleaf,Spring Mvc,Spring Boot,Thymeleaf,当我第一次显示我的页面时,字段的值是正确的。但是当我在下面的方法addCouloir中提交表单时,字段显示为空。然而,topologie实例在addCouloir方法的最后一行仍然有很好的价值(我已经在调试模式下检查了该对象) topologie对象的同一实例的其他字段将在同一屏幕上很好地显示。因此,它可能与select/disabled类型的字段有关 @Controller @SessionAttributes(value = "topologie", types = { Topologie.
addCouloir
中提交表单时,字段显示为空。然而,topologie实例在addCouloir方法的最后一行仍然有很好的价值(我已经在调试模式下检查了该对象)
topologie对象的同一实例的其他字段将在同一屏幕上很好地显示。因此,它可能与select/disabled
类型的字段有关
@Controller
@SessionAttributes(value = "topologie", types = { Topologie.class })
@RequestMapping("/bus/topologieInstanceCouloir")
public class TopologieInstanceCouloirController {
...
@RequestMapping(method = RequestMethod.POST, params = { "addCouloir" })
public String addCouloir(final Topologie topologie, final Model model,
final HttpServletRequest req) throws IOException {
final String param = req.getParameter("addCouloir");
logger.info("REST request to add Couloir : {}", param);
final Matcher matcher = patternPartitionInstance.matcher(param);
if (matcher.find()) {
final Integer partitionId = Integer.valueOf(matcher.group(1));
final Integer instanceId = Integer.valueOf(matcher.group(2));
logger.info("Add a new Couloir on Instance {} on partition {}",
instanceId, partitionId);
topologie.getPartitions().get(partitionId.intValue())
.getInstances().get(instanceId).getCouloirs()
.add(new Couloir());
}
return VIEW_TOPOLOGIE_INSTANCECOULOIR;
}
html/thymeleaf topologieInstanceCouloir.html对应代码:
<form action="#" th:action="@{/bus/topologieInstanceCouloir}"
th:object="${topologie}" method="post" class="form-horizontal">
...
<div class="form-group"
th:if="${#bools.isFalse(topologie.isPassageCvs)}">
<label th:for="*{environnement}" class="col-sm-2 control-label">Environnement</label>
<div class="col-sm-10">
<select th:field="*{environnement}" class="form-control"
th:disabled="disabled">
<option th:each="environnement : ${allEnvironnement}"
th:value="${environnement}" th:text="${environnement}">...</option>
</select>
</div>
</div>
已“禁用”的元素不会提交其数据。由于topologie位于会话上(@SessionAttributes(value=“topologie”,types={topologie.class})
),这意味着无论select的值是什么,它都将保留其原始值
(我不完全确定您所说的“良好价值”是什么意思)将
@ModelAttribute(“topologie”)
放在您的addCouloir
方法上。@Patrick,谢谢您的建议,但它不会改变任何东西。正如在问题中所说,在addCouloir方法末尾的java端,对象很好:Environment
字段填充了正确的值。所谓良好值,是指它保持其原始值。这就是我想要的,这只是一种在不允许更改的情况下调用用户此值的方法。即使删除disabled
属性,environment值也为空。这是我不理解的行为:我不知道值在哪里更改。
<div class="col-sm-10">
<button type="submit" class="btn btn-default"
name="addCouloir"
th:value="'partitions[' + ${rowPartitionStat.index} + '].instances[' + ${rowInstanceStat.index} + ']'">Ajouter
couloir</button>
</div>
public class Topologie {
private String environnement;
...
public String getEnvironnement() {
return environnement;
}
public void setEnvironnement(String environnement) {
this.environnement = environnement;
}
}