添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
天涯  ·  Spring MVC 整合 ...·  5 年前    · 
天涯  ·  spring boot ...·  5 年前    · 
天涯  ·  SpringBoot使用velocity模板 ...·  5 年前    · 
天涯  ·  spring-boot-starter-ve ...·  5 年前    · 
一直单身的跑步鞋  ·  sql ...·  1 年前    · 

发布于:2016-10-31

Apache Velocity是一款基于Java的模板引擎,允许使用模版语言来引用Java对象,与jsp类似。

直接介绍Spring MVC 4 与Velocity整合。

一、添加Maven依赖

<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>1.7</version>
</dependency>
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-tools</artifactId>
    <version>2.0</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.1.5.RELEASE</version>
</dependency>

二、增加Spring配置

在Spring mvc的配置文件中增加视图解析器与velocity配置

<bean id="velocityViewResolver"
    class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
    <property name="suffix" value=".vm" />
    <property name="contentType" value="text/html;charset=utf-8" />
    <!-- 允许velocity从session中取值 -->
    <property name="exposeSessionAttributes" value="true"/>
    <!-- 允许重写session中的值 -->
    <property name="allowSessionOverride" value="true"/>
    <!-- 如果有多个视图解析器,该属性可以指定优先级,值越大优先级越高 -->
    <property name="order" value="9" />
</bean>
<bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/templates/" /> <!-- 模版文件路径 -->
<property name="velocityProperties">
    <props>
        <prop key="input.encoding">utf-8</prop>
        <prop key="output.encoding">utf-8</prop>
        <prop key="directive.set.null.allowed">true</prop>
        <prop key="file.resource.loader.cache">true</prop> <!-- 允许缓存 -->
        <prop key="velocimacro.context.localscope">true</prop>
        <prop key="resource.manager.defaultcache.size">0</prop> <!-- 缓存大小不限制 -->
        <!-- 开启缓存后会定期检查模版是否有修改,单位秒,如不需要检查可改为0 -->
        <prop key="file.resource.loader.modificationCheckInterval">0</prop>
    </props>
</property>
</bean>

完成上面两步后就可以创建模版文件进行开发了。

Spring mvc 返回模版视图示例:

@RequestMapping(value = "/test")
public String test(Model model,HttpSession httpSession) {
    model.addAttribute("name","测试");
    // 在页面上用$userName取值
    httpSession.setAttribute("userName", "test");
    // 模版文件路径templates/other/test.vm
    return "other/test";

具体的模版语言语法参考:
http://velocity.apache.org/engine/1.7/user-guide.html

三、自定义工具类

velocity虽然内置一些函数,但有时还是难以满足我们的需求或者比较繁琐。下面介绍如何创建自定义工具类并在页面使用它们。

1、编写自定义工具类(以判断字符串是否为空或者null举例)

import com.google.common.base.Strings;
public class VelocityStringUtils {
     * 判断字符串是否是null或者空字符串
    public boolean isNullOrEmpty(String s) {
        return Strings.isNullOrEmpty(s);

2、创建自定义工具类配置文件

<?xml version="1.0" encoding="UTF-8"?>
<toolbox>
        <!-- 模板中调用名 -->
        <key>StringUtils</key>
        <scope>application</scope>
        <!-- 类全名 -->
        <class>com.xxx.utils.VelocityStringUtils</class>
    </tool>
</toolbox>

3、修改Spring配置文件

<bean id="velocityViewResolver"
    class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
    <property name="suffix" value=".vm" />
    <property name="contentType" value="text/html;charset=utf-8" />
    <property name="exposeSessionAttributes" value="true"/>
    <property name="allowSessionOverride" value="true"/>
    <property name="order" value="9" />
    <!-- 指定自定义工具类配置文件位置 -->
    <property name="toolboxConfigLocation" value="/WEB-INF/Toolbox.xml" />
</bean>

4、使用自定义工具类

当有用户的昵称时显示欢迎信息。

#if(!$StringUtils.isNullOrEmpty($user.name))
<div>欢迎您:$user.name</div>