|
Spring Framework example source code file (AbstractView.java)
The Spring Framework AbstractView.java source code/* * Copyright 2002-2007 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.web.servlet.view; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; import java.util.StringTokenizer; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.BeanNameAware; import org.springframework.util.CollectionUtils; import org.springframework.web.context.support.WebApplicationObjectSupport; import org.springframework.web.servlet.View; import org.springframework.web.servlet.support.RequestContext; /** * Abstract base class for {@link org.springframework.web.servlet.View} * implementations. Subclasses should be JavaBeans, to allow for * convenient configuration as Spring-managed bean instances. * * <p>Provides support for static attributes, to be made available to the view, * with a variety of ways to specify them. Static attributes will be merged * with the given dynamic attributes (the model that the controller returned) * for each render operation. * * <p>Extends {@link WebApplicationObjectSupport}, which will be helpful to * some views. Subclasses just need to implement the actual rendering. * * @author Rod Johnson * @author Juergen Hoeller * @see #setAttributes * @see #setAttributesMap * @see #renderMergedOutputModel */ public abstract class AbstractView extends WebApplicationObjectSupport implements View, BeanNameAware { /** Default content type. Overridable as bean property. */ public static final String DEFAULT_CONTENT_TYPE = "text/html;charset=ISO-8859-1"; private String beanName; private String contentType = DEFAULT_CONTENT_TYPE; private String requestContextAttribute; /** Map of static attributes, keyed by attribute name (String) */ private final Map staticAttributes = new HashMap(); /** * Set the view's name. Helpful for traceability. * <p>Framework code must call this when constructing views. */ public void setBeanName(String beanName) { this.beanName = beanName; } /** * Return the view's name. Should never be <code>null, * if the view was correctly configured. */ public String getBeanName() { return this.beanName; } /** * Set the content type for this view. * Default is "text/html;charset=ISO-8859-1". * <p>May be ignored by subclasses if the view itself is assumed * to set the content type, e.g. in case of JSPs. */ public void setContentType(String contentType) { this.contentType = contentType; } /** * Return the content type for this view. */ public String getContentType() { return this.contentType; } /** * Set the name of the RequestContext attribute for this view. * Default is none. */ public void setRequestContextAttribute(String requestContextAttribute) { this.requestContextAttribute = requestContextAttribute; } /** * Return the name of the RequestContext attribute, if any. */ public String getRequestContextAttribute() { return this.requestContextAttribute; } /** * Set static attributes as a CSV string. * Format is: attname0={value1},attname1={value1} * <p>"Static" attributes are fixed attributes that are specified in * the View instance configuration. "Dynamic" attributes, on the other hand, * are values passed in as part of the model. */ public void setAttributesCSV(String propString) throws IllegalArgumentException { if (propString != null) { StringTokenizer st = new StringTokenizer(propString, ","); while (st.hasMoreTokens()) { String tok = st.nextToken(); int eqIdx = tok.indexOf("="); if (eqIdx == -1) { throw new IllegalArgumentException("Expected = in attributes CSV string '" + propString + "'"); } if (eqIdx >= tok.length() - 2) { throw new IllegalArgumentException( "At least 2 characters ([]) required in attributes CSV string '" + propString + "'"); } String name = tok.substring(0, eqIdx); String value = tok.substring(eqIdx + 1); // Delete first and last characters of value: { and } value = value.substring(1); value = value.substring(0, value.length() - 1); addStaticAttribute(name, value); } } } /** * Set static attributes for this view from a * <code>java.util.Properties object. * <p>"Static" attributes are fixed attributes that are specified in * the View instance configuration. "Dynamic" attributes, on the other hand, * are values passed in as part of the model. * <p>This is the most convenient way to set static attributes. Note that * static attributes can be overridden by dynamic attributes, if a value * with the same name is included in the model. * <p>Can be populated with a String "value" (parsed via PropertiesEditor) * or a "props" element in XML bean definitions. * @see org.springframework.beans.propertyeditors.PropertiesEditor */ public void setAttributes(Properties attributes) { CollectionUtils.mergePropertiesIntoMap(attributes, this.staticAttributes); } /** * Set static attributes for this view from a Map. This allows to set * any kind of attribute values, for example bean references. * <p>"Static" attributes are fixed attributes that are specified in * the View instance configuration. "Dynamic" attributes, on the other hand, * are values passed in as part of the model. * <p>Can be populated with a "map" or "props" element in XML bean definitions. * @param attributes Map with name Strings as keys and attribute objects as values */ public void setAttributesMap(Map attributes) { if (attributes != null) { Iterator it = attributes.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); Object key = entry.getKey(); if (!(key instanceof String)) { throw new IllegalArgumentException( "Invalid attribute key [" + key + "]: only Strings allowed"); } addStaticAttribute((String) key, entry.getValue()); } } } /** * Allow Map access to the static attributes of this view, * with the option to add or override specific entries. * <p>Useful for specifying entries directly, for example via * "attributesMap[myKey]". This is particularly useful for * adding or overriding entries in child view definitions. */ public Map getAttributesMap() { return this.staticAttributes; } /** * Add static data to this view, exposed in each view. * <p>"Static" attributes are fixed attributes that are specified in * the View instance configuration. "Dynamic" attributes, on the other hand, * are values passed in as part of the model. * <p>Must be invoked before any calls to Other Spring Framework examples (source code examples)Here is a short list of links related to this Spring Framework AbstractView.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.