加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

[View]2 详解JSP和JSTL视图(InternalResourceView,JstlView & ResourceBundleViewResolver,InternalRes

(2012-05-29 18:01:30)
标签:

it

spring

mvc

jsp

jstl

resource

bundle

internal

名称解释

1JSP

JSPJava Server Pages)是由Sun Microsystems公司倡导、许多公司参与一起建立的一种动态网页技术标准。JSP技术有点类似ASP技术,它是在传统的网页HTML文件(*.htm,*.html)中插入Java程序段(Scriptlet)JSP标记(tag),从而形成JSP文件(*.jsp) JSP开发的Web应用是跨平台的,既能在Linux下运行,也能在其他操作系统上运行。

2Servlet

Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面。 它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器响应(HTTP服务器上的数据库或应用程序)的中间层。 Servlet是位于Web 服务器内部的服务器端的Java应用程序,与传统的从命令行启动的Java应用程序不同,ServletWeb服务器进行加载,该Web服务器必须包含支持ServletJava虚拟机。

3JSTL

JSTLJSP Standard Tag Library JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apachejakarta小组来维护的。JSTL只能运行在支持JSP1.2Servlet2.3规范的容器上,如tomcat 4.x。在JSP 2.0中也是作为标准支持的。

 

AViewResolver

A.1ResourceBundleViewResolver

org.springframework.web.servlet.view.ResourceBundleViewResolver

public class ResourceBundleViewResolver extends AbstractCachingViewResolver implements Ordered, InitializingBean, DisposableBean

java.lang.Object

  org.springframework.context.support.ApplicationObjectSupport

      org.springframework.web.context.support.WebApplicationObjectSupport

          org.springframework.web.servlet.view.AbstractCachingViewResolver

              org.springframework.web.servlet.view.ResourceBundleViewResolver

ViewResolver implementation that uses bean definitions in a ResourceBundle, specified by the bundle basename.

ViewResolver的实现,它通过bundlebasename来指定资源文件(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目录)下。默认的basenameviews

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类来支持这个本地化的视图定义。例如,若basenameviews,且localde_AT,那么类路径下的views_de_AT.propertiesviews_de.propertiesviews.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.1SET方法

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支持不同的后置。例如,若basenameviews,它可以映射viewsviews_en_auviews_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.2ResourceBundleViewResolver应用

当一个web应用中混合使用不同的视图技术时(例如,同时使用了JSPExecl等),就可以使用ResourceBundleViewResolver,让其在views.properties的配置文件通过[视图名].(class)来指定不同的视图类,并通过[视图名].url来指定视图的资源(例如JSP视图必须指定一个JSP的资源文件)。

 

A.2InternalResourceViewResolver

org.springframework.web.servlet.view.InternalResourceViewResolver

public class InternalResourceViewResolver extends UrlBasedViewResolver

java.lang.Object
  
 
org.springframework.context.support.ApplicationObjectSupport
      
 
org.springframework.web.context.support.WebApplicationObjectSupport
          
 
org.springframework.web.servlet.view.AbstractCachingViewResolver
              
 
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(如:ServletJSP)和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 InternalResourceViewResolver always needs to be last, as it will attempt to resolve any view name, no matter whether the underlying resource actually exists.

注意,在ViewResolver链中,InternalResourceViewResolver最好放在最后,因为它总是试图解析任何视图名称,不管底层资源是否真实存在。(放在InternalResourceViewResolver后面的解析器基本没有意义,因为InternalResourceViewResolver总是返回一个视图)。

A.2.1Set方法

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 setExposeContextBeansAsAttributes(boolean exposeContextBeansAsAttributes)

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 setExposedContextBeanNames(String[] exposedContextBeanNames)

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可以公开访问。

 

BView

B.1InternalResourceView

org.springframework.web.servlet.view.InternalResourceView

public class InternalResourceView extends AbstractUrlBasedView

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

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应用的一个资源,RequestDispatcherforward 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 InternalResourceViewResolver looks as follows, from the perspective of the DispatcherServlet context definition:

它常和InternalResourceViewResolver一起使用,它在DispatcherServlet上下文中的定义:

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<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"),它是InternalResourceViewResolver的默认视图。

B.2JstlView

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的一个特例,就是一个使用JSTLJSP页面。

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 ReloadableResourceBundleMessageSource instead of the standard ResourceBundleMessageSource for more sophistication. Of course, any other Spring components can share the same MessageSource.

指定的MessageSource从类路径中的messages.properties等文件中加载信息。这些信息也将自动公开给JSTLJSTL可以用fmt标签来使用这些信息。可以考虑使用SpringReloadableResourceBundleMessageSource来替代标准的ResourceBundleMessageSource以获得更复杂的功能。当然,其它的Spring组件也可以共享这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.

它是一个单独的类主要为了避免InternalResourceView本身的JSTL依赖。JSTL已经是J2EE 1.4以上版本的一部分了,所有无须把JSTLjar文件放到类路径下。

Hint: Set the InternalResourceView.setExposeContextBeansAsAttributes(boolean) 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.

提示,把InternalResourceView.setExposeContextBeansAsAttributes设置为true,可使所有应用程序上下文中的bean可以通过JSTL表达式(c:out)来访问。JSP 2.0${...}表达式达到相似的功能。

 

C、举例说明

下面的这个例子同时使用了InternalResourceViewResolverResourceBundleViewResolver两种视图解析器,以各自的方式来解析视图名,并使用InternalResourceView JSP/Servlet)和JstlViewJSTL)这两种视图技术来显示。

1dispatcher-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">

    <!-- HandlerMapping -->

<bean id="htmlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

       <property name="mappings">

           <props>

              <prop key="*.html">htmlController</prop>

           </props>

       </property>

    </bean>

    <!-- *.html Controller -->

<bean id="htmlController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController">

    </bean>

    <!-- ViewResolver -->

<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">

       <property name="order" value="0" />

<property name="defaultParentView" value="org.springframework.web.servlet.view.JstlView"/>

    </bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

       <property name="prefix" value="/WEB-INF/jsp/" />

       <property name="suffix" value=".jsp" />

       <property name="order" value="9" />

    </bean>

</beans>

2views.properties

a.(class)=org.springframework.web.servlet.view.JstlView

a.url=/WEB-INF/jsp/a.jsp

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有