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

[View]1 详解ModelAndView和视图View、ViewResolver

(2012-04-19 16:14:30)
标签:

spring

mvc

modelandview

viewresolver

view

it

Spring mvc视图机制

所有的web应用的mvc框架都有它定位视图的方式。Spring提供了视图解析器供你在浏览器中显示模型数据,而不必被拘束在特定的视图技术上。

Spring的控制器Controller会返回一个ModelAndView的实例。Spring根据ModelAndView实例中的ViewModel把信息反馈给用户。Spring中的视图是以名字为标识的,ViewResolver是通过名字来解析view的。Spring提供了多种视图和视图解析器。

 

AModelAndView

org.springframework.web.servlet.ModelAndView

public class ModelAndView extends Object

ModelAndView如其名称所示,它代表了Spring Web MVC中呈现画面时所使用的ModelView,由于Java一次只能返回一个物件,所以ModelAndView的作用封装这两个物件,以方便您一次返回ModelView这两个物件。

构造方法

ModelAndView(String viewName)

Convenient constructor when there is no model data to expose.

最简单的ModelAndView是只有View的名称,之后View名称被View resolver,也就是org.springframework.web.servlet.View的实例解析,例如 InternalResourceViewJstlView等等。

ModelAndView(String viewName, Map model)

Creates new ModelAndView given a view name and a model.

如果您要返回呈现画面时所需的Model资料,则可以使用Map来收集呈现View时所需的资料,然后在建构ModelAndView作为建构时的参数。

ModelAndView(String viewName, String modelName, Object modelObject)

Convenient constructor to take a single model object.

返回单个model时使用。

 

BViewResolver(视图解析器)

org.springframework.web.servlet.ViewResolver

public interface ViewResolver

现在我们有了view名称,也有了显示时需要的model资料,那么我们如何显示view了。这就需要用到ViewResolver,它提供了从视图名称到实际视图的映射。

(例如我们得到的view名称为test,通过ViewResolver我们把它映射到/WEB-INF/jsp/test.jsp的资源上,当然也可以把test映射到test.pdf的资源上,这部分工作由ViewResolver来完成,但是具体如何显示test.jsptest.pdf,就需要View来实现了)。

 

B.1AbstractCachingViewResolver(抽象类)

org.springframework.web.servlet.view.AbstractCachingViewResolver

public abstract class AbstractCachingViewResolver extends WebApplicationObjectSupport implements ViewResolver

java.lang.Object
  
 


 
  
  
  
  
  
  
  
  
  
  
  
  
 
 
 

 
org.springframework.context.support.ApplicationObjectSupport
      
 
org.springframework.web.context.support.WebApplicationObjectSupport
          
 
org.springframework.web.servlet.view.AbstractCachingViewResolver

Convenient base class for ViewResolver implementations. Caches View objects once resolved: This means that view resolution won't be a performance problem, no matter how costly initial view retrieval is.

ViewResolver接口的基础实现。缓存视图对象一旦被解析,就意味着视图的解析将不会有性能的问题,不管消耗多少资源来初始化视图。

Subclasses need to implement the loadView(java.lang.String, java.util.Locale) template method, building the View object for a specific view name and locale.

子类需要实现loadView方法,来为一个有具体视图名和本地化的视图构建视图对象。

抽象视图解析器实现了对视图的缓存。在视图被使用之前,通常需要进行一些准备工作。从它继承的ViewResolver将对要解析的视图进行缓存。

Set方法

void  setCache(boolean cache)

Enable or disable caching.

Default is "true": caching is enabled. Disable this only for debugging and development.

允许或关闭缓存。

默认是true。一般在调试或开发时才会关闭它。

void setCacheUnresolved(boolean cacheUnresolved)

Whether a view name once resolved to null should be cached and automatically resolved to null subsequently.

Default is "false": unresolved view names are NOT being cached. Note that this flag only applies if the general "cache" flag is kept at its default of "true" as well.

Of specific interest is the ability for some AbstractUrlBasedView implementations (FreeMarker, Velocity, Tiles) to check if an underlying resource exists via AbstractUrlBasedView.checkResource(Locale). With this flag set to "false", an underlying resource that re-appears is noticed and used. With the flag set to "true", one check is made only.

 

B.1.1UrlBasedViewResolver

org.springframework.web.servlet.view.UrlBasedViewResolver

public class UrlBasedViewResolver extends AbstractCachingViewResolver implements Ordered

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

将视图直接解析为对应的URL,不需要显示映射定义(若视图名和资源名称一样,就是不需要在spring配置文件中定义)。

Simple implementation of the ViewResolver interface, allowing for direct resolution of symbolic view names to URLs, without explicit mapping definition. This is useful if your symbolic names match the names of your view resources in a straightforward manner (i.e. the symbolic name is the unique part of the resource's filename), without the need for a dedicated mapping to be defined for each view.

ViewResolver接口的简单实现,把view名直接解析为url,不需要在上下文中显式定义。当你的view符号名称和它匹配view资源名称一致时,就没有必要为每一个view进行映射了。

Supports AbstractUrlBasedView subclasses like InternalResourceView, VelocityView and FreeMarkerView. The view class for all views generated by this resolver can be specified via the "viewClass" property.

它支持AbstractUrlBasedView的子类,如InternalResourceViewVelocityView FreeMarkerView。可以通过viewClass属性来指定这个视图解析器生成视图的视图类。

View names can either be resource URLs themselves, or get augmented by a specified prefix and/or suffix. Exporting an attribute that holds the RequestContext to all views is explicitly supported.

视图名称可以是URL资源本身,也可以通过指定前缀和后缀。

Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp"

例如,若prefix="/WEB-INF/jsp/", suffix=".jsp",那么视图名"test"就会被转化为 "/WEB-INF/jsp/test.jsp"

As a special feature, redirect URLs can be specified via the "redirect:" prefix. E.g.: "redirect:myAction.do" will trigger a redirect to the given URL, rather than resolution as standard view name. This is typically used for redirecting to a controller URL after finishing a form workflow.

一个特殊功能,可以通过"redirect:"前缀来重定向(redirect 一个URL,例如,"redirect:myAction.do"将触发一个重定向(redirect)给给定的URL,并不是返回一个标准的视图名字。这通常用于在完成一个表单工作流后重定向一个控制URL

Furthermore, forward URLs can be specified via the "forward:" prefix. E.g.: "forward:myAction.do" will trigger a forward to the given URL, rather than resolution as standard view name. This is typically used for controller URLs; it is not supposed to be used for JSP URLs - use logical view names there.

此外,可以通过" forward:"前缀来转发(forward 一个URL。例如,"forward:myAction.do"将触发一个转发(forward)给给定的URL,并不是返回一个标准的视图名字。这通常用于控制URL;它不支持使用视图名来映射的JSP路径。

Note: This class does not support localized resolution, i.e. resolving a symbolic view name to different resources depending on the current locale.

注意,这个类不支持本地化,就是它不支持根据当前区域作为参数来解析不同的资源的方式。

Note: When chaining ViewResolvers, a UrlBasedViewResolver will check whether the specified resource actually exists. However, with InternalResourceView, it is not generally possible to determine the existence of the target resource upfront. In such a scenario, a UrlBasedViewResolver will always return View for any given view name; as a consequence, it should be configured as the last ViewResolver in the chain.

注意,在一组ViewResolver链中,UrlBasedViewResolver不管指定的资源是否存在它都将会检查。然而,在解析InternalResourceView视图时,一般不可能预先知道目标资源是否存在。在这种情况下,UrlBasedViewResolver将始终返回一个视图,因此,它一般定义在视图解析链的最后。

Set方法

void  setAttributes(Properties props)

Set static attributes from a java.util.Properties object, for all views returned by this resolver.

This is the most convenient way to set static attributes. Note that static attributes can be overridden by dynamic attributes, if a value with the same name is included in the model.

通过java.util.Properties对象为所有由这个解析器返回的视图设置静态属性。

注意,这个静态属性可以被动态属性覆盖,若model中有一个相同的属性。

void  setAttributesMap(Map attributes)

Set static attributes from a Map, for all views returned by this resolver.

通过Map对象为所有由这个解析器返回的视图设置静态属性。

void  setContentType(String contentType)

Set the content type for all views.

为所有由这个解析器返回的视图设置content type

void  setOrder(int order)

Set the order in which this ViewResolver is evaluated.

void  setRedirectContextRelative(boolean redirectContextRelative)

Set whether to interpret a given redirect URL that starts with a slash ("/") as relative to the current ServletContext, i.e. as relative to the web application root.

设置是否允许"/"表示根路径。

void  setRedirectHttp10Compatible(boolean redirectHttp10Compatible)

Set whether redirects should stay compatible with HTTP 1.0 clients.

设置是否支持HTTP 1.0redirect

void  setRequestContextAttribute(String requestContextAttribute)

Set the name of the RequestContext attribute for all views.

为所有由这个解析器返回的视图设置request context属性。

void  setPrefix(String prefix)

Set the prefix that gets prepended to view names when building a URL.

前缀

void  setSuffix(String suffix)

Set the suffix that gets appended to view names when building a URL.

后缀。

void  setViewClass(Class viewClass)

Set the view class that should be used to create views.

设置这个视图解析器创建视图的类

void  setViewNames(String[] viewNames)

Set the view names (or name patterns) that can be handled by this ViewResolver. View names can contain simple wildcards such that 'my*', '*Report' and '*Repo*' will all match the view name 'myReport'.

设置可以被这个解析器处理的视图名称或视图名称模式。

UrlBasedViewResolver的应用

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

<property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" />

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

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

</bean>

以上是使用UrlBasedViewResolver作为解析器,InternalResourceView JSP/Servlet)作为视图显示技术。这个解析器把视图名解析成URL。当返回的视图名为test时,这个视图解析器就会把请求传递给RequestDispatcherRequestDispatcher再将请求传递给/WEB-INF/jsp/test.jsp

 

B.1.1.1InternalResourceViewResolver

具有UrlBasedViewResolver特性(把视图名转换URL),它支持InternalResourceViewJstlView TilesView等视图。

(详见:《[spring]18 集成JSPJSTL视图》)。

 

B.1.1.2JasperReportsViewResolver

 

B.1.1.3XsltViewResolver

 

B.1.1.4AbstractTemplateViewResolver

 

B.1.1.4.1FreeMarkerViewResolver

 

B.1.1.4.2VelocityViewResolver

 

B.1.1.4.2.1VelocityLayoutViewResolver

 

B.1.2XmlViewResolver

XML文件中定义视图bean。这个解析器可支持不同的视图技术,但不支持根据local解析(国际化)。

(详见:《[spring]19 集成Execl视图》)。

 

B.1.3ResourceBundleViewResolver

ResourceBundle文件中定义视图bean的类或路径等,这个解析器通过查找ResourceBundle文件中定义好的bean,把一个视图名解析成一个视图。这个解析器可支持不同的视图技术,它还支持根据local解析。

注:ResourceBundle一般是一个扩展名为properties的资源文件(ResourceBundle具体规则可查看java.util.ResourceBundle类)。

(详见:《[spring]18 集成JSPJSTL视图》)。

 

B.2BeanNameViewResolver

DispatcherServletXML配置文件直接配置视图bean。一般用于处理简单的视图bean。不推荐使用,不支持国际化,不支持缓存,支持混合使用不同的视图技术。XmlViewResolverBeanNameViewResolver可替代它。

(详见:《[spring]22 集成PDF视图》)。

 

CView(视图)

org.springframework.web.servlet.View

public interface View

处理请求的准备工作,并将该请求提交给某个具体的视图技术。

我们发现spring内建了许多类实现View,主要是对不同的View技术的支持。如:JSPJSTLTilesVelocityFreemarkerXSLTJasperReports等等。当然我们也可以自定义view的实现。

 

C.1AbstractView

org.springframework.web.servlet.view.AbstractView

java.lang.Object
  
 
org.springframework.context.support.ApplicationObjectSupport
      
 
org.springframework.web.context.support.WebApplicationObjectSupport
          
 
org.springframework.web.servlet.view.AbstractView

public abstract class AbstractView extends WebApplicationObjectSupport implements View, BeanNameAware

Set方法

void  setAttributes(Properties attributes)

Set static attributes for this view from a java.util.Properties object.

void  setAttributesCSV(String propString)

Set static attributes as a CSV string.

void  setAttributesMap(Map attributes)

Set static attributes for this view from a Map.

void  setBeanName(String beanName)

Set the view's name.

void  setContentType(String contentType)

Set the content type for this view.

void  setRequestContextAttribute(String requestContextAttribute)

Set the name of the RequestContext attribute for this view.

 

C.1.1AbstractExcelView

为以Apache POI实现Execl封装的View

(详见:《[spring]19 集成Execl视图》)。

 

C.1.2AbstractJExcelView

为以jExcel API实现Execl封装的View

(详见:《[spring]19 集成Execl视图》)。

 

C.1.3AbstractPdfView

为以iText 2.x版本实现PDF封装的View

(详见:《[spring]22 集成PDF视图》)。

 

C.1.4AbstractXsltView

 

C.1.5AbstractUrlBasedView

 

C.1.5.1AbstractJasperReportsView

 

C.1.5.1.1AbstractJasperReportsSingleFormatView

 

C.1.5.1.1.1ConfigurableJasperReportsView

 

C.1.5.1.1.2JasperReportsCsvView

 

C.1.5.1.1.3JasperReportsHtmlView

 

C.1.5.1.1.4JasperReportsPdfView

 

C.1.5.1.1.5JasperReportsXlsView

 

C.1.5.1.2JasperReportsMultiFormatView

 

C.1.5.2AbstractPdfStamperView

 

C.1.5.3AbstractTemplateView

 

C.1.5.3.1FreeMarkerView

 

C.1.5.3.2VelocityView

 

C.1.5.3.2.1VelocityToolboxView

 

C.1.5.3.2.1.1VelocityLayoutView

 

C.1.5.4InternalResourceView

JSPServlet封装的View

(详见:《[spring]18 集成JSPJSTL视图》)。

 

C.1.5.4.1JstlView

JSTL封装的View

(详见:《[spring]18 集成JSPJSTL视图》)。

 

C.1.5.4.2TilesView

 

C.1.5.4.2.1TilesJstlView

 

C.1.5.5RedirectView

 

C.1.5.6TilesView

 

C.1.5.7XsltView

 

 

0

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

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

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

新浪公司 版权所有