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;
}
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;
}
}
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);
}
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;
}
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;
}
Aggregations