use of org.springframework.web.servlet.theme.FixedThemeResolver in project spring-framework by spring-projects.
the class AbstractTagTests method createPageContext.
protected MockPageContext createPageContext() {
MockServletContext sc = new MockServletContext();
SimpleWebApplicationContext wac = new SimpleWebApplicationContext();
wac.setServletContext(sc);
wac.setNamespace("test");
wac.refresh();
MockHttpServletRequest request = new MockHttpServletRequest(sc);
MockHttpServletResponse response = new MockHttpServletResponse();
if (inDispatcherServlet()) {
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
LocaleResolver lr = new AcceptHeaderLocaleResolver();
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr);
ThemeResolver tr = new FixedThemeResolver();
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr);
request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac);
} else {
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
}
return new MockPageContext(sc, request, response);
}
use of org.springframework.web.servlet.theme.FixedThemeResolver in project spring-framework by spring-projects.
the class ViewResolverTests method xmlViewResolver.
@Test
@SuppressWarnings("deprecation")
public void xmlViewResolver() throws Exception {
this.wac.registerSingleton("testBean", TestBean.class);
this.wac.refresh();
TestBean testBean = (TestBean) this.wac.getBean("testBean");
org.springframework.web.servlet.view.XmlViewResolver vr = new org.springframework.web.servlet.view.XmlViewResolver();
vr.setLocation(new ClassPathResource("org/springframework/web/servlet/view/views.xml"));
vr.setApplicationContext(this.wac);
View view1 = vr.resolveViewName("example1", Locale.getDefault());
assertThat(TestView.class.equals(view1.getClass())).as("Correct view class").isTrue();
assertThat("/example1.jsp".equals(((InternalResourceView) view1).getUrl())).as("Correct URL").isTrue();
View view2 = vr.resolveViewName("example2", Locale.getDefault());
assertThat(JstlView.class.equals(view2.getClass())).as("Correct view class").isTrue();
assertThat("/example2new.jsp".equals(((InternalResourceView) view2).getUrl())).as("Correct URL").isTrue();
Map<String, Object> model = new HashMap<>();
TestBean tb = new TestBean();
model.put("tb", tb);
this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
this.request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
this.request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
view1.render(model, this.request, this.response);
assertThat(tb.equals(this.request.getAttribute("tb"))).as("Correct tb attribute").isTrue();
assertThat("testvalue1".equals(this.request.getAttribute("test1"))).as("Correct test1 attribute").isTrue();
assertThat(testBean.equals(this.request.getAttribute("test2"))).as("Correct test2 attribute").isTrue();
this.request.clearAttributes();
this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
this.request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
this.request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
view2.render(model, this.request, this.response);
assertThat(tb.equals(this.request.getAttribute("tb"))).as("Correct tb attribute").isTrue();
assertThat("testvalue1".equals(this.request.getAttribute("test1"))).as("Correct test1 attribute").isTrue();
assertThat("testvalue2".equals(this.request.getAttribute("test2"))).as("Correct test2 attribute").isTrue();
}
use of org.springframework.web.servlet.theme.FixedThemeResolver in project spring-framework by spring-projects.
the class ViewResolverTests method doTestUrlBasedViewResolverWithoutPrefixes.
private void doTestUrlBasedViewResolverWithoutPrefixes(UrlBasedViewResolver vr) throws Exception {
this.wac.refresh();
vr.setApplicationContext(this.wac);
vr.setContentType("myContentType");
vr.setRequestContextAttribute("rc");
View view = vr.resolveViewName("example1", Locale.getDefault());
assertThat(view).isInstanceOf(JstlView.class);
assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("example1");
assertThat(((InternalResourceView) view).getContentType()).as("Correct textContentType").isEqualTo("myContentType");
view = vr.resolveViewName("example2", Locale.getDefault());
assertThat(view).isInstanceOf(JstlView.class);
assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("example2");
assertThat(((InternalResourceView) view).getContentType()).as("Correct textContentType").isEqualTo("myContentType");
this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
this.request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
this.request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
Map<String, Object> model = new HashMap<>();
TestBean tb = new TestBean();
model.put("tb", tb);
view.render(model, this.request, this.response);
assertThat(tb.equals(this.request.getAttribute("tb"))).as("Correct tb attribute").isTrue();
boolean condition = this.request.getAttribute("rc") instanceof RequestContext;
assertThat(condition).as("Correct rc attribute").isTrue();
view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
assertThat(view.getClass()).as("Correct view class").isEqualTo(RedirectView.class);
assertThat(((RedirectView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
assertThat(((RedirectView) view).getApplicationContext()).as("View not initialized as bean").isSameAs(this.wac);
view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
assertThat(view.getClass()).as("Correct view class").isEqualTo(InternalResourceView.class);
assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
}
Aggregations