一、快速搭建 Velocity 框架

1. 加入所需 Jar 包

<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>velocity-tools</groupId>
<artifactId>velocity-tools-generic</artifactId>
<version>2.0-beta1</version>
</dependency>
<dependency>
<groupId>velocity-tools</groupId>
<artifactId>velocity-tools-view</artifactId>
<version>2.0-beta1</version>
</dependency>
<dependency>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
<version>2.1</version>
</dependency>

2. 修改 SpringMVC 的配置文件 springmvc-servlet.xml

旧配置:

<!-- 对加载页面的路径解析 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <!-- 前缀 -->
  <property name="prefix" value="/"></property>
  <!-- 后缀 -->
  <property name="suffix" value=".html"></property>
</bean>

改为:

<!-- 对加载页面的路径解析 不需要配 prefix, 在 velocity-servlet.xml 中配置 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
  <!-- 加载 toolbox 配置文件 -->
  <property name="toolboxConfigLocation" value="/WEB-INF/velocityToolBox.xml" />
  <!-- 后缀 -->
  <property name="suffix" value=".vm"></property>
</bean>

3. 在 WEB-INF 下面创建配置文件,命名为 velocityToolBox.xml

<toolbox>
  <!-- application start -->
  <tool>
    <key>date</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.DateTool</class>
  </tool>
  <tool>
<key>math</key>
<scope>application</scope>
    <class>org.apache.velocity.tools.generic.MathTool</class>
  </tool>
  <!-- application end -->
</toolbox>

4. 在 WEB-INF 下创建配置文件 velocity-servlet.xml

<bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
  <!-- 模版路径 -->
  <property name="resourceLoaderPath">
    <!-- prefix -->
    <value>/</value>
  </property>
  <!-- 模版属性 -->
  <property name="velocityProperties">
    <props>
      <prop key="input.encoding">utf-8</prop>
      <prop key="output.encoding">utf-8</prop>
    </props>
  </property>
</bean>

记得在 web.xml 文件中引入 velocity-servlet.xml

5. velocity 模板的一些基本指令

比如循环:后台传递过来的用户信息列表 userList

#foreach($item in $userList)
<div>$item.userName $item.createDate</div>
#end

二. 如何自定义标签

1. 先创建一个自定义标签 Java 类。必须要有 @DefaultKey("") 注解

package com.erim.web.common;

@DefaultKey("erim")
public class ErimTag {
  // 格式化时间
public String formatDate(Date date) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
}
}

2. 修改 WEB-INF 下 velocityToolBox.xml 引入 Java 类

<tool>
<key>erim</key>
<scope>application</scope>
<class>com.erim.web.common.ErimTag</class>
</tool>

这样自定义注解就生效了。假如后台有一个时间变量 createDate 需要在页面渲染,渲染完发现格式是酱紫的:

Thu May 12 15:20:02 CST 2016

这就需要用到我们的自定义标签:

$erim.formatDate($createDate)

三. 如何自定义指令

1. 还是先创建一个 Java 类,继承 Directive。

public class TextTag extends Directive {

    @Override
public String getName() {
return "vText";
} @Override
public int getType() {
return LINE;
} @Override
public boolean render(InternalContextAdapter context, Writer writer, Node node)
throws IOException, ResourceNotFoundException, ParseErrorException,
MethodInvocationException {
// 获取第一个参数
SimpleNode user = (SimpleNode) node.jjtGetChild(0);
// 格式化
String userName = (String) user.value(context);
// 操作
userName = userName + ".....";
// 输出
writer.write(userName);
return true;
} }

2. 在 WEB-INF 下创建资源文件 velocity.properties

input.encoding=utf-8
output.encoding=utf-8
userdirective=com.erim.web.common.TextTag

其中 userdirective 的值为自定义指令 Java 类的地址,多个 Java 类以逗号分隔

3. 修改 velocity-servlet.xml 文件

<!-- velocity 相关配置 -->
<bean id="velocityConfigurer"
  class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
  <!-- 引入外部资源 -->
  <property name="configLocation" value="/WEB-INF/velocity.properties" />
  <!-- 模板路径 -->
  <property name="resourceLoaderPath" value="/templates/"></property>
  <!-- 模板属性 -->
  <property name="velocityProperties">
    <props>
      <prop key="input.encoding">utf-8</prop>
      <prop key="output.encoding">utf-8</prop>
    </props>
  </property>
</bean>

4. 使用场景

#vText("hhahaha")

hahahha, demo 就是这么任意

#vText 就是上面 getName() 方法中指定的字符串。

SpringMVC 学习-如何搭配使用 Velocity 页面模板的更多相关文章

  1. SpringMvc学习心得(五)控制器产生与构建

    SpringMvc学习心得(五)控制器产生与构建 标签: springspring mvc框架 2016-03-22 15:29 140人阅读 评论(0) 收藏 举报  分类: Spring(4)  ...

  2. SpringMVC学习系列-后记 解决GET请求时中文乱码的问题

    SpringMVC学习系列-后记 解决GET请求时中文乱码的问题 之前项目中的web.xml中的编码设置: <filter> <filter-name>CharacterEnc ...

  3. 史上最全的SpringMVC学习笔记

    SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于Spring ...

  4. SpringMVC学习系列-后记 开启项目的OpenSessionInView

    在系列的 SpringMVC学习系列(12) 完结篇 的示例项目中,由于当时考虑到OpenSessionInView会对性能有一定的影响,所以就没有配置项目的OpenSessionInView.在ma ...

  5. SpringMVC学习系列-后记 结合SpringMVC和Hibernate-validator,根据后台验证规则自动生成前台的js验证代码

    在SpringMVC学习系列(6) 之 数据验证中我们已经学习了如何结合Hibernate-validator进行后台的数据合法性验证,但是通常来说后台验证只是第二道保险,为了更好的用户体验会现在前端 ...

  6. springmvc学习笔记--REST API的异常处理

    前言: 最近使用springmvc写了不少rest api, 觉得真是一个好框架. 之前描述的几篇关于rest api的文章, 其实还是不够完善. 比如当遇到参数缺失, 类型不匹配的情况时, 直接抛出 ...

  7. springmvc学习笔记---面向移动端支持REST API

    前言: springmvc对注解的支持非常灵活和飘逸, 也得web编程少了以往很大一坨配置项. 另一方面移动互联网的到来, 使得REST API变得流行, 甚至成为主流. 因此我们来关注下spring ...

  8. SpringMVC:学习笔记(8)——文件上传

    SpringMVC--文件上传 说明: 文件上传的途径 文件上传主要有两种方式: 1.使用Apache Commons FileUpload元件. 2.利用Servlet3.0及其更高版本的内置支持. ...

  9. springmvc学习笔记(简介及使用)

    springmvc学习笔记(简介及使用) 工作之余, 回顾了一下springmvc的相关内容, 这次也为后面复习什么的做个标记, 也希望能与大家交流学习, 通过回帖留言等方式表达自己的观点或学习心得. ...

随机推荐

  1. Chorme 快捷键

    掌握谷歌浏览器的快捷键,能提升一定的使用效率. Windows 和 Linux 标签页和窗口快捷键 操作 快捷键 打开新窗口 Ctrl + n 在隐身模式下打开新窗口 Ctrl + Shift + n ...

  2. C#导出EXCEL的方法

    /// using System; using System.Collections.Generic; using System.Text; using System.Data; using Syst ...

  3. C# DataTable转实体通用方法

    public static class DataTableHelper { public static T GetEntity<T>(DataTable table) where T : ...

  4. C#隐藏(new)方法和重写(override)方法

    在基类调用的时候 隐藏方法还是调用基类的方法 而重写方法调用的就是子类的中的方法 同时,当子类中的方法与父类或者所实现的接口中的扩展方法冲突时,那么此时相当于一个隐藏方法 基类调用或者接口调用的时候使 ...

  5. scala占位符_的用法

    占位符的用途颇多,先介绍几种常用的用法 1.作为"通配符",类似Java中的*.如import scala.math._2.:_*作为一个整体,告诉编译器你希望将某个参数当作参数序 ...

  6. github使用方法(一)

    github是目前流行的代码托管网站. github.com是一个网站,它为你提供一个远程版本库(你和你的协作者的工作成果最终提交在这里):同时它也是一个共享平台,你可以在这里找到数不尽的源码. 关于 ...

  7. Inverse属性和cascade属性以及集合的多对多关系

    Inverse属性 Inverse属性,是在维护关联关系的时候起作用的. 表示控制权是否转移.(在一的一方起作用) Inverse = true, 控制反转. Inverse = false  不反转 ...

  8. Web安全知多少

    随着Web2.0.网络社交等一系列新型的互联网产品的诞生,基于Web环境的互联网应用越来越广泛,企业信息化的过程中,越来越多的应用都架设在Web平台上.Web业务的迅速发展吸引了黑客们的强烈关注,接踵 ...

  9. javascript基础(二)类型转换

    原文http://pij.robinqu.me/ 类型转换 当期望使用一个布尔值的时候,可以提供任意类型值,JavaScript将根据需要自行转换类型.类型转换可以分为隐式转换和显式转换. 显式转换 ...

  10. dubbo 分布式架构学习视频链接

    http://www.roncoo.com/course/view/f614343765bc4aac8597c6d8b38f06fd 书籍 大型分布式网站架构设计与实践 http://item.jd. ...