KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > SimpleWebApplicationContext


1 /*
2  * Copyright 2002-2005 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;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 import org.springframework.beans.BeansException;
27 import org.springframework.beans.MutablePropertyValues;
28 import org.springframework.beans.PropertyValue;
29 import org.springframework.context.support.StaticMessageSource;
30 import org.springframework.ui.context.Theme;
31 import org.springframework.ui.context.ThemeSource;
32 import org.springframework.ui.context.support.SimpleTheme;
33 import org.springframework.ui.context.support.UiApplicationContextUtils;
34 import org.springframework.web.context.support.StaticWebApplicationContext;
35 import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
36 import org.springframework.web.servlet.mvc.Controller;
37 import org.springframework.web.servlet.mvc.LastModified;
38 import org.springframework.web.servlet.mvc.SimpleFormController;
39 import org.springframework.web.servlet.mvc.throwaway.ThrowawayController;
40 import org.springframework.web.servlet.support.RequestContextUtils;
41 import org.springframework.web.servlet.theme.AbstractThemeResolver;
42
43 /**
44  * @author Juergen Hoeller
45  * @since 21.05.2003
46  */

47 public class SimpleWebApplicationContext extends StaticWebApplicationContext {
48
49     public void refresh() throws BeansException {
50         MutablePropertyValues pvs = new MutablePropertyValues();
51         pvs.addPropertyValue(new PropertyValue("commandClass", "org.springframework.beans.TestBean"));
52         pvs.addPropertyValue(new PropertyValue("formView", "form"));
53         registerSingleton("/form.do", SimpleFormController.class, pvs);
54
55         registerSingleton("/locale.do", LocaleChecker.class);
56
57         registerPrototype("/throwaway.do", TestThrowawayController.class);
58
59         addMessage("test", Locale.ENGLISH, "test message");
60         addMessage("test", Locale.CANADA, "Canadian & test message");
61         addMessage("testArgs", Locale.ENGLISH, "test {0} message {1}");
62
63         registerSingleton(UiApplicationContextUtils.THEME_SOURCE_BEAN_NAME, DummyThemeSource.class);
64
65         super.refresh();
66     }
67
68
69     public static class LocaleChecker implements Controller, LastModified {
70
71         public ModelAndView handleRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
72                 throws ServletException JavaDoc, IOException JavaDoc {
73             if (!(RequestContextUtils.getWebApplicationContext(request) instanceof SimpleWebApplicationContext)) {
74                 throw new ServletException JavaDoc("Incorrect WebApplicationContext");
75             }
76             if (!(RequestContextUtils.getLocaleResolver(request) instanceof AcceptHeaderLocaleResolver)) {
77                 throw new ServletException JavaDoc("Incorrect LocaleResolver");
78             }
79             if (!Locale.CANADA.equals(RequestContextUtils.getLocale(request))) {
80                 throw new ServletException JavaDoc("Incorrect Locale");
81             }
82             return null;
83         }
84
85         public long getLastModified(HttpServletRequest JavaDoc request) {
86             return 98;
87         }
88     }
89
90
91     public static class DummyThemeSource implements ThemeSource {
92
93         private StaticMessageSource messageSource;
94
95         public DummyThemeSource() {
96             this.messageSource = new StaticMessageSource();
97             this.messageSource.addMessage("themetest", Locale.ENGLISH, "theme test message");
98             this.messageSource.addMessage("themetestArgs", Locale.ENGLISH, "theme test message {0}");
99         }
100
101         public Theme getTheme(String JavaDoc themeName) {
102             if (AbstractThemeResolver.ORIGINAL_DEFAULT_THEME_NAME.equals(themeName)) {
103                 return new SimpleTheme(AbstractThemeResolver.ORIGINAL_DEFAULT_THEME_NAME, this.messageSource);
104             }
105             else {
106                 return null;
107             }
108         }
109     }
110
111
112     public static class TestThrowawayController implements ThrowawayController {
113
114         public static int counter = 0;
115
116         private int myInt;
117
118         public TestThrowawayController() {
119             counter++;
120         }
121
122         public void setMyInt(int myInt) {
123             this.myInt = myInt;
124         }
125
126         public ModelAndView execute() throws Exception JavaDoc {
127             return new ModelAndView("view" + this.myInt);
128         }
129     }
130
131 }
132
Popular Tags