Search in sources :

Example 1 with ThemeResolver

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

the class RequestContext method changeTheme.

/**
	 * Change the current theme to the specified one,
	 * storing the new theme name through the configured {@link ThemeResolver}.
	 * @param theme the new theme
	 * @see ThemeResolver#setThemeName
	 */
public void changeTheme(Theme theme) {
    ThemeResolver themeResolver = RequestContextUtils.getThemeResolver(this.request);
    if (themeResolver == null) {
        throw new IllegalStateException("Cannot change theme if no ThemeResolver configured");
    }
    themeResolver.setThemeName(this.request, this.response, (theme != null ? theme.getName() : null));
    this.theme = theme;
}
Also used : ThemeResolver(org.springframework.web.servlet.ThemeResolver)

Example 2 with ThemeResolver

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

the class RequestContextUtils method getTheme.

/**
	 * Retrieves the current theme from the given request, using the ThemeResolver
	 * and ThemeSource bound to the request by the DispatcherServlet.
	 * @param request current HTTP request
	 * @return the current theme, or {@code null} if not found
	 * @see #getThemeResolver
	 */
public static Theme getTheme(HttpServletRequest request) {
    ThemeResolver themeResolver = getThemeResolver(request);
    ThemeSource themeSource = getThemeSource(request);
    if (themeResolver != null && themeSource != null) {
        String themeName = themeResolver.resolveThemeName(request);
        return themeSource.getTheme(themeName);
    } else {
        return null;
    }
}
Also used : ThemeResolver(org.springframework.web.servlet.ThemeResolver) ThemeSource(org.springframework.ui.context.ThemeSource)

Example 3 with ThemeResolver

use of org.springframework.web.servlet.ThemeResolver 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);
}
Also used : LocaleResolver(org.springframework.web.servlet.LocaleResolver) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) ThemeResolver(org.springframework.web.servlet.ThemeResolver) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) MockPageContext(org.springframework.mock.web.test.MockPageContext) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse)

Example 4 with ThemeResolver

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

the class ThemeChangeInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
    String newTheme = request.getParameter(this.paramName);
    if (newTheme != null) {
        ThemeResolver themeResolver = RequestContextUtils.getThemeResolver(request);
        if (themeResolver == null) {
            throw new IllegalStateException("No ThemeResolver found: not in a DispatcherServlet request?");
        }
        themeResolver.setThemeName(request, response, newTheme);
    }
    // Proceed in any case.
    return true;
}
Also used : ThemeResolver(org.springframework.web.servlet.ThemeResolver)

Example 5 with ThemeResolver

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

the class RequestContext method changeTheme.

/**
	 * Change the current theme to the specified theme by name,
	 * storing the new theme name through the configured {@link ThemeResolver}.
	 * @param themeName the name of the new theme
	 * @see ThemeResolver#setThemeName
	 */
public void changeTheme(String themeName) {
    ThemeResolver themeResolver = RequestContextUtils.getThemeResolver(this.request);
    if (themeResolver == null) {
        throw new IllegalStateException("Cannot change theme if no ThemeResolver configured");
    }
    themeResolver.setThemeName(this.request, this.response, themeName);
    // Ask for re-resolution on next getTheme call.
    this.theme = null;
}
Also used : ThemeResolver(org.springframework.web.servlet.ThemeResolver)

Aggregations

ThemeResolver (org.springframework.web.servlet.ThemeResolver)5 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)1 MockPageContext (org.springframework.mock.web.test.MockPageContext)1 MockServletContext (org.springframework.mock.web.test.MockServletContext)1 ThemeSource (org.springframework.ui.context.ThemeSource)1 LocaleResolver (org.springframework.web.servlet.LocaleResolver)1 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)1 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)1 FixedThemeResolver (org.springframework.web.servlet.theme.FixedThemeResolver)1