Spring中的下拉列表中的Null值

时间:2018-10-22 12:34:54

标签: spring spring-mvc spring-boot thymeleaf

我有一个下拉列表,其中包含项目列表。我的问题是我想接收选择的值,但我却收到了null。

<form action="#" th:action="@{/cidashboard/table}" th:object="${projectVariantSelected}" method="post">
 <p>Project variants options:</p>
 <select class="form-control"  th:field="*{projectVariantId}" >
      <option  th:each = "projectVariant : ${projectsVariants}"
               th:value="${projectVariant.projectVariantId}"
               th:text="${projectVariant.projectVariantName}" >
      </option>
 </select>

<input type="submit" value="Submit" />
</form>

我创建一个GET请求:

public final Map< String, Object > modelAtribute = new HashMap< String, Object >();

    @GetMapping("cidashboard/filter/data")
    public String allDataForFilter(Model model, ModelAndView modelAndView) {
        model.addAttribute("projectsVariants", projectVariantService.findAllProjectsVariants());

       addAttributes(modelAtribute);

       return "test2";
    }

    @ModelAttribute
    public void addAttributes(Map<String, Object> model) {
        model.put("projectVariantSelected", new ProjectVariant());
    }

我的POST请求看起来像这样:

@PostMapping("cidashboard/table")
    public String createTable(@ModelAttribute("projectVariantSelected") ProjectVariant projectVariantSelected) {

        System.out.println("Project Variant selected: " + projectVariantSelected.getProjectVariantName() + " " + projectVariantSelected.getProjectVariantId());

        return "test1";
    }

当我尝试打印输出时,我收到:

Project Variant selected: null 0

1 个答案:

答案 0 :(得分:1)

请使用以下代码。在选择代码中将idname与值projectVariantId一起使用。

<select class="form-control" id="projectVariantId" name="projectVariantId"" >