use of jakarta.servlet.jsp.jstl.fmt.LocalizationContext in project spring-framework by spring-projects.
the class ViewResolverTests method internalResourceViewResolverWithJstlAndContextParam.
@Test
public void internalResourceViewResolverWithJstlAndContextParam() throws Exception {
Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;
this.sc.addInitParameter(Config.FMT_LOCALIZATION_CONTEXT, "org/springframework/web/context/WEB-INF/context-messages");
this.wac.addMessage("code1", locale, "messageX");
this.wac.refresh();
InternalResourceViewResolver vr = new InternalResourceViewResolver();
vr.setViewClass(JstlView.class);
vr.setApplicationContext(this.wac);
View view = vr.resolveViewName("example1", Locale.getDefault());
assertThat(view).isInstanceOf(JstlView.class);
assertThat(((JstlView) view).getUrl()).as("Correct URL").isEqualTo("example1");
view = vr.resolveViewName("example2", Locale.getDefault());
assertThat(view).isInstanceOf(JstlView.class);
assertThat(((JstlView) view).getUrl()).as("Correct URL").isEqualTo("example2");
this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
this.request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
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();
assertThat(this.request.getAttribute("rc") == null).as("Correct rc attribute").isTrue();
assertThat(Config.get(this.request, Config.FMT_LOCALE)).isEqualTo(locale);
LocalizationContext lc = (LocalizationContext) Config.get(this.request, Config.FMT_LOCALIZATION_CONTEXT);
assertThat(lc.getResourceBundle().getString("code1")).isEqualTo("message1");
assertThat(lc.getResourceBundle().getString("code2")).isEqualTo("message2");
}
use of jakarta.servlet.jsp.jstl.fmt.LocalizationContext in project spring-framework by spring-projects.
the class JstlUtils method exposeLocalizationContext.
/**
* Exposes JSTL-specific request attributes specifying locale
* and resource bundle for JSTL's formatting and message tags,
* using Spring's locale and MessageSource.
* @param requestContext the context for the current HTTP request,
* including the ApplicationContext to expose as MessageSource
*/
public static void exposeLocalizationContext(RequestContext requestContext) {
Config.set(requestContext.getRequest(), Config.FMT_LOCALE, requestContext.getLocale());
TimeZone timeZone = requestContext.getTimeZone();
if (timeZone != null) {
Config.set(requestContext.getRequest(), Config.FMT_TIME_ZONE, timeZone);
}
MessageSource messageSource = getJstlAwareMessageSource(requestContext.getServletContext(), requestContext.getMessageSource());
LocalizationContext jstlContext = new SpringLocalizationContext(messageSource, requestContext.getRequest());
Config.set(requestContext.getRequest(), Config.FMT_LOCALIZATION_CONTEXT, jstlContext);
}
use of jakarta.servlet.jsp.jstl.fmt.LocalizationContext in project spring-framework by spring-projects.
the class JstlUtils method exposeLocalizationContext.
/**
* Exposes JSTL-specific request attributes specifying locale
* and resource bundle for JSTL's formatting and message tags,
* using Spring's locale and MessageSource.
* @param request the current HTTP request
* @param messageSource the MessageSource to expose,
* typically the current ApplicationContext (may be {@code null})
* @see #exposeLocalizationContext(RequestContext)
*/
public static void exposeLocalizationContext(HttpServletRequest request, @Nullable MessageSource messageSource) {
Locale jstlLocale = RequestContextUtils.getLocale(request);
Config.set(request, Config.FMT_LOCALE, jstlLocale);
TimeZone timeZone = RequestContextUtils.getTimeZone(request);
if (timeZone != null) {
Config.set(request, Config.FMT_TIME_ZONE, timeZone);
}
if (messageSource != null) {
LocalizationContext jstlContext = new SpringLocalizationContext(messageSource, request);
Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, jstlContext);
}
}
use of jakarta.servlet.jsp.jstl.fmt.LocalizationContext in project spring-framework by spring-projects.
the class ViewResolverTests method internalResourceViewResolverWithJstl.
@Test
public void internalResourceViewResolverWithJstl() throws Exception {
Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;
this.wac.addMessage("code1", locale, "messageX");
this.wac.refresh();
InternalResourceViewResolver vr = new InternalResourceViewResolver();
vr.setViewClass(JstlView.class);
vr.setApplicationContext(this.wac);
View view = vr.resolveViewName("example1", Locale.getDefault());
assertThat(view).isInstanceOf(JstlView.class);
assertThat(((JstlView) view).getUrl()).as("Correct URL").isEqualTo("example1");
view = vr.resolveViewName("example2", Locale.getDefault());
assertThat(view).isInstanceOf(JstlView.class);
assertThat(((JstlView) view).getUrl()).as("Correct URL").isEqualTo("example2");
this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
this.request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
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();
assertThat(this.request.getAttribute("rc") == null).as("Correct rc attribute").isTrue();
assertThat(Config.get(this.request, Config.FMT_LOCALE)).isEqualTo(locale);
LocalizationContext lc = (LocalizationContext) Config.get(this.request, Config.FMT_LOCALIZATION_CONTEXT);
assertThat(lc.getResourceBundle().getString("code1")).isEqualTo("messageX");
}
Aggregations