[View]1 详解ModelAndView和视图View、ViewResolver
(2012-04-19 16:14:30)
标签:
springmvcmodelandviewviewresolverviewit |
Spring mvc视图机制
所有的web应用的mvc框架都有它定位视图的方式。Spring提供了视图解析器供你在浏览器中显示模型数据,而不必被拘束在特定的视图技术上。
Spring的控制器Controller会返回一个ModelAndView的实例。Spring根据ModelAndView实例中的View和Model把信息反馈给用户。Spring中的视图是以名字为标识的,ViewResolver是通过名字来解析view的。Spring提供了多种视图和视图解析器。
A、ModelAndView
org.springframework.web.servlet.ModelAndView
public class ModelAndView extends Object
ModelAndView如其名称所示,它代表了Spring Web MVC中呈现画面时所使用的Model与View,由于Java一次只能返回一个物件,所以ModelAndView的作用封装这两个物件,以方便您一次返回Model与View这两个物件。
构造方法
ModelAndView(String viewName)
Convenient constructor when there is no model data to expose.
最简单的ModelAndView是只有View的名称,之后View名称被View resolver,也就是org.springframework.web.servlet.View的实例解析,例如 InternalResourceView或JstlView等等。
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时使用。
B、ViewResolver(视图解析器)
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.jsp或test.pdf,就需要View来实现了)。
B.1、AbstractCachingViewResol
org.springframework.web.servlet.view.AbstractCachingViewResol
public abstract class
AbstractCachingViewResol
java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupp ort
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
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.1、UrlBasedViewResolver
org.springframework.web.servlet.view.UrlBasedViewResolver
public class UrlBasedViewResolver extends
AbstractCachingViewResol
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
将视图直接解析为对应的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的子类,如InternalResourceView,VelocityView, 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
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
Set static attributes from a Map, for all views returned by this resolver.
通过Map对象为所有由这个解析器返回的视图设置静态属性。
void
Set the content type for all views.
为所有由这个解析器返回的视图设置content type。
void
Set the order in which this ViewResolver is evaluated.
void
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
Set whether redirects should stay compatible with HTTP 1.0 clients.
设置是否支持HTTP 1.0的redirect。
void
Set the name of the RequestContext attribute for all views.
为所有由这个解析器返回的视图设置request context属性。
void
Set the prefix that gets prepended to view names when building a URL.
前缀
void
Set the suffix that gets appended to view names when building a URL.
后缀。
void
Set the view class that should be used to create views.
设置这个视图解析器创建视图的类
void
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" />
</bean>
以上是使用UrlBasedViewResolver作为解析器,InternalResourceView (JSP/Servlet)作为视图显示技术。这个解析器把视图名解析成URL。当返回的视图名为test时,这个视图解析器就会把请求传递给RequestDispatcher,RequestDispatcher再将请求传递给/WEB-INF/jsp/test.jsp。
B.1.1.1、InternalResourceViewReso
具有UrlBasedViewResolver特性(把视图名转换URL),它支持InternalResourceView,JstlView 和TilesView等视图。
(详见:《[spring]18 集成JSP和JSTL视图》)。
B.1.1.2、JasperReportsViewResolve
B.1.1.3、XsltViewResolver
B.1.1.4、AbstractTemplateViewReso
B.1.1.4.1、FreeMarkerViewResolver
B.1.1.4.2、VelocityViewResolver
B.1.1.4.2.1、VelocityLayoutViewResolv
B.1.2、XmlViewResolver
在XML文件中定义视图bean。这个解析器可支持不同的视图技术,但不支持根据local解析(国际化)。
(详见:《[spring]19 集成Execl视图》)。
B.1.3、ResourceBundleViewResolv
在ResourceBundle文件中定义视图bean的类或路径等,这个解析器通过查找ResourceBundle文件中定义好的bean,把一个视图名解析成一个视图。这个解析器可支持不同的视图技术,它还支持根据local解析。
注:ResourceBundle一般是一个扩展名为properties的资源文件(ResourceBundle具体规则可查看java.util.ResourceBundle类)。
(详见:《[spring]18 集成JSP和JSTL视图》)。
B.2、BeanNameViewResolver
在DispatcherServlet的XML配置文件直接配置视图bean。一般用于处理简单的视图bean。不推荐使用,不支持国际化,不支持缓存,支持混合使用不同的视图技术。XmlViewResolver或BeanNameViewResolver可替代它。
(详见:《[spring]22 集成PDF视图》)。
C、View(视图)
org.springframework.web.servlet.View
public interface View
处理请求的准备工作,并将该请求提交给某个具体的视图技术。
我们发现spring内建了许多类实现View,主要是对不同的View技术的支持。如:JSP、JSTL、Tiles、Velocity、Freemarker、XSLT、JasperReports等等。当然我们也可以自定义view的实现。
C.1、AbstractView
org.springframework.web.servlet.view.AbstractView
java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupp ort
org.springframework.web.servlet.view.AbstractView
public abstract class AbstractView extends
WebApplicationObjectSupp
Set方法
void
Set static attributes for this view from a java.util.Properties object.
void
Set static attributes as a CSV string.
void
Set static attributes for this view from a Map.
void
Set the view's name.
void
Set the content type for this view.
void
Set the name of the RequestContext attribute for this view.
C.1.1、AbstractExcelView
为以Apache POI实现Execl封装的View
(详见:《[spring]19 集成Execl视图》)。
C.1.2、AbstractJExcelView
为以jExcel API实现Execl封装的View
(详见:《[spring]19 集成Execl视图》)。
C.1.3、AbstractPdfView
为以iText 2.x版本实现PDF封装的View
(详见:《[spring]22 集成PDF视图》)。
C.1.4、AbstractXsltView
C.1.5、AbstractUrlBasedView
C.1.5.1、AbstractJasperReportsVie
C.1.5.1.1、AbstractJasperReportsSin
C.1.5.1.1.1、ConfigurableJasperReport
C.1.5.1.1.2、JasperReportsCsvView
C.1.5.1.1.3、JasperReportsHtmlView
C.1.5.1.1.4、JasperReportsPdfView
C.1.5.1.1.5、JasperReportsXlsView
C.1.5.1.2、JasperReportsMultiFormat
C.1.5.2、AbstractPdfStamperView
C.1.5.3、AbstractTemplateView
C.1.5.3.1、FreeMarkerView
C.1.5.3.2、VelocityView
C.1.5.3.2.1、VelocityToolboxView
C.1.5.3.2.1.1、VelocityLayoutView
C.1.5.4、InternalResourceView
为JSP和Servlet封装的View
(详见:《[spring]18 集成JSP和JSTL视图》)。
C.1.5.4.1、JstlView
为JSTL封装的View
(详见:《[spring]18 集成JSP和JSTL视图》)。
C.1.5.4.2、TilesView
C.1.5.4.2.1、TilesJstlView
C.1.5.5、RedirectView
C.1.5.6、TilesView
C.1.5.7、XsltView