001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.servlet.support;
018:
019: import java.util.Locale;
020: import java.util.Map;
021:
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.jsp.PageContext;
024:
025: /**
026: * JSP-aware subclass of RequestContext, allowing population of the context
027: * from a JSP PageContext.
028: *
029: * <p>This context will also detect a JSTL locale attribute in page scope,
030: * in addition to the fallback locale strategy provided by the base class.
031: *
032: * @author Juergen Hoeller
033: * @since 1.1.4
034: * @see #getFallbackLocale
035: */
036: public class JspAwareRequestContext extends RequestContext {
037:
038: protected static final String PAGE_SCOPE_SUFFIX = ".page";
039:
040: private PageContext pageContext;
041:
042: /**
043: * Create a new JspAwareRequestContext for the given page context,
044: * using the request attributes for Errors retrieval.
045: * @param pageContext current JSP page context
046: */
047: public JspAwareRequestContext(PageContext pageContext) {
048: initContext(pageContext, null);
049: }
050:
051: /**
052: * Create a new JspAwareRequestContext for the given page context,
053: * using the given model attributes for Errors retrieval.
054: * @param pageContext current JSP page context
055: * @param model the model attributes for the current view
056: * (can be <code>null</code>, using the request attributes for Errors retrieval)
057: */
058: public JspAwareRequestContext(PageContext pageContext, Map model) {
059: initContext(pageContext, model);
060: }
061:
062: /**
063: * Initialize this context with the given page context,
064: * using the given model attributes for Errors retrieval.
065: * @param pageContext current JSP page context
066: * @param model the model attributes for the current view
067: * (can be <code>null</code>, using the request attributes for Errors retrieval)
068: */
069: protected void initContext(PageContext pageContext, Map model) {
070: if (!(pageContext.getRequest() instanceof HttpServletRequest)) {
071: throw new IllegalArgumentException(
072: "RequestContext only supports HTTP requests");
073: }
074: this .pageContext = pageContext;
075: initContext((HttpServletRequest) pageContext.getRequest(),
076: pageContext.getServletContext(), model);
077: }
078:
079: /**
080: * Return the underlying PageContext.
081: * Only intended for cooperating classes in this package.
082: */
083: protected PageContext getPageContext() {
084: return pageContext;
085: }
086:
087: /**
088: * This implementation looks for a JSTL locale attribute in the
089: * JSP page scope, falling back to the superclass if not found.
090: * @see RequestContext#getFallbackLocale
091: */
092: protected Locale getFallbackLocale() {
093: Locale locale = (Locale) getPageContext().getAttribute(
094: JSTL_LOCALE_ATTRIBUTE);
095: if (locale == null) {
096: locale = (Locale) getPageContext().getAttribute(
097: JSTL_LOCALE_ATTRIBUTE + PAGE_SCOPE_SUFFIX);
098: if (locale == null) {
099: locale = super.getFallbackLocale();
100: }
101: }
102: return locale;
103: }
104:
105: }
|