绑定选择选项列表



我目前不确定我应该如何做到这一点。当方法 = RequestMethod.POST 时,我应该如何将选择选取器绑定到我的对象。

这是我表格的一部分

<form th:action="@{/incidentDetail/update}" method="post" id="incidentDetailForm">
<div class="form-row">
    <div class="form-group col-md-6">
                <label for="location">Name</label> <input class="form-control"
                    type="text" name="ioName" id="ioName" th:value="${incident.ioName}" />
    </div>
       <div class="form-group col-md-3">
                <label for="location" class="cols-sm-2 control-label">Preparation
                    By</label><span class="bg-danger pull-right"></span>
                <div class="cols-sm-10">
                    <div class="input-group">
                        <span class="input-group-addon"><i class="fa fa-reorder fa"
                            aria-hidden="true"></i></span> <select class="form-control selectpicker"
                            th:object="${incident}" th:field="*{incidentPreparationBy}"
                            id="incidentPreparationBy" name="incidentPreparationBy"
                            roleId="incidentPreparationBy">
                            <option th:each="user: ${userList}" th:value="${incident.incidentPreparationBy}"
                                th:text="${user.name}"></option>
                        </select>
                    </div>
                </div>
    </div>
</form>

我的控制器

@RequestMapping(value = "/update", method = RequestMethod.POST)
    public String registerIncidentPost(@ModelAttribute("incident") Incident incident, HttpServletRequest request)
            throws Exception {
        incidentService.save(incident);
        return "redirect:/incidentDetail?id=" + incident.getId();
    }

事件实体的一部分

@ManyToOne
    @JoinColumn(name = "user_id_preparation_by")
    private User incidentPreparationBy;

您可以通过添加以下内容来访问选择:

@RequestParam("incidentPreparationBy") String option作为方法参数

到您的控制器方法。这样,字符串"选项"将包含所选值。

喜欢这个:

@RequestMapping(value = "/update", method = RequestMethod.POST)
public String registerIncidentPost(@ModelAttribute("incident") Incident incident, 
    @RequestParam("incidentPreparationBy") String option, HttpServletRequest request)
        throws Exception {
    String incidentPrepartionBy = option; //incidentPrepartionBy will give you the selected value now.
    incidentService.save(incident);
    return "redirect:/incidentDetail?id=" + incident.getId();
}

最新更新