All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.ui.ModelMap Maven / Gradle / Ivy

There is a newer version: 5.3.34
Show newest version
/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.ui;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.springframework.core.Conventions;
import org.springframework.util.Assert;

/**
 * Implementation of {@link java.util.Map} for use when building model data for use
 * with UI tools. Supports chained calls and generation of model attribute names.
 *
 * 

This class serves as generic model holder for both Servlet and Portlet MVC, * but is tied to neither of those. * * @author Rob Harrop * @since 2.0 * @see Conventions#getVariableName * @see org.springframework.web.servlet.ModelAndView * @see org.springframework.web.portlet.ModelAndView */ public class ModelMap extends HashMap { /** * Construct a new, empty ModelMap. */ public ModelMap() { } /** * Construct a new ModelMap containing the supplied model object * under the supplied name. * @see #addObject(String, Object) */ public ModelMap(String modelName, Object modelObject) { addObject(modelName, modelObject); } /** * Construct a new ModelMap containing the supplied model object. * Uses attribute name generation to generate the key for the supplied model * object. * @see #addObject(Object) */ public ModelMap(Object modelObject) { addObject(modelObject); } /** * Add the supplied Object under the supplied name. * @param modelName the name of the model attribute (never null) * @param modelObject the model attribute object (can be null) */ public ModelMap addObject(String modelName, Object modelObject) { Assert.notNull(modelName, "Model name must not be null"); this.put(modelName, modelObject); return this; } /** * Add the supplied Object to this Map used a * {@link org.springframework.core.Conventions#getVariableName generated name}. *

Note: Empty {@link Collection Collections} are not added to * the model when using this method because we cannot correctly determine * the true convention name. View code should check for null rather than * for empty collections as is already done by JSTL tags. * @param modelObject the model attribute object (never null) */ public ModelMap addObject(Object modelObject) { Assert.notNull(modelObject, "Model object must not be null"); if (modelObject instanceof Collection && ((Collection) modelObject).isEmpty()) { return this; } return addObject(Conventions.getVariableName(modelObject), modelObject); } /** * Copy all objects in the supplied Map into this Map. */ public ModelMap addAllObjects(Map objects) { if (objects != null) { this.putAll(objects); } return this; } /** * Copy all objects in the supplied Collection into this Map, * using attribute name generation for each element. * @see #addObject(Object) */ public ModelMap addAllObjects(Collection objects) { if (objects != null) { for (Iterator it = objects.iterator(); it.hasNext();) { addObject(it.next()); } } return this; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy