[View]2 详解JSP和JSTL视图(InternalResourceView,JstlView & ResourceBundleViewResolver,InternalRes
(2012-05-29 18:01:30)
标签:
itspringmvcjspjstlresourcebundleinternal |
名称解释
1、JSP
JSP(Java Server Pages)是由Sun Microsystems公司倡导、许多公司参与一起建立的一种动态网页技术标准。JSP技术有点类似ASP技术,它是在传统的网页HTML文件(*.htm,*.html)中插入Java程序段(Scriptlet)和JSP标记(tag),从而形成JSP文件(*.jsp)。 用JSP开发的Web应用是跨平台的,既能在Linux下运行,也能在其他操作系统上运行。
2、Servlet
Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面。 它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器响应(HTTP服务器上的数据库或应用程序)的中间层。 Servlet是位于Web 服务器内部的服务器端的Java应用程序,与传统的从命令行启动的Java应用程序不同,Servlet由Web服务器进行加载,该Web服务器必须包含支持Servlet的Java虚拟机。
3、JSTL
JSTL(JSP Standard Tag Library ,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的。JSTL只能运行在支持JSP1.2和Servlet2.3规范的容器上,如tomcat 4.x。在JSP 2.0中也是作为标准支持的。
A、ViewResolver
A.1、ResourceBundleViewResolv
org.springframework.web.servlet.view.ResourceBundleViewResolv
public class ResourceBundleViewResolv
ViewResolver implementation that uses bean definitions in a ResourceBundle, specified by the bundle basename.
ViewResolver的实现,它通过bundle的basename来指定资源文件(ResourceBundle),在资源文件ResourceBundle中寻找bean的定义(这个bean定义了具体视图的实现)。
The bundle is typically defined in a properties file, located in the class path. The default bundle basename is "views".
通常在properties类型的文件中定义这个bundle,这个properties文件存放在类路径(执行项目的WEB-INF/classes目录)下。默认的basename是views。
This ViewResolver supports localized view definitions, using the default support of PropertyResourceBundle. For example, the basename "views" will be resolved as class path resources "views_de_AT.properties", "views_de.properties", "views.properties" - for a given Locale "de_AT".
这个视图解析器支持本地化的视图定义,默认使用PropertyResourceBundle类来支持这个本地化的视图定义。例如,若basename为views,且local为de_AT,那么类路径下的views_de_AT.properties,views_de.properties,views.properties的资源文件将依次被解析。
Note: this ViewResolver implements the Ordered interface to allow for flexible participation in ViewResolver chaining. For example, some special views could be defined via this ViewResolver (giving it 0 as "order" value), while all remaining views could be resolved by a UrlBasedViewResolver.
注意,这个ViewResolver还实现了Ordered接口,这允许它灵活的参与视图解析链。例如,一些特殊的视图可以通过这个ViewResolver来定义(order设置为0),而其它的视图可以通过UrlBasedViewResolver来解析。
A.1.1、SET方法
void setBasename(String basename)
Set a single basename, following ResourceBundle conventions. The default is "views".
ResourceBundle supports different suffixes. For example, a base name of "views" might map to ResourceBundle files "views", "views_en_au" and "views_de".
设置单个basename,它遵循ResourceBundle规则。默认为views。
ResourceBundle支持不同的后置。例如,若basename为views,它可以映射views,views_en_au,views_de等资源文件。
Note that ResourceBundle names are effectively classpath locations: As a consequence, the JDK's standard ResourceBundle treats dots as package separators. This means that "test.theme" is effectively equivalent to "test/theme", just like it is for programmatic java.util.ResourceBundle usage.
注意,这个ResourceBundle的名称是一个有效的classpath位置:因此JDK标准资源文件是由”.”来分割的(如:com.xxx.view. properties)。也就是说"test.theme"表示"test/theme",这个规则和java.util.ResourceBundle定义的规则相似。
void setBasenames(String[] basenames)
The associated resource bundles will be checked sequentially when resolving a message code. Note that message definitions in a previous resource bundle will override ones in a later bundle, due to the sequential lookup.
当解析一个信息时相关的资源文件将按设定的顺序挨个检查。注意,由于按顺序检查,在前面定义的资源文件将优先于后面定义的资源文件。
void setDefaultParentView(String defaultParentView)
Set the default parent for views defined in the ResourceBundle.
This avoids repeated "yyy1.(parent)=xxx", "yyy2.(parent)=xxx" definitions in the bundle, especially if all defined views share the same parent.
为资源文件中的视图定义默认的父视图。
这就避免在资源文件中重复定义"yyy1.(parent)=xxx", "yyy2.(parent)=xxx",特别是若所有定义的视图有一个相同的父类。
The parent will typically define the view class and common attributes. Concrete views might simply consist of an URL definition then: a la "yyy1.url=/my.jsp", "yyy2.url=/your.jsp".
这个父类通常定义为一个视图类和公共属性。这样具体的视图可能只需要定于URL即可,如:"yyy1.url=/my.jsp", "yyy2.url=/your.jsp"。
View definitions that define their own parent or carry their own class can still override this. Strictly speaking, the rule that a default parent setting does not apply to a bean definition that carries a class is there for backwards compatiblity reasons. It still matches the typical use case.
在资源文件中视图也可以定义它们自己的父类来覆盖它。严格上来说,默认的父类设置并不作用于所有的bean。
A.1.2、ResourceBundleViewResolv
当一个web应用中混合使用不同的视图技术时(例如,同时使用了JSP和Execl等),就可以使用ResourceBundleViewResolv
A.2、InternalResourceViewReso
org.springframework.web.servlet.view.InternalResourceViewReso
public class InternalResourceViewReso
java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupp ort
org.springframework.web.servlet.view.AbstractCachingViewResol ver
org.springframework.web.servlet.view.UrlBasedViewResolver
org.springframework.web.servlet.view.InternalResourceViewResolver
Convenient subclass of UrlBasedViewResolver that supports InternalResourceView (i.e. Servlets and JSPs) and subclasses such as JstlView and TilesView.
UrlBasedViewResolver的子类,它支持InternalResourceView(如:Servlet和JSP)和InternalResourceView的子类(JstlView 和TilesView)。
The view class for all views generated by this resolver can be specified via UrlBasedViewResolver.setViewClass(java.lang.Class). See UrlBasedViewResolver's javadoc for details. The default is InternalResourceView, or JstlView if the JSTL API is present.
通过这个解析器可以通过UrlBasedViewResolver.setViewClass方法指定所有视图的视图类。这个视图类默认是InternalResourceView,若支持JSTL可以是JstlView。
BTW, it's good practice to put JSP files that just serve as views under WEB-INF, to hide them from direct access (e.g. via a manually entered URL). Only controllers will be able to access them then.
顺便说一句,JSP文件若是作为一个视图来使用的话,它最好放在WEB-INF目录下,防止它被直接访问。只用控制器才可以访问它们。
Note: When chaining ViewResolvers, an
InternalResourceViewReso
注意,在ViewResolver链中,InternalResourceViewReso
A.2.1、Set方法
void setAlwaysInclude(boolean alwaysInclude)
Specify whether to always include the view rather than forward to it.
Default is "false". Switch this flag on to enforce the use of a Servlet include, even if a forward would be possible.
是否始终包含这个视图,而不能forward。
默认是false。若为true,表示强制使用一个Servlet,不允许forward。
void setExposeContextBeansAsA
Set whether to make all Spring beans in the application context accessible as request attributes, through lazy checking once an attribute gets accessed.
This will make all such beans accessible in plain ${...} expressions in a JSP 2.0 page, as well as in JSTL's c:out value expressions.
Default is "false".
设置是否把所有在上下文中定义的bean作为request属性可公开访问。
这样在JSP 2.0中可使用${}来存取,JSTL中使用c:out。
默认为false。
void setExposedContextBeanNam
Specify the names of beans in the context which are supposed to be exposed. If this is non-null, only the specified beans are eligible for exposure as attributes.
指定在上下文中运行被公开的bean的名称。若这个值非空,那只有指定的bean可以公开访问。
B、View
B.1、InternalResourceView
org.springframework.web.servlet.view.InternalResourceView
public class InternalResourceView extends AbstractUrlBasedView
java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupp ort
org.springframework.web.servlet.view.AbstractView
org.springframework.web.servlet.view.AbstractUrlBasedView
org.springframework.web.servlet.view.InternalResourceView
Wrapper for a JSP or other resource within the same web application. Exposes model objects as request attributes and forwards the request to the specified resource URL using a RequestDispatcher.
封装了在同一web应用中的JSP或其它资源。它把模型对象作为一个请求属性对外公开,通过RequestDispatcher把请求forward到一个指定的URL资源。
A URL for this view is supposed to specify a resource within the web application, suitable for RequestDispatcher's forward orinclude method.
视图的一个URL应该指定web应用的一个资源,RequestDispatcher的forward 和include方法。
If operating within an already included request or within a response that has already been committed, this view will fall back to an include instead of a forward. This can be enforced by calling response.flushBuffer() (which will commit the response) before rendering the view.
若有一个有include请求或响应的操作完成,那么这个视图将退回到一个include而不是forward。也可以在显示视图前强制调用response.flushBuffer()。
Typical usage with
InternalResourceViewReso
它常和InternalResourceViewReso
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewReso
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</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 by default.
所有返回的视图名称都会被转化为一个JSP资源(例如,视图名"myView"被转换为一个JSP资源"/WEB-INF/jsp/myView.jsp"),它是InternalResourceViewReso
B.2、JstlView
org.springframework.web.servlet.view.JstlView
public class JstlView extends InternalResourceView
java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.view.AbstractView
org.springframework.web.servlet.view.AbstractUrlBasedView
org.springframework.web.servlet.view.InternalResourceView
org.springframework.web.servlet.view.JstlView
Specialization of InternalResourceView for JSTL pages, i.e. JSP pages that use the JSP Standard Tag Library.
JSTL页面是InternalResourceView的一个特例,就是一个使用JSTL的JSP页面。
Exposes JSTL-specific request attributes specifying locale and resource bundle for JSTL's formatting and message tags, using Spring's locale and MessageSource.
通过JSTL指定的标签可以在请求中使用Spring的本地化(locale)和消息源(MessageSource)。
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.
JTSL定位资源文件的方式和InternalResourceView一样。
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 ReloadableResourceBundle
指定的MessageSource从类路径中的messages.properties等文件中加载信息。这些信息也将自动公开给JSTL,JSTL可以用fmt标签来使用这些信息。可以考虑使用Spring的ReloadableResourceBundle
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.
它是一个单独的类主要为了避免InternalResourceView本身的JSTL依赖。JSTL已经是J2EE 1.4以上版本的一部分了,所有无须把JSTL的jar文件放到类路径下。
Hint: Set the
InternalResourceView.setExposeContextBeansAsA
提示,把InternalResourceView.setExposeContextBeansAsA
C、举例说明
下面的这个例子同时使用了InternalResourceViewReso
1、dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=http://www.springframework.org/schema/beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p=http://www.springframework.org/schema/p xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="htmlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<bean id="htmlController" class="org.springframework.web.servlet.mvc.UrlFilenameViewControlle
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolv
<property name="defaultParentView" value="org.springframework.web.servlet.view.JstlView"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewReso
</beans>
2、views.properties
a.(class)=org.springframework.web.servlet.view.JstlView
a.url=/WEB-INF/jsp/a.jsp

加载中…