Top Description Fields Constructors Methods
org.springframework.web.servlet.view

public Class JstlView

extends InternalResourceView
Class Inheritance
Imports
jakarta.servlet.ServletContext, jakarta.servlet.http.HttpServletRequest, org.springframework.context.MessageSource, org.springframework.lang.Nullable, org.springframework.web.servlet.support.JstlUtils, .RequestContext

Specialization of 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 org.springframework.context.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 setExposeContextBeansAsAttributes flag to "true" in order to make all Spring beans in the application context accessible within JSTL expressions (e.g. in a c:out value expression). This will also make all such beans accessible in plain ${...} expressions in a JSP 2.0 page.

Author
Juergen Hoeller
Since
27.02.2003
See Also
org.springframework.web.servlet.support.JstlUtils#exposeLocalizationContext, InternalResourceViewResolver, org.springframework.context.support.ResourceBundleMessageSource, org.springframework.context.support.ReloadableResourceBundleMessageSource

Field Summary

Modifier and TypeField and Description
private MessageSource

Constructor Summary

AccessConstructor and Description
public
JstlView()

Constructor for use as a bean.

public
JstlView(String
the URL to forward to
url
)

Create a new JstlView with the given URL.

public
JstlView(String
the URL to forward to
url
,
MessageSource
the MessageSource to expose to JSTL tags (will be wrapped with a JSTL-aware MessageSource that is aware of JSTL's jakarta.servlet.jsp.jstl.fmt.localizationContext context-param)
messageSource
)

Create a new JstlView with the given URL.

Method Summary

Modifier and TypeMethod and Description
protected void
exposeHelpers(HttpServletRequest
current HTTP request
request
)

Overrides org.springframework.web.servlet.view.InternalResourceView.exposeHelpers.

Exposes a JSTL LocalizationContext for Spring's locale and MessageSource.

protected void
initServletContext(ServletContext
the ServletContext that this application object runs in (never null)
servletContext
)

Overrides org.springframework.web.context.support.WebApplicationObjectSupport.initServletContext.

Wraps the MessageSource with a JSTL-aware MessageSource that is aware of JSTL's jakarta.servlet.jsp.jstl.fmt.localizationContext context-param.

Inherited from org.springframework.web.servlet.view.InternalResourceView:
getRequestDispatcherisContextRequiredprepareForRenderingrenderMergedOutputModelsetAlwaysIncludesetPreventDispatchLoopuseInclude

Field Detail

messageSourceback to summary
private MessageSource messageSource
Annotations
@Nullable

Constructor Detail

JstlViewback to summary
public JstlView()

Constructor for use as a bean.

See Also
setUrl
JstlViewback to summary
public JstlView(String url)

Create a new JstlView with the given URL.

Parameters
url:String

the URL to forward to

JstlViewback to summary
public JstlView(String url, MessageSource messageSource)

Create a new JstlView with the given URL.

Parameters
url:String

the URL to forward to

messageSource:MessageSource

the MessageSource to expose to JSTL tags (will be wrapped with a JSTL-aware MessageSource that is aware of JSTL's jakarta.servlet.jsp.jstl.fmt.localizationContext context-param)

See Also
JstlUtils#getJstlAwareMessageSource

Method Detail

exposeHelpersback to summary
protected void exposeHelpers(HttpServletRequest request) throws Exception

Overrides org.springframework.web.servlet.view.InternalResourceView.exposeHelpers.

Exposes a JSTL LocalizationContext for Spring's locale and MessageSource.

Parameters
request:HttpServletRequest

Doc from org.springframework.web.servlet.view.InternalResourceView.exposeHelpers.

current HTTP request

Annotations
@Override
Exceptions
Exception:

Doc from org.springframework.web.servlet.view.InternalResourceView.exposeHelpers.

if there's a fatal error while we're adding attributes

See Also
JstlUtils#exposeLocalizationContext
initServletContextback to summary
protected void initServletContext(ServletContext servletContext)

Overrides org.springframework.web.context.support.WebApplicationObjectSupport.initServletContext.

Wraps the MessageSource with a JSTL-aware MessageSource that is aware of JSTL's jakarta.servlet.jsp.jstl.fmt.localizationContext context-param.

Parameters
servletContext:ServletContext

Doc from org.springframework.web.context.support.WebApplicationObjectSupport.initServletContext.

the ServletContext that this application object runs in (never null)

Annotations
@Override
See Also
JstlUtils#getJstlAwareMessageSource