gpt4 book ai didi

java - 从 Spring MVC 中的 Controller 获取选定的选项值

转载 作者:行者123 更新时间:2023-11-30 07:19:17 25 4
gpt4 key购买 nike

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

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

    @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";
}

我该怎么做?

最佳答案

 <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:form 中添加隐藏字段,例如

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

并将可选请求参数添加到您的 Controller 中,如下所示

 @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

....
}

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

关于java - 从 Spring MVC 中的 Controller 获取选定的选项值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37855203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com