SpringMVC 学习-如何搭配使用 Velocity 页面模板
一、快速搭建 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 页面模板的更多相关文章
- SpringMvc学习心得(五)控制器产生与构建
SpringMvc学习心得(五)控制器产生与构建 标签: springspring mvc框架 2016-03-22 15:29 140人阅读 评论(0) 收藏 举报 分类: Spring(4) ...
- SpringMVC学习系列-后记 解决GET请求时中文乱码的问题
SpringMVC学习系列-后记 解决GET请求时中文乱码的问题 之前项目中的web.xml中的编码设置: <filter> <filter-name>CharacterEnc ...
- 史上最全的SpringMVC学习笔记
SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于Spring ...
- SpringMVC学习系列-后记 开启项目的OpenSessionInView
在系列的 SpringMVC学习系列(12) 完结篇 的示例项目中,由于当时考虑到OpenSessionInView会对性能有一定的影响,所以就没有配置项目的OpenSessionInView.在ma ...
- SpringMVC学习系列-后记 结合SpringMVC和Hibernate-validator,根据后台验证规则自动生成前台的js验证代码
在SpringMVC学习系列(6) 之 数据验证中我们已经学习了如何结合Hibernate-validator进行后台的数据合法性验证,但是通常来说后台验证只是第二道保险,为了更好的用户体验会现在前端 ...
- springmvc学习笔记--REST API的异常处理
前言: 最近使用springmvc写了不少rest api, 觉得真是一个好框架. 之前描述的几篇关于rest api的文章, 其实还是不够完善. 比如当遇到参数缺失, 类型不匹配的情况时, 直接抛出 ...
- springmvc学习笔记---面向移动端支持REST API
前言: springmvc对注解的支持非常灵活和飘逸, 也得web编程少了以往很大一坨配置项. 另一方面移动互联网的到来, 使得REST API变得流行, 甚至成为主流. 因此我们来关注下spring ...
- SpringMVC:学习笔记(8)——文件上传
SpringMVC--文件上传 说明: 文件上传的途径 文件上传主要有两种方式: 1.使用Apache Commons FileUpload元件. 2.利用Servlet3.0及其更高版本的内置支持. ...
- springmvc学习笔记(简介及使用)
springmvc学习笔记(简介及使用) 工作之余, 回顾了一下springmvc的相关内容, 这次也为后面复习什么的做个标记, 也希望能与大家交流学习, 通过回帖留言等方式表达自己的观点或学习心得. ...
随机推荐
- String to Double出现误差
场景描述 做实际项目的时候,由于使用Double类的valueOf得到一个用String类型保存的金额参数(单位为元),当需要转换成以分为单位即整形表示(Integer类表示)时,需要用之前得到的do ...
- C#模板打印功能-模板为WPS或Excel
//---WPS----- using EtApp = ET; using System.Reflection; using System.Runtime.InteropServices; using ...
- 通过字典给类的实体属性赋值生成url字符串
private static Dictionary<string, string> SortedToDictionary(SortedDictionary<string, strin ...
- 最有用的Gulp插件汇总
HTML&CSS autoprefixer - parse CSS and add vendor prefixes to rules by Can I Use. gulp-browser-sy ...
- js 仿 asp中的 asc 和 chr 函数的代码
<script type="text/javascript">var str;var asc; str = "A";document.write(s ...
- redis8--数据持久化两种方式
持久化功能redis为了内部数据的安全考虑,会把本身的数据以文件形式保存到硬盘中一份,在服务器重启之后会把硬盘中的数据恢复到内存(redis)的里边.数据保存到硬盘的过程就称为"持久化&qu ...
- BestCoder Round #89
过了这么久才来写-- BC的后两道题好难--(第二道题也不怎么简单--) 1001 Fxx and string 正着倒着枚举一次就ok #include<iostream> #inclu ...
- HDU 5834 Magic boy Bi Luo with his excited tree
树形dp. 先dfs一次处理子树上的最优解,记录一下回到这个点和不回到这个点的最优解. 然后从上到下可以推出所有答案.细节较多,很容易写错. #pragma comment(linker, " ...
- Ubuntu系统上安装搜狗拼音输入法sogou
1. 首先在百度搜索搜狗拼音输入法Linux就能看到它的官网:http://pinyin.sogou.com/linux/ 下载后直接安装.安装成功后在控制台输入 im-config 如果选中的是fc ...
- python网络编程 — HTTP客户端
A simple http client. It gets the contents of special webserver page and print it.(Default path is & ...