我根据枚举中的值创建下拉列表,然后尝试像其他字段一样使用th:value =“ $ {parameterName}”返回值,但是返回值为null。
控制器的get方法:
@Before
public void executedBeforeEach() {
//this method will execute before every single test
}
@Test
public void EmptyCollection() {
assertTrue(testList.isEmpty());
}
页面浏览量:
@GetMapping("/createorupdatebusvehicle/{id}")
public String createBusVehicleDisplay(Model model, @PathVariable(value = "id") long id, HttpServletResponse response) throws IOException {
BusVehicle busVehicle = busVehicleRepository.findById(id).get();
if(busVehicle == null){
response.sendRedirect("/createorupdatebusvehicle");
return null;
}
model.addAttribute("busVehicleId", id);
model.addAttribute("busVehicleColor", busVehicle.getColor().toString());
model.addAttribute("busVehicleType", busVehicle.getType().toString());
// all attributes are set
return "createOrUpdateBusVehicle";
}
然后将所选值返回到控制器中:
<form action="#" th:action="@{/createorupdatebusvehicle}" method="post">
<input type="hidden" name="busVehicleId" th:value="${busVehicleId}" />
<p>Plate number: <input type="text" name="busVehiclePlateNumber" th:value="${busVehiclePlateNumber}" /></p>
<p>Passenger capacity: <input type="text" name="busVehiclePassengerCapacity" th:value="${busVehiclePassengerCapacity}" /></p>
//== Here are the selects ==
<select name="color">
<option th:each="colorOpt : ${T(com.grazzini.model.BusVehicleColor).values()}"
th:value="${busVehicleColor}" th:text="${colorOpt}" th:selected="${busVehicleColor} == colorOpt"></option>
</select>
<select name="type">
<option th:each="typeOpt : ${T(com.grazzini.model.BusVehicleType).values()}"
th:value="${busVehicleType}" th:text="${typeOpt}" th:selected="${busVehicleType} == typeOpt"></option>
</select>
颜色和类型是枚举。所有其他请求都返回正确的值,例如对于文本字段。知道为什么这个人表现不同吗?
答案 0 :(得分:0)
您在有效的字段上有name="..."
。您需要在各自的name="busVehicleColor"
标签上添加name="busVehicleType"
和<select />
。