动态创建的下拉列表不返回值

问题描述 投票:0回答:1

我根据枚举中的值创建下拉列表,然后像其他字段一样尝试使用th:value =“ $ {parameterName}”返回值,但返回值为null。

控制器获取方法:

@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>

然后将选定的值返回到控制器中:

@PostMapping("/createorupdatebusvehicle")
public String checkAndCreateBusVehicle (HttpServletRequest request, HttpServletResponse response) throws IOException {
    String busVehicleId = request.getParameter("busVehicleId");
//...
    String busVehicleColor = request.getParameter("busVehicleColor"); //null
    String busVehicleType = request.getParameter("busVehicleType"); //null

/// the rest

颜色和类型是枚举。所有其他请求都返回正确的值,例如对于文本字段。知道为什么这个行为不同吗?

spring-mvc thymeleaf dropdown
1个回答
0
投票

您在正在使用的字段上有name="..."。您需要在各自的name="busVehicleColor"标签上添加name="busVehicleType"<select />

© www.soinside.com 2019 - 2024. All rights reserved.