Class JstlView
- All Implemented Interfaces:
Aware, BeanNameAware, InitializingBean, ApplicationContextAware, ServletContextAware, View
InternalResourceView for JSTL pages,
i.e. JSP pages that use the JSP Standard Tag Library.
Exposes JSTL-specific request attributes specifying locale
and resource bundle for JSTL's formatting and message tags,
using Spring's locale and MessageSource.
Typical usage with InternalResourceViewResolver would look as follows,
from the perspective of the DispatcherServlet context definition:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="messages"/> </bean>Every view name returned from a handler will be translated to a JSP resource (for example: "myView" → "/WEB-INF/jsp/myView.jsp"), using this view class to enable explicit JSTL support.
The specified MessageSource loads messages from "messages.properties" etc. files in the class path. This will automatically be exposed to views as JSTL localization context, which the JSTL fmt tags (message etc.) will use. Consider using Spring's ReloadableResourceBundleMessageSource instead of the standard ResourceBundleMessageSource for more sophistication. Of course, any other Spring components can share the same MessageSource.
This is a separate class mainly to avoid JSTL dependencies in
InternalResourceView itself. JSTL has not been part of standard
J2EE up until J2EE 1.4, so we can't assume the JSTL API jar to be
available on the class path.
Hint: Set the AbstractView.setExposeContextBeansAsAttributes(boolean) flag to "true"
in order to make all Spring beans in the application context accessible
within JSTL expressions (for example, in a c:out value expression).
This will also make all such beans accessible in plain ${...}
expressions in a JSP 2.0 page.
- Since:
- 27.02.2003
- Author:
- Juergen Hoeller
- See Also:
-
Field Summary
Fields inherited from class AbstractView
DEFAULT_CONTENT_TYPEFields inherited from class ApplicationObjectSupport
loggerFields inherited from interface View
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidexposeHelpers(jakarta.servlet.http.HttpServletRequest request) Exposes a JSTL LocalizationContext for Spring's locale and MessageSource.protected voidinitServletContext(jakarta.servlet.ServletContext servletContext) Wraps the MessageSource with a JSTL-aware MessageSource that is aware of JSTL'sjakarta.servlet.jsp.jstl.fmt.localizationContextcontext-param.Methods inherited from class InternalResourceView
getRequestDispatcher, isContextRequired, prepareForRendering, renderMergedOutputModel, setAlwaysInclude, setPreventDispatchLoop, useIncludeMethods inherited from class AbstractUrlBasedView
afterPropertiesSet, checkResource, getUrl, isUrlRequired, setUrl, toStringMethods inherited from class AbstractView
addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, formatViewName, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getRequestToExpose, getStaticAttributes, isExposePathVariables, prepareResponse, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setRequestContextAttribute, setResponseContentType, writeToResponseMethods inherited from class WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, setServletContextMethods inherited from class ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext
-
Constructor Details
-
JstlView
public JstlView()Constructor for use as a bean.- See Also:
-
JstlView
Create a new JstlView with the given URL.- Parameters:
url- the URL to forward to
-
JstlView
Create a new JstlView with the given URL.- Parameters:
url- the URL to forward tomessageSource- the MessageSource to expose to JSTL tags (will be wrapped with a JSTL-aware MessageSource that is aware of JSTL'sjakarta.servlet.jsp.jstl.fmt.localizationContextcontext-param)- See Also:
-
-
Method Details
-
initServletContext
protected void initServletContext(jakarta.servlet.ServletContext servletContext) Wraps the MessageSource with a JSTL-aware MessageSource that is aware of JSTL'sjakarta.servlet.jsp.jstl.fmt.localizationContextcontext-param.- Overrides:
initServletContextin classWebApplicationObjectSupport- Parameters:
servletContext- the ServletContext that this application object runs in (nevernull)- See Also:
-
exposeHelpers
Exposes a JSTL LocalizationContext for Spring's locale and MessageSource.- Overrides:
exposeHelpersin classInternalResourceView- Parameters:
request- current HTTP request- Throws:
Exception- if there's a fatal error while we're adding attributes- See Also:
-