spring model addObject(Object attributeName)的目的是什么?

时间:2016-07-18 00:39:20

标签: spring jsp spring-mvc

在spring mvc中,当创建ModelAndView时,有一个名为addObject(Object attributeName)单个参数的方法,我不明白如何使用它。我也看到了model.addAllObjects(Map<String, ?> object)

如何在jsp中获取该地图?或者那些方法的目的是什么?我只知道如何使用model.addObject("car", new Car())因为就像定义servlet参数一样。我在春天发现了这些信息,但我真的不明白。

Spring addObject and addAllObjects

2 个答案:

答案 0 :(得分:3)

请查看以下示例。我已经介绍了如何使用addObject(Object attributeValue)以及addAllObjects(Map<String, ?> modelMap)

<强> Car.java

public class Car {

    private String regNo;
    private String model;
    private String year;
    public String getRegNo() {
        return regNo;
    }
    public void setRegNo(String regNo) {
        this.regNo = regNo;
    }
    public String getModel() {
        return model;
    }
    public void setModel(String model) {
        this.model = model;
    }
    public String getYear() {
        return year;
    }
    public void setYear(String year) {
        this.year = year;
    }
}

<强> PageContent.java

public class PageContent {

    private String headerName;

    public String getHeaderName() {
        return headerName;
    }

    public void setHeaderName(String headerName) {
        this.headerName = headerName;
    }
}

控制器方法

@RequestMapping(value = "/showCars", method = RequestMethod.GET)
public ModelAndView showApp() {

    ModelAndView modelAndView = new ModelAndView();

    //adding a single attribute for the modelMap
    PageContent pageContent = new PageContent();
    pageContent.setHeaderName("All Cars - From Controller");
    modelAndView.addObject(pageContent);

    List<Car> carList = new ArrayList<>();

    Car car1 = new Car();
    car1.setModel("Toyota");
    car1.setRegNo("223456");
    car1.setYear("2005");

    Car car2 = new Car();
    car2.setModel("Mazda");
    car2.setRegNo("24244");
    car2.setYear("2015");

    Car car3 = new Car();
    car3.setModel("Nissan");
    car3.setRegNo("4465757");
    car3.setYear("2013");

    carList.add(car1);
    carList.add(car2);
    carList.add(car3);

    Map<String,Object> allObjectsMap = new HashMap<String,Object>();
    allObjectsMap.put("allCarObjects", carList);

    //adding a set of objects for the model map
    modelAndView.addAllObjects(allObjectsMap);

    modelAndView.setViewName("CarView");
    return modelAndView;
}

<强> CarView.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <!DOCTYPE html>
    <html>
    <head>
    <title>ModelAttribute Example</title>
    </head>
    <body>
        <h1>${pageContent.headerName}</h1>
        <table>
            <tr>
                <th>Model</th>
                <th>Registration No</th>
                <th>Year of Manufacture</th>
            </tr>

            <c:forEach var="car" items="${allCarObjects}">
                <tr>
                    <td><c:out value="${car.model}" /></td>

                    <td><c:out value="${car.regNo}" /></td>

                    <td><c:out value="${car.year}" /></td>
                </tr>
            </c:forEach>
        </table>
    </body>
    </html>

希望这对你有所帮助!

答案 1 :(得分:0)

好吧,第一个方法是addObject,它会在转发时调用并将数据带到jsp.Then你可以用jstl或其他东西迭代jsp中的数据。方法addAllObjects只是一个多类型的addObject,就像map&put;方法put和putAll。