Search in sources :

Example 1 with JstlView

use of cn.taketoday.web.servlet.view.JstlView in project today-infrastructure by TAKETODAY.

the class ViewResolverTests method beanNameViewResolver.

@Test
public void beanNameViewResolver() {
    PropertyValues pvs1 = new PropertyValues();
    pvs1.add(new PropertyValue("url", "/example1.jsp"));
    this.wac.registerSingleton("example1", InternalResourceView.class, pvs1);
    PropertyValues pvs2 = new PropertyValues();
    pvs2.add(new PropertyValue("url", "/example2.jsp"));
    this.wac.registerSingleton("example2", JstlView.class, pvs2);
    BeanNameViewResolver vr = new BeanNameViewResolver();
    vr.setApplicationContext(this.wac);
    this.wac.refresh();
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(InternalResourceView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("/example1.jsp");
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((JstlView) view).getUrl()).as("Correct URL").isEqualTo("/example2.jsp");
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) PropertyValue(cn.taketoday.beans.PropertyValue) JstlView(cn.taketoday.web.servlet.view.JstlView) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) Test(org.junit.jupiter.api.Test)

Example 2 with JstlView

use of cn.taketoday.web.servlet.view.JstlView in project today-framework by TAKETODAY.

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, "cn/taketoday/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(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");
    wac.registerSingleton(LocaleResolver.BEAN_NAME, new FixedLocaleResolver(locale));
    Map<String, Object> model = new HashMap<>();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    view.render(model, this.requestContext);
    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");
}
Also used : Locale(java.util.Locale) LocalizationContext(jakarta.servlet.jsp.jstl.fmt.LocalizationContext) HashMap(java.util.HashMap) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) FixedLocaleResolver(cn.taketoday.web.i18n.FixedLocaleResolver) JstlView(cn.taketoday.web.servlet.view.JstlView) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) InternalResourceViewResolver(cn.taketoday.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Example 3 with JstlView

use of cn.taketoday.web.servlet.view.JstlView in project today-framework by TAKETODAY.

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.wac.registerSingleton(LocaleResolver.BEAN_NAME, new FixedLocaleResolver(locale));
    Map<String, Object> model = new HashMap<>();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    view.render(model, this.requestContext);
    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");
}
Also used : Locale(java.util.Locale) LocalizationContext(jakarta.servlet.jsp.jstl.fmt.LocalizationContext) HashMap(java.util.HashMap) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) FixedLocaleResolver(cn.taketoday.web.i18n.FixedLocaleResolver) JstlView(cn.taketoday.web.servlet.view.JstlView) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) InternalResourceViewResolver(cn.taketoday.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Example 4 with JstlView

use of cn.taketoday.web.servlet.view.JstlView in project today-framework by TAKETODAY.

the class ViewResolverTests method beanNameViewResolver.

@Test
public void beanNameViewResolver() {
    PropertyValues pvs1 = new PropertyValues();
    pvs1.add(new PropertyValue("url", "/example1.jsp"));
    this.wac.registerSingleton("example1", InternalResourceView.class, pvs1);
    PropertyValues pvs2 = new PropertyValues();
    pvs2.add(new PropertyValue("url", "/example2.jsp"));
    this.wac.registerSingleton("example2", JstlView.class, pvs2);
    BeanNameViewResolver vr = new BeanNameViewResolver();
    vr.setApplicationContext(this.wac);
    this.wac.refresh();
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(InternalResourceView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("/example1.jsp");
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((JstlView) view).getUrl()).as("Correct URL").isEqualTo("/example2.jsp");
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) PropertyValue(cn.taketoday.beans.PropertyValue) JstlView(cn.taketoday.web.servlet.view.JstlView) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) Test(org.junit.jupiter.api.Test)

Example 5 with JstlView

use of cn.taketoday.web.servlet.view.JstlView in project today-infrastructure by TAKETODAY.

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, "cn/taketoday/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(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");
    wac.registerSingleton(LocaleResolver.BEAN_NAME, new FixedLocaleResolver(locale));
    Map<String, Object> model = new HashMap<>();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    view.render(model, this.requestContext);
    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");
}
Also used : Locale(java.util.Locale) LocalizationContext(jakarta.servlet.jsp.jstl.fmt.LocalizationContext) HashMap(java.util.HashMap) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) FixedLocaleResolver(cn.taketoday.web.i18n.FixedLocaleResolver) JstlView(cn.taketoday.web.servlet.view.JstlView) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) InternalResourceViewResolver(cn.taketoday.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Aggregations

InternalResourceView (cn.taketoday.web.servlet.view.InternalResourceView)6 JstlView (cn.taketoday.web.servlet.view.JstlView)6 Test (org.junit.jupiter.api.Test)6 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)4 FixedLocaleResolver (cn.taketoday.web.i18n.FixedLocaleResolver)4 InternalResourceViewResolver (cn.taketoday.web.servlet.view.InternalResourceViewResolver)4 LocalizationContext (jakarta.servlet.jsp.jstl.fmt.LocalizationContext)4 HashMap (java.util.HashMap)4 Locale (java.util.Locale)4 PropertyValue (cn.taketoday.beans.PropertyValue)2 PropertyValues (cn.taketoday.beans.PropertyValues)2