Java模版引擎velocity的使用

前言

關於velocity與SpringMVC的配置請參考前一篇文章,此處再也不介紹。velocity做爲Java模版引擎的主要目的是爲了容許任何人使用簡單而強大的模板語言來引用定義在Java代碼中的對象。在velocity文件中能夠給該頁面指定模版佈局,從而節省了大量的時間去寫通用的模版佈局。能夠定義變量,與Java方法進行交互。javascript

定義一個layout模版

在上一篇文章中提到了配置默認模版,固然也能夠不使用默認模版即在要用到的頁面的最上端寫上css

#set($layout='layout/yourlayout.vm')

那麼如何定義一個layout,看下面的例子:html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
    <TITLE>$!page_title 測試layout頁面</TITLE>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="Content-Language" content="zh-CN"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="Content-Language" content="zh-CN"/>
    <link href="/resources/lib/font-awesome/css/font-awesome.min.css" tppabs="/resources/lib/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
    <link href="/resources/lib/bootstrap/css/bootstrap.min.css" tppabs="/resources/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
</HEAD>
<BODY>
    $screen_content
<script src="/resources/lib/jquery-1.10.2.min.js" tppabs="/resources/lib/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="/resources/lib/jquery-migrate-1.2.1.min.js" tppabs="/resources/lib/jquery-migrate-1.2.1.min.js" type="text/javascript"></script>
</BODY>
</HTML>

那麼$screen_content所在的位置便是你所打開頁面將要佔用的位置。java

定義一個變量

#set($var='xxxx')

##輸出上面的變量直接使用$var

魂環數組或者list

#foreach($ele in $list)
<span>output element:$ele</span>
#end

條件判斷

#if($var=='xxxx')
<span>it is right</span>
#elseif($var=='')
<span>sorry</span>
#else
<span>it is wrong</span>
#end

內置對象

相似於JSP,velocity也有本身的內置對象,如$request,$response,$session這樣以來咱們就能夠將Java對象request和response以及session裏的東西垂手可得的展示在web頁面裏。node

自定義標籤(指令)

velocity同時也支持自定義標籤,或者稱爲指令。若是查看velocity的源碼就會發現有個directive.properties
jquery

directive.1=org.apache.velocity.runtime.directive.Foreach
directive.2=org.apache.velocity.runtime.directive.Include
directive.3=org.apache.velocity.runtime.directive.Parse
directive.4=org.apache.velocity.runtime.directive.Macro
directive.5=org.apache.velocity.runtime.directive.Literal
directive.6=org.apache.velocity.runtime.directive.Evaluate
directive.7=org.apache.velocity.runtime.directive.Break
directive.8=org.apache.velocity.runtime.directive.Define
directive.9=org.apache.velocity.runtime.directive.Stop

這裏正是velocity當前所提供的9個標籤(指令),下面咱們新添加一個指令sayweb

<body>
  #say("hello")
</body>

java代碼實現spring

import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.directive.Directive;
import org.apache.velocity.runtime.parser.node.Node;
import org.apache.velocity.runtime.parser.node.SimpleNode;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.service.template.TemplateService;
import com.alibaba.click.util.HostUtil;

public class Say extends Directive{
	
	@Autowired
	TemplateService templateService;
	
	private static final VelocityEngine velocityEngine = new VelocityEngine();

	@Override
	public String getName() {
		return "say";
	}

	@Override
	public int getType() {
		return LINE;
	}

	@Override
	public boolean render(InternalContextAdapter context, Writer writer,
			Node node) throws IOException, ResourceNotFoundException,
			ParseErrorException, MethodInvocationException {
		SimpleNode sn = (SimpleNode) node.jjtGetChild(0);   
		writer.write((String)sn.value(context));
        return true;
	}
	
}

接下來就能夠在velocity.properties文件裏添加上apache

say=cn.buglife.demo.msv.directive.Say
相關文章
相關標籤/搜索