从Spring MVC中的控制器获取选定的选项值

时间:2016-06-16 09:31:20

标签: java jsp spring-mvc

enter image description here

我正在制作一个Spring MVC项目,我在.jsp中有这个表单

JSP

 <form:form action="/admin/assign/add" modelAttribute="cafeTable">
    <table>
        <tr>
            <td>
                <form:label path="tableNumber">
                    <spring:message text="Table's Number"/>
                </form:label>
            </td>
            <td>
            <form:select path="tableNumber" action = "select">
                <form:options items="${tableNumbers}"></form:options>
            </form:select>
            </td>
        </tr>
        <tr>
            <td>
                <form:label path="${user.fullName}">
                    <spring:message text="Waiter's name"/>
                </form:label>
            </td>
            <td>
            <form:select path="${user.fullName}" action = "select">
                <c:forEach items="${listUser}" var="user">
                    <option value="${user.fullName}">${user.fullName}</option>
                </c:forEach>
            </form:select>
            </td>
        </tr>
    </table>
    <br>
    <input type="submit" name="assign"
           value="<spring:message text="Assign"/>"/>
</form:form>

所以我有UserCafeTable个模型,但我的模型属性为CafeTable

使用提交按钮,我需要在控制器中获取两个选定的值:

    @RequestMapping(value = "/admin/assign/add", params = "assign", method = RequestMethod.POST)
public String assign(@ModelAttribute("cafeTable") CafeTable cafeTable) {

    //set cafeTable's UserID field matching selected fullName value

    tableService.addTable(cafeTable);
    return "admin";
}

我该怎么做?

1 个答案:

答案 0 :(得分:1)

 <form:form action="/admin/assign/add" modelAttribute="cafeTable">
    <table>
        <tr>
            <td>
                <form:label path="tableNumber">
                    <spring:message text="Table's Number"/>
                </form:label>
            </td>
            <td>
 <input type="hidden" value="${user.fullName}" name="fullname">
            <form:select path="tableNumber" action = "select">
                <form:options items="${tableNumbers}"></form:options>
            </form:select>
            </td>
        </tr>
        <tr>
            <td>
                <form:label path="${user.fullName}">
                    <spring:message text="Waiter's name"/>
                </form:label>
            </td>
            <td>
            <form:select path="${user.fullName}" action = "select">
                <c:forEach items="${listUser}" var="user">
                    <option value="${user.fullName}">${user.fullName}</option>
                </c:forEach>
            </form:select>
            </td>
        </tr>
    </table>
    <br>
    <input type="submit" name="assign"
           value="<spring:message text="Assign"/>"/>
</form:form>

如果您在CafeTable中没有fullname属性,请尝试在表单中添加隐藏字段:form like

<input type="hidden" value="${user.fullName}" name="fullname">

并在您的控制器中添加一个可选的请求参数,如下所示

 @RequestMapping(value = "/admin/assign/add", method = RequestMethod.POST)
        public String save(@RequestParam("fullname") String fullname,@Valid @ModelAttribute("cafeTable") CafeTable cafeTable) {

//now you can access full name from fullname variable here

            ....
        }

我还没有测试过这段代码。