类 VelocityView

  • 所有已实现的接口:
    Aware, BeanNameAware, InitializingBean, ApplicationContextAware, ServletContextAware, View
    直接已知子类:
    VelocityToolboxView

    @Deprecated
    public class VelocityView
    extends AbstractTemplateView
    已过时。
    as of Spring 4.3, in favor of FreeMarker
    View using the Velocity template engine.

    Exposes the following JavaBean properties:

    • url: the location of the Velocity template to be wrapped, relative to the Velocity resource loader path (see VelocityConfigurer).
    • encoding (optional, default is determined by Velocity configuration): the encoding of the Velocity template file
    • velocityFormatterAttribute (optional, default=null): the name of the VelocityFormatter helper object to expose in the Velocity context of this view, or null if not needed. VelocityFormatter is part of standard Velocity.
    • dateToolAttribute (optional, default=null): the name of the DateTool helper object to expose in the Velocity context of this view, or null if not needed. DateTool is part of Velocity Tools.
    • numberToolAttribute (optional, default=null): the name of the NumberTool helper object to expose in the Velocity context of this view, or null if not needed. NumberTool is part of Velocity Tools.
    • cacheTemplate (optional, default=false): whether or not the Velocity template should be cached. It should normally be true in production, but setting this to false enables us to modify Velocity templates without restarting the application (similar to JSPs). Note that this is a minor optimization only, as Velocity itself caches templates in a modification-aware fashion.

    Depends on a VelocityConfig object such as VelocityConfigurer being accessible in the current web application context, with any bean name. Alternatively, you can set the VelocityEngine object as bean property.

    Note: Spring 3.0's VelocityView requires Velocity 1.4 or higher, and optionally Velocity Tools 1.1 or higher (depending on the use of DateTool and/or NumberTool).

    作者:
    Rod Johnson, Juergen Hoeller, Dave Syer
    另请参阅:
    VelocityConfig, VelocityConfigurer, AbstractUrlBasedView.setUrl(java.lang.String), AbstractTemplateView.setExposeSpringMacroHelpers(boolean), setEncoding(java.lang.String), setVelocityEngine(org.apache.velocity.app.VelocityEngine), VelocityConfig, VelocityConfigurer
    • 构造器详细资料

    • 方法详细资料

      • setToolAttributes

        public void setToolAttributes​(Map<String,​Class<?>> toolAttributes)
        已过时。
        Set tool attributes to expose to the view, as attribute name / class name pairs. An instance of the given class will be added to the Velocity context for each rendering operation, under the given attribute name.

        For example, an instance of MathTool, which is part of the generic package of Velocity Tools, can be bound under the attribute name "math", specifying the fully qualified class name "org.apache.velocity.tools.generic.MathTool" as value.

        Note that VelocityView can only create simple generic tools or values, that is, classes with a public default constructor and no further initialization needs. This class does not do any further checks, to not introduce a required dependency on a specific tools package.

        For tools that are part of the view package of Velocity Tools, a special Velocity context and a special init callback are needed. Use VelocityToolboxView in such a case, or override createVelocityContext and initTool accordingly.

        For a simple VelocityFormatter instance or special locale-aware instances of DateTool/NumberTool, which are part of the generic package of Velocity Tools, specify the "velocityFormatterAttribute", "dateToolAttribute" or "numberToolAttribute" properties, respectively.

        参数:
        toolAttributes - attribute names as keys, and tool class names as values
        另请参阅:
        MathTool, VelocityToolboxView, createVelocityContext(java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse), initTool(java.lang.Object, org.apache.velocity.context.Context), setDateToolAttribute(java.lang.String), setNumberToolAttribute(java.lang.String)
      • setNumberToolAttribute

        public void setNumberToolAttribute​(String numberToolAttribute)
        已过时。
        Set the name of the NumberTool helper object to expose in the Velocity context of this view, or null if not needed. The exposed NumberTool will be aware of the current locale, as determined by Spring's LocaleResolver.

        NumberTool is part of the generic package of Velocity Tools 1.1. Spring uses a special locale-aware subclass of NumberTool.

        另请参阅:
        NumberTool, RequestContextUtils.getLocale(javax.servlet.http.HttpServletRequest), LocaleResolver
      • setEncoding

        public void setEncoding​(String encoding)
        已过时。
        Set the encoding of the Velocity template file. Default is determined by the VelocityEngine: "ISO-8859-1" if not specified otherwise.

        Specify the encoding in the VelocityEngine rather than per template if all your templates share a common encoding.

      • getEncoding

        protected String getEncoding()
        已过时。
        Return the encoding for the Velocity template.
      • setCacheTemplate

        public void setCacheTemplate​(boolean cacheTemplate)
        已过时。
        Set whether the Velocity template should be cached. Default is "false". It should normally be true in production, but setting this to false enables us to modify Velocity templates without restarting the application (similar to JSPs).

        Note that this is a minor optimization only, as Velocity itself caches templates in a modification-aware fashion.

      • isCacheTemplate

        protected boolean isCacheTemplate()
        已过时。
        Return whether the Velocity template should be cached.
      • setVelocityEngine

        public void setVelocityEngine​(org.apache.velocity.app.VelocityEngine velocityEngine)
        已过时。
        Set the VelocityEngine to be used by this view.

        If this is not set, the default lookup will occur: A single VelocityConfig is expected in the current web application context, with any bean name.

        另请参阅:
        VelocityConfig
      • getVelocityEngine

        protected org.apache.velocity.app.VelocityEngine getVelocityEngine()
        已过时。
        Return the VelocityEngine used by this view.
      • checkResource

        public boolean checkResource​(Locale locale)
                              throws Exception
        已过时。
        Check that the Velocity template used for this view exists and is valid.

        Can be overridden to customize the behavior, for example in case of multiple templates to be rendered into a single view.

        覆盖:
        checkResource 在类中 AbstractUrlBasedView
        参数:
        locale - the desired Locale that we're looking for
        返回:
        true if the resource exists (or is assumed to exist); false if we know that it does not exist
        抛出:
        Exception - if the resource exists but is invalid (e.g. could not be parsed)
      • createVelocityContext

        protected org.apache.velocity.context.Context createVelocityContext​(Map<String,​Object> model)
                                                                     throws Exception
        已过时。
        Create a Velocity Context instance for the given model, to be passed to the template for merging.

        Default implementation creates an instance of Velocity's VelocityContext implementation class.

        参数:
        model - the model Map, containing the model attributes to be exposed to the view
        返回:
        the Velocity Context
        抛出:
        Exception - if there's a fatal error while creating the context
        另请参阅:
        VelocityContext
      • exposeHelpers

        protected void exposeHelpers​(org.apache.velocity.context.Context velocityContext,
                                     HttpServletRequest request,
                                     HttpServletResponse response)
                              throws Exception
        已过时。
        Expose helpers unique to each rendering operation. This is necessary so that different rendering operations can't overwrite each other's formats etc.

        Called by renderMergedTemplateModel. Default implementation delegates to exposeHelpers(velocityContext, request). This method can be overridden to add special tools to the context, needing the servlet response to initialize (see Velocity Tools, for example LinkTool and ViewTool/ChainedContext).

        参数:
        velocityContext - Velocity context that will be passed to the template
        request - current HTTP request
        response - current HTTP response
        抛出:
        Exception - if there's a fatal error while we're adding model attributes
        另请参阅:
        exposeHelpers(org.apache.velocity.context.Context, HttpServletRequest)
      • exposeHelpers

        protected void exposeHelpers​(org.apache.velocity.context.Context velocityContext,
                                     HttpServletRequest request)
                              throws Exception
        已过时。
        Expose helpers unique to each rendering operation. This is necessary so that different rendering operations can't overwrite each other's formats etc.

        Default implementation is empty. This method can be overridden to add custom helpers to the Velocity context.

        参数:
        velocityContext - Velocity context that will be passed to the template
        request - current HTTP request
        抛出:
        Exception - if there's a fatal error while we're adding model attributes
        另请参阅:
        exposeHelpers(Map, HttpServletRequest)
      • getTemplate

        protected org.apache.velocity.Template getTemplate​(String name)
                                                    throws Exception
        已过时。
        Retrieve the Velocity template specified by the given name, using the encoding specified by the "encoding" bean property.

        Can be called by subclasses to retrieve a specific template, for example to render multiple templates into a single view.

        参数:
        name - the file name of the desired template
        返回:
        the Velocity template
        抛出:
        Exception - if thrown by Velocity
        另请参阅:
        VelocityEngine.getTemplate(java.lang.String)
      • mergeTemplate

        protected void mergeTemplate​(org.apache.velocity.Template template,
                                     org.apache.velocity.context.Context context,
                                     HttpServletResponse response)
                              throws Exception
        已过时。
        Merge the template with the context. Can be overridden to customize the behavior.
        参数:
        template - the template to merge
        context - the Velocity context to use for rendering
        response - servlet response (use this to get the OutputStream or Writer)
        抛出:
        Exception - if thrown by Velocity
        另请参阅:
        Template.merge(org.apache.velocity.context.Context, java.io.Writer)