【发布时间】:2021-12-14 15:16:28
【问题描述】:
我正在创建一个 Web 应用程序并且有一个问题是否可以在 Spring 中编写多个 ModelAttribute?
我尝试过这样的事情,但总是得到 0。我也尝试过使用弹簧绑定,但我再次得到 0。谢谢。
这是我的控制器。
@RequestMapping(value="/assignguesttohotel",method = RequestMethod.GET)
public ModelAndView assignguesttohotel_form(HttpServletResponse response) throws IOException {
return new ModelAndView("assignguesttohotel");
}
@RequestMapping(value="/assign",method = RequestMethod.POST)
public ModelAndView assigning(Guest guest, Hotel hotel) {
ModelAndView m = new ModelAndView();
System.out.println(guest.getId() + " " + hotel.getId());
m.addObject("guestid", guest.getId());
m.addObject("hotelid", hotel.getId());
HotelDAO dao = new HotelDAO();
dao.assignGuestToHotel(guest.getId(), hotel.getId());
m.setViewName("temp");
return m;
}
还有 JSP (assignguesttohotel.jsp)。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Guest register</title>
</head>
<body>
<div align="center">
<h1>Search Guest by name</h1>
<form:form action="assign" method="post">
<table>
<tr>
<td>Guest id:</td>
<td>
<%-- <spring:bind path="guestid.id">--%>
<%-- <input name="${status.expression}" type="number" value="${status.value}">--%>
<%-- </spring:bind>--%>
<input name="guestid" type="number">
</td>
</tr>
<tr>
<td>Hotel id:</td>
<td>
<%-- <spring:bind path="hotelid.id">--%>
<%-- <input name="${status.expression}" type="number" value="${status.value}">--%>
<%-- </spring:bind>--%>
<input name="hotelid" type="number">
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Seach"></td>
</tr>
</table>
</form:form>
</div>
</body>
</html>
【问题讨论】: