开发者社区> 问答> 正文

Spring已集成jsp的环境下同时集成Velocity

1、集成使用的jar:Spring 3.0.5 ,Velocity-1.7,velocity-tools-1.4

2、配制后缀(*.vm)映射。修改web.xml,添加如下代码


<servlet>
        <servlet-name> my</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
   </servlet>
   
   <servlet-mapping>
       <servlet-name> my</servlet-name>
       <url-pattern>*.vm</url-pattern>
   </servlet-mapping>

并相应的创建一个my-servlet.xml(webapp\WEB-INF\my-servlet.xml),因为DispatcherServlet会去找这个配制文件并初始化,这个相当重要。



3、配制解析*.vm的请求



<?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" xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
   <!-- 配制velocity的解析视图 -->
   <!-- VelocityResolver 继承Spring的VelocityViewResolver,并扩展其layout模版布局 -->
   <bean id="viewResolver2" class="com.my.velocity.VelocityResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".vm" />
      <property name="contentType" value="text/html;charset=UTF-8" />
      <!-- VelocityLayoutView 继承 Spring的VelocityToolboxView,用于支持toolbox.xml中的类  -->
      <property name="viewClass" value="com.my.velocity.VelocityLayoutView" />  
      <property name="layoutUrl" value="/WEB-INF/velocity/layout/default.vm"/>
      <property name="layoutKey" value="layout"/>
      <property name="layoutDir" value="/WEB-INF/velocity/layout/" />
      <property name="toolboxConfigLocation" value="/WEB-INF/velocity/toolbox.xml"/> 
    </bean>
    <!-- velocity的本身配制 -->
    <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
   <property name="resourceLoaderPath" value="/" />
   <property name="configLocation" value="/WEB-INF/velocity/velocity.properties"/>
    </bean>
   <!-- 配制 *.vm 解析UIRI相对应的Controller类-->
   <import resource=" dispatcher-servlet-my.xml"/>
</beans>


4、页面请求与表单处理(/WEB-INF/dispatcher-servlet-blog.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:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
   <bean name="/my/*.vm" class="com.my.blog.controller.DiaryController">
       <property name="diaryService" ref="diaryService" />
   </bean>
</beans>
如:显示 /blog/user/diary_cata.vm的数据,在DiaryController.java写一个如下方法即可:

public ModelAndView diary_cata(HttpServletRequest req,HttpServletResponse resp){
       ModelAndView mv = new ModelAndView("blog/diary/diary_cata");
       return mv;
   }
提交表单数据同理:

public ModelAndView save_diary(HttpServletRequest req,HttpServletResponse resp){
       ModelAndView mv = new ModelAndView("blog/diary/save_diary");
       return mv;
   }

展开
收起
a123456678 2016-03-12 17:28:48 3516 0
1 条回答
写回答
取消 提交回答
  • 1. [文件] VelocityLayoutView.java ~ 5KB     下载(22)     
    
    package com.my.velocity;
     
    import java.io.StringWriter;
    import java.util.Locale;
     
    import javax.servlet.http.HttpServletResponse;
     
    import org.apache.velocity.Template;
    import org.apache.velocity.context.Context;
    import org.apache.velocity.exception.ResourceNotFoundException;
    import org.springframework.core.NestedIOException;
    import org.springframework.web.servlet.view.velocity.VelocityToolboxView;
     
    public class VelocityLayoutView extends VelocityToolboxView {
        /**
         * The default {@link #setLayoutUrl(String) layout url}.
         */
        public static final String DEFAULT_LAYOUT_URL = "layout.vm";
        public static final String DEFAULT_LAYOUT_DIR = "/";
        /**
         * The default {@link #setLayoutKey(String) layout key}.
         */
        public static final String DEFAULT_LAYOUT_KEY = "layout";
     
        /**
         * The default {@link #setScreenContentKey(String) screen content key}.
         */
        public static final String DEFAULT_SCREEN_CONTENT_KEY = "screen_content";
     
     
        private String layoutUrl = DEFAULT_LAYOUT_URL;
     
        private String layoutKey = DEFAULT_LAYOUT_KEY;
        private String screenContentKey = DEFAULT_SCREEN_CONTENT_KEY;
        private String layoutDir = DEFAULT_LAYOUT_DIR;
         
        /**
         * Set the layout template to use. Default is {@link #DEFAULT_LAYOUT_URL "layout.vm"}.
         * @param layoutUrl the template location (relative to the template
         * root directory)
         */
        public void setLayoutUrl(String layoutUrl) {
            this.layoutUrl = layoutUrl;
        }
        public String getLayoutDir() {
            return layoutDir;
        }
     
        public void setLayoutDir(String layoutDir) {
            this.layoutDir = layoutDir;
        }
     
        /**
         * Set the context key used to specify an alternate layout to be used instead
         * of the default layout. Screen content templates can override the layout
         * template that they wish to be wrapped with by setting this value in the
         * template, for example:<br>
         * <code>#set( $layout = "MyLayout.vm" )</code>
         * <p>Default key is {@link #DEFAULT_LAYOUT_KEY "layout"}, as illustrated above.
         * @param layoutKey the name of the key you wish to use in your
         * screen content templates to override the layout template
         */
        public void setLayoutKey(String layoutKey) {
            this.layoutKey = layoutKey;
        }
     
        /**
         * Set the name of the context key that will hold the content of
         * the screen within the layout template. This key must be present
         * in the layout template for the current screen to be rendered.
         * <p>Default is {@link #DEFAULT_SCREEN_CONTENT_KEY "screen_content"}:
         * accessed in VTL as <code>$screen_content</code>.
         * @param screenContentKey the name of the screen content key to use
         */
        public void setScreenContentKey(String screenContentKey) {
            this.screenContentKey = screenContentKey;
        }
     
     
        /**
         * Overrides <code>VelocityView.checkTemplate()</code> to additionally check
         * that both the layout template and the screen content template can be loaded.
         * Note that during rendering of the screen content, the layout template
         * can be changed which may invalidate any early checking done here.
         */
        @Override
        public boolean checkResource(Locale locale) throws Exception {
            if (!super.checkResource(locale)) {
                return false;
            }
     
            try {
                // Check that we can get the template, even if we might subsequently get it again.
                getTemplate(this.layoutUrl);
                return true;
            }
            catch (ResourceNotFoundException ex) {
                throw new NestedIOException("Cannot find Velocity template for URL [" + this.layoutUrl +
                        "]: Did you specify the correct resource loader path?", ex);
            }
            catch (Exception ex) {
                throw new NestedIOException(
                        "Could not load Velocity template for URL [" + this.layoutUrl + "]", ex);
            }
        }
     
        /**
         * Overrides the normal rendering process in order to pre-process the Context,
         * merging it with the screen template into a single value (identified by the
         * value of screenContentKey). The layout template is then merged with the
         * modified Context in the super class.
         */
        @Override
        protected void doRender(Context context, HttpServletResponse response) throws Exception {
            renderScreenContent(context);
     
            // Velocity context now includes any mappings that were defined
            // (via #set) in screen content template.
            // The screen template can overrule the layout by doing
            // #set( $layout = "MyLayout.vm" )
            String layoutUrlToUse = (String) context.get(this.layoutKey);
            if (layoutUrlToUse != null) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Screen content template has requested layout [" + layoutUrlToUse + "]");
                }
                if(this.layoutDir != null){
                    layoutUrlToUse = this.layoutDir + layoutUrlToUse;
                }
            }
            else {
                // No explicit layout URL given -> use default layout of this view.
                layoutUrlToUse = this.layoutUrl;
            }
     
            mergeTemplate(getTemplate(layoutUrlToUse), context, response);
        }
     
        /**
         * The resulting context contains any mappings from render, plus screen content.
         */
        private void renderScreenContent(Context velocityContext) throws Exception {
            if (logger.isDebugEnabled()) {
                logger.debug("Rendering screen content template [" + getUrl() + "]");
            }
     
            StringWriter sw = new StringWriter();
            Template screenContentTemplate = getTemplate(getUrl());
            screenContentTemplate.merge(velocityContext, sw);
     
            // Put rendered content into Velocity context.
            velocityContext.put(this.screenContentKey, sw.toString());
        }
    }
    2. [文件] VelocityResolver.java ~ 3KB     下载(15)     
    
    package com.my.velocity;
     
    import org.springframework.web.servlet.view.AbstractUrlBasedView;
    import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
     
    public class VelocityResolver extends VelocityViewResolver {
         
        private String layoutUrl;
        private String layoutKey;
        private String screenContentKey;
        private String layoutDir;
         
        public String getLayoutDir() {
            return layoutDir;
        }
        public void setLayoutDir(String layoutDir) {
            this.layoutDir = layoutDir;
        }
        /**
         * Set the layout template to use. Default is "layout.vm".
         * @param layoutUrl the template location (relative to the template
         * root directory)
         * @see VelocityLayoutView#setLayoutUrl
         */
        public void setLayoutUrl(String layoutUrl) {
            this.layoutUrl = layoutUrl;
        }
     
        /**
         * Set the context key used to specify an alternate layout to be used instead
         * of the default layout. Screen content templates can override the layout
         * template that they wish to be wrapped with by setting this value in the
         * template, for example:<br>
         * <code>#set( $layout = "MyLayout.vm" )</code>
         * <p>The default key is "layout", as illustrated above.
         * @param layoutKey the name of the key you wish to use in your
         * screen content templates to override the layout template
         * @see VelocityLayoutView#setLayoutKey
         */
        public void setLayoutKey(String layoutKey) {
            this.layoutKey = layoutKey;
        }
     
        /**
         * Set the name of the context key that will hold the content of
         * the screen within the layout template. This key must be present
         * in the layout template for the current screen to be rendered.
         * <p>Default is "screen_content": accessed in VTL as
         * <code>$screen_content</code>.
         * @param screenContentKey the name of the screen content key to use
         * @see VelocityLayoutView#setScreenContentKey
         */
        public void setScreenContentKey(String screenContentKey) {
            this.screenContentKey = screenContentKey;
        }
         
        /**
         * Requires VelocityLayoutView.
         * @see VelocityLayoutView
         */
        protected Class requiredViewClass() {
            return VelocityLayoutView.class;
        }
     
     
        protected AbstractUrlBasedView buildView(String viewName) throws Exception {
            VelocityLayoutView view = (VelocityLayoutView) super.buildView(viewName);
            //设置模版文件(layout)的根目录
            if(this.layoutDir != null){
                view.setLayoutDir(this.layoutDir);
            }
            if (this.layoutUrl != null) {
                view.setLayoutUrl(this.layoutUrl);
            }
             
            if (this.layoutKey != null) {
                view.setLayoutKey(this.layoutKey);
            }
            if (this.screenContentKey != null) {
                view.setScreenContentKey(this.screenContentKey);
            }
            return view;
        }
    }
    2019-07-17 19:01:16
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载