分享

mediaTypes+viewResolvers+viewResolvers

 千水流水门 2013-04-28

现在AJAX技术基本上都会用到JSONSpring 3.0也对JSON的视图技术增加了支持。

 

A、名称解释及其它相关知识

A.1Internet Media Type

Internet Media Type(互联网媒体类型),也叫做于MIME类型,有时在一些协议的消息头中叫做“Content-Type”。它使用两部分标识符来确定一个类型。这些标识符起源于RFC 2046,最初用于使用SMTP协议发送Email。但是如今被扩展用于其他的协议,比如HTTPRTPSIP等。一个媒体配型至少包括两部分:一个类型,和一个子类型,还可以包括一个或多个可选的参数。比如,子类型是text类型则包括一个可选的charset参数,用来设定文本的编码。多重类型的子类型常常用来确定不同部分的边界。

  类型和子类型如果是以“x-”开头,则是非标准的(没有在IANA注册的)。特定供应商的子类型以“vnd.”开头,个人的子类型以“prs.”开头。

A.2MIME

MIMEMultipurpose Internet Mail Extensions)意思是多功能互联网邮件扩展。是一个通过互联网发送非ASCII消息的规范。许多的email客户端现在都支持MIME 所以可以通过互联网的邮件系统发送图片、声音、视频文件等等。

  有许多预先定义的MIME类型,比如GIF图片格式和PostScript文件。也可以自己定义MIME类型。

  除了email应用外,浏览器同样支持各种MIME类型。这使得浏览器可以显示或输出除了HTML格式的文件。

  MIME是在1992年,由IETF创建的。一个新的版本叫做S/MIME,用于支持加密的消息。

A.3、常见的媒体类型(Media Type

application/json

JSON格式的数据,在RFC 4627中定义

application/javascript

JavaScript,在RFC 4329中定义但是不被IE8以及之前的版本支持

text/javascript

JavaScript在已经废弃的RFC 4329中定义,现在推荐使用“application/javascript”。然而“text/javascript”允许在HTML 4 5 中使用。并且与“application/javascript”不同,它是可以跨浏览器支持的。

application/octet-stream

任意的二进制数据。通常这种类型的文件没有和任何应用程序相关联,已过去假设的软件比如Apache相反,这并不是一种未知文件的类型,而是应该在服务器端不确定内容的额类型,让客户端自行猜测类型。

application/pdf

PDF文件,在 RFC 3778 中定义

application/soap+xml

SOAP格式,在RFC 3902中定义

application/xhtml+xml

XHTML文件,在RFC 3236中定义

application/xml-dtd

DTD文件, RFC 3023中定义

application/zip

ZIP文件,一种压缩格式。

text/css

CSS文本样式表,在RFC 2318中定义

text/html

HTML格式,在RFC 2854中定义

video/mp4

MP4视频,在RFC 4337中定义

image/jpeg

JPEG JFIF格式,在RFC 2045  RFC 2046中定义

A.4JAF

  JAFJava Activation Framework

  Mail API的所有版本都需要Java Activation Framework来支持任意数据块的输入及相应处理。功能似乎不多,但目前许多浏览器和邮件工具中都能找到这种基本的MIME型支持。activation.jar文件就是JAF的框架jar包。

  JAF是一个专用的数据处理框架,它用于封装数据,并为应用程序提供访问和操作数据的接口。JAF的主要作用在于让java应用程序知道如何对一个数据源进行查看、编辑和打印等操作。

  对于通过JAF封装的数据,应用程序通过JAF提供的接口可以完成如下功能:

  1、访问数据源中的数据.

  2、获知数据源的数据类型.

  3、获知可对数据进行的各种操作.

  4、用户对数据执行某种操作时,自动创建执行该操作的软件部件的实例对象.

  javaMail API可以利用JAF从某种数据源中读取数据和获知数据的MIME类型,并用这些数据生成MIME消息中的消息体和消息类型。

 

 

BViewResolver(视图解析器)

Spring 3增加了2ViewResolver来支持更多的视图技术。

 

B.1ContentNegotiatingViewResolver

org.springframework.web.servlet.view.ContentNegotiatingViewResolver

public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport implements ViewResolver, Ordered

java.lang.Object

  org.springframework.context.support.ApplicationObjectSupport

      org.springframework.web.context.support.WebApplicationObjectSupport

          org.springframework.web.servlet.view.ContentNegotiatingViewResolver

Implementation of ViewResolver that resolves a view based on the request file name or Accept header.

ViewResolver接口的一个实现,它根据请求的文件名或请求的Accept头来解析一个视图。

The ContentNegotiatingViewResolver does not resolve views itself, but delegates to other ViewResolvers. By default, these other view resolvers are picked up automatically from the application context, though they can also be set explicitly by using the viewResolvers property. Note that in order for this view resolver to work properly, the order property needs to be set to a higher precedence than the others (the default is Ordered.HIGHEST_PRECEDENCE.)

ContentNegotiatingViewResolver它本身不解析视图,他是委托其它ViewResolver来解析的。默认情况下,它委托的ViewResolver是通过应用程序上下文自动选取的,你也可以通过viewResolvers属性来设置。注意,若要这个ViewResolver正常工作,那么它的order属性需要比其它ViewResolver更高的级别(默认是Ordered.HIGHEST_PRECEDENCE)。

This view resolver uses the requested media type to select a suitable View for a request. This media type is determined by using the following criteria:

这个ViewResolver使用请求的媒体类型(Media Type)来为一个请求选择一个合适的视图。这个媒体类型(Media Type)由以下条件决定:

1. If the requested path has a file extension and if the setFavorPathExtension(boolean) property is true, the mediaTypes property is inspected for a matching media type.

1.若请求路径有一个文件扩展名(如:htmljson等),并且setFavorPathExtension(boolean)属性设置为true,那么通过检查mediaTypes属性来指定一个匹配的媒体类型(Media Type)。

例如一个路径为(http://xxx/1.html),那么它的扩展名为(html),根据html来检查mediaTypes属性(mediaTypes是一个map,查找keyhtmlvalue)中html所对应的值(如:text/html),若返回视图是JSP,那么text/html就作为返回视图的Content-Type

2.If the request contains a parameter defining the extension and if the setFavorParameter(boolean) property is true, the mediaTypes property is inspected for a matching media type. The default name of the parameter is format and it can be configured using the parameterName property.

2.若请求包含一个定义扩展名的参数,且setFavorParameter(boolean) 属性设置为true,那么通过检查mediaTypes属性来指定一个匹配的媒体类型(Media Type)。这个参数的默认名称是format,你也可以通过parameterName属性来指定。

3.If there is no match in the mediaTypes property and if the Java Activation Framework (JAF) is both enabled and present on the classpath, FileTypeMap.getContentType(String) is used instead.

3.若没有匹配的mediaTypes属性,但JAFJava Activation Framework)在启用状态,那么FileTypeMap.getContentType(String)将替代mediaTypes来指定一个媒体类型(Media Type)。

4.If the previous steps did not result in a media type, and ignoreAcceptHeader is false, the request Accept header is used.

4.若前面的三步未找到合适的媒体类型(Media Type),且ignoreAcceptHeader属性是flase,那么请求的Accept头将被使用。

Once the requested media type has been determined, this resolver queries each delegate view resolver for a View and determines if the requested media type is compatible with the view's content type). The most compatible view is returned.

一旦确定了请求的媒体类型,这个解析器就这个视图搜寻所有委托的视图解析器(viewResolvers属性指定的),并确定这请求的媒体类型是否兼容视图的content type。其中最合适的视图将返回。

Additionally, this view resolver exposes the defaultViews property, allowing you to override the views provided by the view resolvers. Set the default views to use when a more specific view can’t be obtained from the ViewResolver chain. Note that these default views are offered as candicates, and still need have the content type requested (via file extension, parameter, or Accept header, described above).

此外,这个视图解析器公开defaultViews属性,允许你覆盖这个解析器提供的视图。当无法从视图解析器获取一个特定的视图时,将使用这个默认视图。注意,defaultViews是在无法正常获取视图时使用,它仍然需要content type(通过文件扩展名,参数或Accept头)。

You can also set the default content type directly, which will be returned when the other mechanisms (Accept header, file extension or parameter) do not result in a match.

你还可以直接设置默认的content type,当其它机制(文件扩展名,参数或Accept头)失效时,这个默认的content type将返回。

For example, if the request path is /view.html, this view resolver will look for a view that has the text/html content type (based on the html file extension). A request for /view with a text/html request Accept header has the same result.

例如,在基于文件扩展名的情况下,若一个请求路径为/view.html,那么这个视图解析器就会查找一个content type text/html的视图。一个/view的请求(它有一个text/html类型的Accept请求头)达到相似的目的。

B.1.1SET方法

void setDefaultContentType(MediaType defaultContentType)

Set the default content type.

This content type will be used when file extension, parameter, nor Accept header define a content-type, either through being disabled or empty.

设置默认的content type

当用文件扩展名,参数,Accept头等机制来定义content type时,机制未被启用或返回空时,就使用这个默认的content type值。

void setDefaultViews(List<View> defaultViews)

Set the default views to use when a more specific view can’t be obtained from the ViewResolver chain.

当无法从视图解析器获取一个特定的视图时,将使用这个默认视图。

void setFavorParameter(boolean favorParameter)

Indicate whether a request parameter should be used to determine the requested media type, in favor of looking at the Accept header. The default value is false.

For instance, when this flag is true, a request for /hotels?format=pdf will result in an AbstractPdfView being resolved, while the Accept header can be the browser-defined text/html,application/xhtml+xml.

指示是否使用一个请求参数来确定请求媒体类型,以利于Accept头的查询。默认值为false

例如,当它为true,一个请求为/hotels?format=pdf将导致一个AbstractPdfView被调用,而Accept头则可以是浏览器定义的text/htmlapplication/xhtml+xml

void setFavorPathExtension(boolean favorPathExtension)

Indicate whether the extension of the request path should be used to determine the requested media type, in favor of looking at the Accept header. The default value is true.

For instance, when this flag is true (the default), a request for /hotels.pdf will result in an AbstractPdfView being resolved, while the Accept header can be the browser-defined text/html,application/xhtml+xml.

指示是否使用一个请求路径的扩展名来确定请求媒体类型,以利于Accept头的查询。默认值为true

例如,当它为true,一个请求为/hotels.pdf将导致一个AbstractPdfView被调用,而Accept头则可以是浏览器定义的text/htmlapplication/xhtml+xml

void setIgnoreAcceptHeader(boolean ignoreAcceptHeader)

Indicate whether the HTTP Accept header should be ignored. Default is false.

If set to true, this view resolver will only refer to the file extension and/or parameter, as indicated by the favorPathExtension and favorParameter properties.

指出HTTP Accept头是否要忽略。默认为false

若设置为true,这个视图解析器将只能通过文件扩展名和参数取得content type,当然要设置favorPathExtensionfavorParameter属性。

void setMediaTypes(Map<String,String> mediaTypes)

Set the mapping from file extensions to media types.

When this mapping is not set or when an extension is not present, this view resolver will fall back to using a FileTypeMap when the Java Action Framework is available.

设置文件扩展名和媒体类型之间的映射。

当映射没有设置或一个扩展名呈现,并且JAF可用,这个视图解析器将使用FileTypeMap返回一个content type

void setOrder(int order)

          

void setParameterName(String parameterName)

Set the parameter name that can be used to determine the requested media type if the setFavorParameter(boolean) property is true. The default parameter name is format.

setFavorParameter设置为true,可以用这个方法设置决定媒体类型的参数名称。默认名称为format

void setUseJaf(boolean useJaf)

Indicate whether to use the Java Activation Framework to map from file extensions to media types.

Default is true, i.e. the Java Activation Framework is used (if available).

指出是否需要使用JAF来把文件扩展名映射为媒体类型。

默认为true。若JAF可用。

void setUseNotAcceptableStatusCode(boolean useNotAcceptableStatusCode)

Indicate whether a 406 Not Acceptable status code should be returned if no suitable view can be found.

Default is false, meaning that this view resolver returns null for resolveViewName(String, Locale) when an acceptable view cannot be found. This will allow for view resolvers chaining. When this property is set to true, resolveViewName(String, Locale) will respond with a view that sets the response status to 406 Not Acceptable instead.

它指出若没有发现合适视图时,一个406 Not Accepttable的状态码是否需要返回。

当没有发现一个合适的视图时,并且它设置为trueresolveViewName方法将用一个视图来响应请求,来替代一个406 Not Accepttable错误。

void  setViewResolvers(List<ViewResolver> viewResolvers)

Sets the view resolvers to be wrapped by this view resolver.

If this property is not set, view resolvers will be detected automatically.

设置这个视图解析器捆绑的视图解析器。

这个属性若没有设置,它将自动选择。

 

A.2TilesViewResolver

org.springframework.web.servlet.view.tiles2.TilesViewResolver

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.tiles2.TilesViewResolver

public class TilesViewResolver extends UrlBasedViewResolver

 

BView(视图)

Spring 3增加了5View来支持更多的视图技术。

 

B.1MappingJacksonJsonView

org.springframework.web.servlet.view.json.MappingJacksonJsonView

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.json.MappingJacksonJsonView

public class MappingJacksonJsonView extends AbstractView

 

 

B.2AbstractFeedView

 

B.3AbstractAtomFeedView

 

B.4AbstractRssFeedView

 

B.5MarshallingView

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多