Java Code Examples for javax.servlet.jsp.jstl.core.Config#get()

The following examples show how to use javax.servlet.jsp.jstl.core.Config#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JstlLocalization.java    From vraptor4 with Apache License 2.0 6 votes vote down vote up
/**
 * Looks up a configuration variable in the request, session and application scopes. If none is found, return by
 * {@link ServletContext#getInitParameter(String)} method.
 */
private Object findByKey(String key) {
	Object value = Config.get(request, key);
	if (value != null) {
		return value;
	}

	value = Config.get(request.getSession(createNewSession()), key);
	if (value != null) {
		return value;
	}

	value = Config.get(request.getServletContext(), key);
	if (value != null) {
		return value;
	}

	return request.getServletContext().getInitParameter(key);
}
 
Example 2
Source File: ViewResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithJstl() throws Exception {
	Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;

	MockServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.addMessage("code1", locale, "messageX");
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	vr.setViewClass(JstlView.class);
	vr.setApplicationContext(wac);

	View view = vr.resolveViewName("example1", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());

	view = vr.resolveViewName("example2", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());

	MockHttpServletRequest request = new MockHttpServletRequest(sc);
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	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, request, response);

	assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
	assertTrue("Correct rc attribute", request.getAttribute("rc") == null);

	assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
	LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
	assertEquals("messageX", lc.getResourceBundle().getString("code1"));
}
 
Example 3
Source File: RequestContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public static Locale getJstlLocale(HttpServletRequest request, ServletContext servletContext) {
	Object localeObject = Config.get(request, Config.FMT_LOCALE);
	if (localeObject == null) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			localeObject = Config.get(session, Config.FMT_LOCALE);
		}
		if (localeObject == null && servletContext != null) {
			localeObject = Config.get(servletContext, Config.FMT_LOCALE);
		}
	}
	return (localeObject instanceof Locale ? (Locale) localeObject : null);
}
 
Example 4
Source File: ViewResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithJstlAndContextParam() throws Exception {
	Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;

	MockServletContext sc = new MockServletContext();
	sc.addInitParameter(Config.FMT_LOCALIZATION_CONTEXT, "org/springframework/web/context/WEB-INF/context-messages");
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.addMessage("code1", locale, "messageX");
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	vr.setViewClass(JstlView.class);
	vr.setApplicationContext(wac);

	View view = vr.resolveViewName("example1", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());

	view = vr.resolveViewName("example2", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());

	MockHttpServletRequest request = new MockHttpServletRequest(sc);
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
	Map model = new HashMap();
	TestBean tb = new TestBean();
	model.put("tb", tb);
	view.render(model, request, response);

	assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
	assertTrue("Correct rc attribute", request.getAttribute("rc") == null);

	assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
	LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
	assertEquals("message1", lc.getResourceBundle().getString("code1"));
	assertEquals("message2", lc.getResourceBundle().getString("code2"));
}
 
Example 5
Source File: JstlUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public ResourceBundle getResourceBundle() {
	HttpSession session = this.request.getSession(false);
	if (session != null) {
		Object lcObject = Config.get(session, Config.FMT_LOCALIZATION_CONTEXT);
		if (lcObject instanceof LocalizationContext) {
			ResourceBundle lcBundle = ((LocalizationContext) lcObject).getResourceBundle();
			return new MessageSourceResourceBundle(this.messageSource, getLocale(), lcBundle);
		}
	}
	return new MessageSourceResourceBundle(this.messageSource, getLocale());
}
 
Example 6
Source File: RequestContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static TimeZone getJstlTimeZone(HttpServletRequest request, ServletContext servletContext) {
	Object timeZoneObject = Config.get(request, Config.FMT_TIME_ZONE);
	if (timeZoneObject == null) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			timeZoneObject = Config.get(session, Config.FMT_TIME_ZONE);
		}
		if (timeZoneObject == null && servletContext != null) {
			timeZoneObject = Config.get(servletContext, Config.FMT_TIME_ZONE);
		}
	}
	return (timeZoneObject instanceof TimeZone ? (TimeZone) timeZoneObject : null);
}
 
Example 7
Source File: RequestContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static Locale getJstlLocale(HttpServletRequest request, ServletContext servletContext) {
	Object localeObject = Config.get(request, Config.FMT_LOCALE);
	if (localeObject == null) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			localeObject = Config.get(session, Config.FMT_LOCALE);
		}
		if (localeObject == null && servletContext != null) {
			localeObject = Config.get(servletContext, Config.FMT_LOCALE);
		}
	}
	return (localeObject instanceof Locale ? (Locale) localeObject : null);
}
 
Example 8
Source File: JstlUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Locale getLocale() {
	HttpSession session = this.request.getSession(false);
	if (session != null) {
		Object localeObject = Config.get(session, Config.FMT_LOCALE);
		if (localeObject instanceof Locale) {
			return (Locale) localeObject;
		}
	}
	return RequestContextUtils.getLocale(this.request);
}
 
Example 9
Source File: JstlUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ResourceBundle getResourceBundle() {
	HttpSession session = this.request.getSession(false);
	if (session != null) {
		Object lcObject = Config.get(session, Config.FMT_LOCALIZATION_CONTEXT);
		if (lcObject instanceof LocalizationContext) {
			ResourceBundle lcBundle = ((LocalizationContext) lcObject).getResourceBundle();
			return new MessageSourceResourceBundle(this.messageSource, getLocale(), lcBundle);
		}
	}
	return new MessageSourceResourceBundle(this.messageSource, getLocale());
}
 
Example 10
Source File: ViewResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithJstlAndContextParam() throws Exception {
	Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;

	MockServletContext sc = new MockServletContext();
	sc.addInitParameter(Config.FMT_LOCALIZATION_CONTEXT, "org/springframework/web/context/WEB-INF/context-messages");
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.addMessage("code1", locale, "messageX");
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	vr.setViewClass(JstlView.class);
	vr.setApplicationContext(wac);

	View view = vr.resolveViewName("example1", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());

	view = vr.resolveViewName("example2", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());

	MockHttpServletRequest request = new MockHttpServletRequest(sc);
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	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, request, response);

	assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
	assertTrue("Correct rc attribute", request.getAttribute("rc") == null);

	assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
	LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
	assertEquals("message1", lc.getResourceBundle().getString("code1"));
	assertEquals("message2", lc.getResourceBundle().getString("code2"));
}
 
Example 11
Source File: JstlUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public ResourceBundle getResourceBundle() {
	HttpSession session = this.request.getSession(false);
	if (session != null) {
		Object lcObject = Config.get(session, Config.FMT_LOCALIZATION_CONTEXT);
		if (lcObject instanceof LocalizationContext) {
			ResourceBundle lcBundle = ((LocalizationContext) lcObject).getResourceBundle();
			return new MessageSourceResourceBundle(this.messageSource, getLocale(), lcBundle);
		}
	}
	return new MessageSourceResourceBundle(this.messageSource, getLocale());
}
 
Example 12
Source File: RequestContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
public static TimeZone getJstlTimeZone(HttpServletRequest request, @Nullable ServletContext servletContext) {
	Object timeZoneObject = Config.get(request, Config.FMT_TIME_ZONE);
	if (timeZoneObject == null) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			timeZoneObject = Config.get(session, Config.FMT_TIME_ZONE);
		}
		if (timeZoneObject == null && servletContext != null) {
			timeZoneObject = Config.get(servletContext, Config.FMT_TIME_ZONE);
		}
	}
	return (timeZoneObject instanceof TimeZone ? (TimeZone) timeZoneObject : null);
}
 
Example 13
Source File: RequestContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
public static Locale getJstlLocale(HttpServletRequest request, @Nullable ServletContext servletContext) {
	Object localeObject = Config.get(request, Config.FMT_LOCALE);
	if (localeObject == null) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			localeObject = Config.get(session, Config.FMT_LOCALE);
		}
		if (localeObject == null && servletContext != null) {
			localeObject = Config.get(servletContext, Config.FMT_LOCALE);
		}
	}
	return (localeObject instanceof Locale ? (Locale) localeObject : null);
}
 
Example 14
Source File: RequestContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public static TimeZone getJstlTimeZone(HttpServletRequest request, ServletContext servletContext) {
	Object timeZoneObject = Config.get(request, Config.FMT_TIME_ZONE);
	if (timeZoneObject == null) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			timeZoneObject = Config.get(session, Config.FMT_TIME_ZONE);
		}
		if (timeZoneObject == null && servletContext != null) {
			timeZoneObject = Config.get(servletContext, Config.FMT_TIME_ZONE);
		}
	}
	return (timeZoneObject instanceof TimeZone ? (TimeZone) timeZoneObject : null);
}
 
Example 15
Source File: ViewResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithJstl() throws Exception {
	Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;

	MockServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.addMessage("code1", locale, "messageX");
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	vr.setViewClass(JstlView.class);
	vr.setApplicationContext(wac);

	View view = vr.resolveViewName("example1", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());

	view = vr.resolveViewName("example2", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());

	MockHttpServletRequest request = new MockHttpServletRequest(sc);
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
	Map model = new HashMap();
	TestBean tb = new TestBean();
	model.put("tb", tb);
	view.render(model, request, response);

	assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
	assertTrue("Correct rc attribute", request.getAttribute("rc") == null);

	assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
	LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
	assertEquals("messageX", lc.getResourceBundle().getString("code1"));
}
 
Example 16
Source File: ViewResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithJstl() throws Exception {
	Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;

	MockServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.addMessage("code1", locale, "messageX");
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	vr.setViewClass(JstlView.class);
	vr.setApplicationContext(wac);

	View view = vr.resolveViewName("example1", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());

	view = vr.resolveViewName("example2", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());

	MockHttpServletRequest request = new MockHttpServletRequest(sc);
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	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, request, response);

	assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
	assertTrue("Correct rc attribute", request.getAttribute("rc") == null);

	assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
	LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
	assertEquals("messageX", lc.getResourceBundle().getString("code1"));
}
 
Example 17
Source File: RequestContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
public static TimeZone getJstlTimeZone(HttpServletRequest request, @Nullable ServletContext servletContext) {
	Object timeZoneObject = Config.get(request, Config.FMT_TIME_ZONE);
	if (timeZoneObject == null) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			timeZoneObject = Config.get(session, Config.FMT_TIME_ZONE);
		}
		if (timeZoneObject == null && servletContext != null) {
			timeZoneObject = Config.get(servletContext, Config.FMT_TIME_ZONE);
		}
	}
	return (timeZoneObject instanceof TimeZone ? (TimeZone) timeZoneObject : null);
}
 
Example 18
Source File: RequestContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
public static Locale getJstlLocale(HttpServletRequest request, @Nullable ServletContext servletContext) {
	Object localeObject = Config.get(request, Config.FMT_LOCALE);
	if (localeObject == null) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			localeObject = Config.get(session, Config.FMT_LOCALE);
		}
		if (localeObject == null && servletContext != null) {
			localeObject = Config.get(servletContext, Config.FMT_LOCALE);
		}
	}
	return (localeObject instanceof Locale ? (Locale) localeObject : null);
}
 
Example 19
Source File: JstlUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Locale getLocale() {
	HttpSession session = this.request.getSession(false);
	if (session != null) {
		Object localeObject = Config.get(session, Config.FMT_LOCALE);
		if (localeObject instanceof Locale) {
			return (Locale) localeObject;
		}
	}
	return RequestContextUtils.getLocale(this.request);
}
 
Example 20
Source File: JstlResolver.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
public Locale resolveLocale(final HttpServletRequest request) {

		return (Locale) Config.get(request.getSession(), Config.FMT_LOCALE);
	}