1 16 17 package org.springframework.web.servlet; 18 19 import java.io.IOException ; 20 import java.util.Locale ; 21 22 import javax.servlet.ServletException ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 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 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 request, HttpServletResponse response) 72 throws ServletException , IOException { 73 if (!(RequestContextUtils.getWebApplicationContext(request) instanceof SimpleWebApplicationContext)) { 74 throw new ServletException ("Incorrect WebApplicationContext"); 75 } 76 if (!(RequestContextUtils.getLocaleResolver(request) instanceof AcceptHeaderLocaleResolver)) { 77 throw new ServletException ("Incorrect LocaleResolver"); 78 } 79 if (!Locale.CANADA.equals(RequestContextUtils.getLocale(request))) { 80 throw new ServletException ("Incorrect Locale"); 81 } 82 return null; 83 } 84 85 public long getLastModified(HttpServletRequest 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 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 { 127 return new ModelAndView("view" + this.myInt); 128 } 129 } 130 131 } 132 | Popular Tags |