KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > support > JspAwareRequestContext


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.support;
18
19 import java.util.Locale JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.jsp.PageContext JavaDoc;
24
25 /**
26  * JSP-aware subclass of RequestContext, allowing population of the context
27  * from a JSP PageContext.
28  *
29  * <p>This context will also detect a JSTL locale attribute in page scope,
30  * in addition to the fallback locale strategy provided by the base class.
31  *
32  * @author Juergen Hoeller
33  * @since 1.1.4
34  * @see #getFallbackLocale
35  */

36 public class JspAwareRequestContext extends RequestContext {
37
38     protected static final String JavaDoc PAGE_SCOPE_SUFFIX = ".page";
39
40     private PageContext JavaDoc pageContext;
41
42
43     /**
44      * Create a new JspAwareRequestContext for the given page context,
45      * using the request attributes for Errors retrieval.
46      * @param pageContext current JSP page context
47      */

48     public JspAwareRequestContext(PageContext JavaDoc pageContext) {
49         initContext(pageContext, null);
50     }
51
52     /**
53      * Create a new JspAwareRequestContext for the given page context,
54      * using the given model attributes for Errors retrieval.
55      * @param pageContext current JSP page context
56      * @param model the model attributes for the current view
57      * (can be <code>null</code>, using the request attributes for Errors retrieval)
58      */

59     public JspAwareRequestContext(PageContext JavaDoc pageContext, Map JavaDoc model) {
60         initContext(pageContext, model);
61     }
62
63     /**
64      * Initialize this context with the given page context,
65      * using the given model attributes for Errors retrieval.
66      * @param pageContext current JSP page context
67      * @param model the model attributes for the current view
68      * (can be <code>null</code>, using the request attributes for Errors retrieval)
69      */

70     protected void initContext(PageContext JavaDoc pageContext, Map JavaDoc model) {
71         if (!(pageContext.getRequest() instanceof HttpServletRequest JavaDoc)) {
72             throw new IllegalArgumentException JavaDoc("RequestContext only supports HTTP requests");
73         }
74         this.pageContext = pageContext;
75         initContext((HttpServletRequest JavaDoc) pageContext.getRequest(), pageContext.getServletContext(), model);
76     }
77
78
79     /**
80      * Return the underlying PageContext.
81      * Only intended for cooperating classes in this package.
82      */

83     protected PageContext JavaDoc getPageContext() {
84         return pageContext;
85     }
86
87     /**
88      * This implementation looks for a JSTL locale attribute in the
89      * JSP page scope, falling back to the superclass if not found.
90      * @see RequestContext#getFallbackLocale
91      */

92     protected Locale JavaDoc getFallbackLocale() {
93         Locale JavaDoc locale = (Locale JavaDoc) getPageContext().getAttribute(JSTL_LOCALE_ATTRIBUTE);
94         if (locale == null) {
95             locale = (Locale JavaDoc) getPageContext().getAttribute(JSTL_LOCALE_ATTRIBUTE + PAGE_SCOPE_SUFFIX);
96             if (locale == null) {
97                 locale = super.getFallbackLocale();
98             }
99         }
100         return locale;
101     }
102
103 }
104
Popular Tags