【问题标题】:Java Spring multiple ModelAttributeJava Spring 多个 ModelAttribute
【发布时间】: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>

【问题讨论】:

    标签: java spring jsp


    【解决方案1】:

    是的,您可以使用多个模型属性在控制器中接收多个对象,我们也可以将多个对象发送到视图页面。

    <form action="" method="post">
      <div>
      <label>Leave a comment below</label>
      <textarea th:field="${rootComment.text}" rows="3"></textarea>
       </div>
    <textarea th:field="${anotherObject.text}" rows="3"></textarea>
       </div>
      </form>
    
    
    
     Controller
        @PostMapping("/receive")
        String receiveObjects(Comment rootComment, Object2 anotherObject){
         ........
         }
    

    我正在使用百里香叶,所以我提到了“th”百里香叶标签。

    【讨论】:

      猜你喜欢
      • 2012-01-01
      • 1970-01-01
      • 2012-10-25
      • 2016-05-26
      • 2012-12-20
      • 1970-01-01
      • 2012-05-05
      • 2015-12-24
      • 2017-02-23
      相关资源
      最近更新 更多