Search in sources :

Example 6 with ThemeChangeInterceptor

use of org.springframework.web.servlet.theme.ThemeChangeInterceptor in project tutorials by eugenp.

the class AppConfig method themeChangeInterceptor.

@Bean
public ThemeChangeInterceptor themeChangeInterceptor() {
    ThemeChangeInterceptor interceptor = new ThemeChangeInterceptor();
    interceptor.setParamName("theme");
    return interceptor;
}
Also used : ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 7 with ThemeChangeInterceptor

use of org.springframework.web.servlet.theme.ThemeChangeInterceptor in project spring-framework by spring-projects.

the class MvcNamespaceTests method testViewControllers.

@Test
public void testViewControllers() throws Exception {
    loadBeanDefinitions("mvc-config-view-controllers.xml");
    RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
    assertThat(mapping).isNotNull();
    mapping.setDefaultHandler(handlerMethod);
    BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
    assertThat(beanNameMapping).isNotNull();
    assertThat(beanNameMapping.getOrder()).isEqualTo(2);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    HandlerExecutionChain chain = mapping.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(3);
    assertThat(chain.getInterceptorList().get(0) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(1) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof ThemeChangeInterceptor).isTrue();
    SimpleUrlHandlerMapping mapping2 = appContext.getBean(SimpleUrlHandlerMapping.class);
    assertThat(mapping2).isNotNull();
    SimpleControllerHandlerAdapter adapter = appContext.getBean(SimpleControllerHandlerAdapter.class);
    assertThat(adapter).isNotNull();
    request = new MockHttpServletRequest("GET", "/foo");
    chain = mapping2.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(4);
    assertThat(chain.getInterceptorList().get(1) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(3) instanceof ThemeChangeInterceptor).isTrue();
    ModelAndView mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertThat((Object) mv.getViewName()).isNull();
    request = new MockHttpServletRequest("GET", "/myapp/app/bar");
    request.setContextPath("/myapp");
    request.setServletPath("/app");
    chain = mapping2.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(4);
    assertThat(chain.getInterceptorList().get(1) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(3) instanceof ThemeChangeInterceptor).isTrue();
    mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertThat(mv.getViewName()).isEqualTo("baz");
    request = new MockHttpServletRequest("GET", "/myapp/app/");
    request.setContextPath("/myapp");
    request.setServletPath("/app");
    chain = mapping2.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(4);
    assertThat(chain.getInterceptorList().get(1) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(3) instanceof ThemeChangeInterceptor).isTrue();
    mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertThat(mv.getViewName()).isEqualTo("root");
    request = new MockHttpServletRequest("GET", "/myapp/app/old");
    request.setContextPath("/myapp");
    request.setServletPath("/app");
    request.setQueryString("a=b");
    chain = mapping2.getHandler(request);
    mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertThat(mv.getView()).isNotNull();
    assertThat(mv.getView().getClass()).isEqualTo(RedirectView.class);
    RedirectView redirectView = (RedirectView) mv.getView();
    MockHttpServletResponse response = new MockHttpServletResponse();
    redirectView.render(Collections.emptyMap(), request, response);
    assertThat(response.getRedirectedUrl()).isEqualTo("/new?a=b");
    assertThat(response.getStatus()).isEqualTo(308);
    request = new MockHttpServletRequest("GET", "/bad");
    chain = mapping2.getHandler(request);
    response = new MockHttpServletResponse();
    mv = adapter.handle(request, response, chain.getHandler());
    assertThat(mv).isNull();
    assertThat(response.getStatus()).isEqualTo(404);
}
Also used : BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) SimpleControllerHandlerAdapter(org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter) RedirectView(org.springframework.web.servlet.view.RedirectView) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 8 with ThemeChangeInterceptor

use of org.springframework.web.servlet.theme.ThemeChangeInterceptor in project spring-framework by spring-projects.

the class MvcNamespaceTests method testViewControllersOnWebSphere.

/**
 * WebSphere gives trailing servlet path slashes by default!!
 */
@Test
public void testViewControllersOnWebSphere() throws Exception {
    loadBeanDefinitions("mvc-config-view-controllers.xml");
    SimpleUrlHandlerMapping mapping2 = appContext.getBean(SimpleUrlHandlerMapping.class);
    SimpleControllerHandlerAdapter adapter = appContext.getBean(SimpleControllerHandlerAdapter.class);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/myapp/app/bar");
    request.setContextPath("/myapp");
    request.setServletPath("/app/");
    request.setAttribute("com.ibm.websphere.servlet.uri_non_decoded", "/myapp/app/bar");
    HandlerExecutionChain chain = mapping2.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(4);
    assertThat(chain.getInterceptorList().get(1) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(3) instanceof ThemeChangeInterceptor).isTrue();
    ModelAndView mv2 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertThat(mv2.getViewName()).isEqualTo("baz");
    request.setRequestURI("/myapp/app/");
    request.setContextPath("/myapp");
    request.setServletPath("/app/");
    chain = mapping2.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(4);
    assertThat(chain.getInterceptorList().get(1) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(3) instanceof ThemeChangeInterceptor).isTrue();
    ModelAndView mv3 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertThat(mv3.getViewName()).isEqualTo("root");
    request.setRequestURI("/myapp/");
    request.setContextPath("/myapp");
    request.setServletPath("/");
    chain = mapping2.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(4);
    assertThat(chain.getInterceptorList().get(1) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(3) instanceof ThemeChangeInterceptor).isTrue();
    mv3 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertThat(mv3.getViewName()).isEqualTo("root");
}
Also used : LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) SimpleControllerHandlerAdapter(org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 9 with ThemeChangeInterceptor

use of org.springframework.web.servlet.theme.ThemeChangeInterceptor in project spring-framework by spring-projects.

the class MvcNamespaceTests method testBeanDecoration.

@Test
public void testBeanDecoration() throws Exception {
    loadBeanDefinitions("mvc-config-bean-decoration.xml");
    RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
    assertThat(mapping).isNotNull();
    mapping.setDefaultHandler(handlerMethod);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
    HandlerExecutionChain chain = mapping.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(3);
    assertThat(chain.getInterceptorList().get(0) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(1) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof ThemeChangeInterceptor).isTrue();
    LocaleChangeInterceptor interceptor = (LocaleChangeInterceptor) chain.getInterceptorList().get(1);
    assertThat(interceptor.getParamName()).isEqualTo("lang");
    ThemeChangeInterceptor interceptor2 = (ThemeChangeInterceptor) chain.getInterceptorList().get(2);
    assertThat(interceptor2.getParamName()).isEqualTo("style");
}
Also used : LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) Test(org.junit.jupiter.api.Test)

Aggregations

ThemeChangeInterceptor (org.springframework.web.servlet.theme.ThemeChangeInterceptor)9 LocaleChangeInterceptor (org.springframework.web.servlet.i18n.LocaleChangeInterceptor)6 Test (org.junit.jupiter.api.Test)4 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)4 ConversionServiceExposingInterceptor (org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor)4 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)4 Bean (org.springframework.context.annotation.Bean)3 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)3 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 RefreshScope (org.springframework.cloud.context.config.annotation.RefreshScope)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)2 UserRoleAuthorizationInterceptor (org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor)2 SimpleControllerHandlerAdapter (org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter)2 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)2 ArrayList (java.util.ArrayList)1 lombok.val (lombok.val)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)1 ServletListenerRegistrationBean (org.springframework.boot.web.servlet.ServletListenerRegistrationBean)1