Velocity模板技术学习笔记

基本语句语法可以查看https://www.ibm.com/developerworks/cn/java/j-lo-velocity1/

HelloVelocity.java

public class HelloVelocity {

    public static void main(String[] args) {
        //初始化并取得Velocity引擎
        VelocityEngine ve = new VelocityEngine();

        //设置一些配置
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());

        //初始化引擎
        ve.init();

        //取得velocity的模板
        Template t = ve.getTemplate("Hellovelocity.vm");

        //取出velocity的上下文context
        VelocityContext ctx = new VelocityContext();

        //把数据填入上下文
        ctx.put("name", "velocity");
        ctx.put("date", (new Date()).toString());

        //创建一个list
        List temp = new ArrayList();
        temp.add(1);
        temp.add(2);

        //将list填入上下文
        ctx.put("list", temp);

        //输出流
        StringWriter sw = new StringWriter();

        //转化输出
        t.merge(ctx, sw);
        System.out.println(sw.toString());

    }

}

Hellovelocity.vm

#set($iAmvariable = "good!")
welcome $name to velocity.com
today is $date.
#foreach($i in $list)
$i
$velocityCount
#end
$iAmvariable

输出:

welcome velocity to velocity.com
today is Thu Jul 20 20:50:09 CST 2017.
1
1
2
2
good!

velocity融合springmvc的配置

<!-- 配置velocity引擎 -->
    <bean id="velocityConfigurer" name="velocityConfigurer"
        class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <!--模板存放的路径  -->
        <property name="resourceLoaderPath">
            <value>/WEB-INF/velocity</value>
        </property>
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">UTF-8</prop> <!-- 指定模板引擎进行模板处理的编码 -->
                <prop key="output.encoding">UTF-8</prop>    <!-- 指定输出流的编码 -->
                <prop key="contentType">text/html;charset=UTF-8</prop>  
                <prop key="velocimacro.library">macro/macros.vm</prop>
                <!-- <prop key="eventhandler.referenceinsertion.class">com.sde.common.web.escape.reference.NoEscapeHtmlReference</prop> 
                    <prop key="eventhandler.noescape.html.match">/(?:screen_content)|(?:noescape_.*)/</prop> -->
            </props>
        </property>
    </bean>

    <!-- 配置视图解析器 -->
    <bean id="vmViewResolver" class="com.ailk.web.VelocityLayoutViewResolverExt">
        <property name="order" value="1"/>
        <property name="cache" value="false" />
        <property name="prefix" value="" /> <!--视图前缀,即存放路径  -->
        <property name="suffix" value=".vm" />  <!-- 视图文件的后缀名 -->
        <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml" /> <!--toolbox配置文件路径 -->
        <property name="dateToolAttribute" value="dateTool" />  <!--日期函数名称 -->
        <property name="numberToolAttribute" value="number" />  <!--数字函数名称 -->
        <property name="exposeSpringMacroHelpers" value="true" />   <!-- 是否使用spring对宏定义的支持 -->
        <property name="exposeRequestAttributes" value="true" />    <!--是否开放request属性 -->
        <property name="requestContextAttribute" value="rc" />  <!--request属性引用名称 -->
        <!-- <property name="layoutUrl" value="templates/layout/default.vm"/> --><!--指定layout文件 -->
        <property name="contentType" value="text/html;charset=UTF-8" /> <!-- 页面内容的字符编码 -->

        <property name="layoutUrl" value="layout/layout.vm" />  <!-- 设置系统默认的模板路径 -->
        <property name="layoutKey" value="layout" />    <!-- 设定模板文件键值,设定该值后可以在vm文件种使用 该键值设置模板路径 -->
        <property name="screenContentKey" value="screen_content" /> <!-- 指定vm文件显示位置  -->
        <property name="attributes">
            <props>
                <prop key="relase">true</prop><!-- relase;debug -->
            </props>
        </property>
    </bean>

猜你喜欢

转载自blog.csdn.net/whiteblack_dream/article/details/75577927