Velocity



package com.g3net.templeteEngine;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import com.book.bussiness.TempleteData;
import com.book.domain.Book;

/**
* 模版处理器,用于拼装页面以及数据
*
*
*/
public class TempleteProcesser {
private static Logger log = Logger.getLogger(TempleteProcesser.class);
/**
*
* @param tname 经过处理的模板名,保证在不同平台分辨率下的唯一性。当这个参数没有的时候,采用动态模板进行操作
* @param realTname 真实的模板名,未经过处理
* @param tcontent
* @param vars
* @return
*/
public static String process(TempleteData td,String tcontent,Map<String,Object> vars){
/**
* 初始化上下文环境
*/
VelocityContext context = new VelocityContext();
if(vars!=null)
{
Iterator<String> keys=vars.keySet().iterator();
while (keys.hasNext()) {
String key = (String) keys.next();
context.put(key, vars.get(key));
}
}
StringWriter w = new StringWriter();
if(td!=null)
{
Velocity.mergeTemplate(td.wholeTname(), "utf-8", context, w );
}else{//调用动态模板
try
{
Velocity.evaluate( context, w, "dynamic", tcontent );
}
catch( Exception pee )
{
log.error("", pee);
}
}
return w.toString();
}





}


package com.book.bussiness;

import java.io.Serializable;

import com.g3net.tool.StringUtils;

/**
* 业务功能模板相关的数据
*
*
*/
public class TempleteData implements Serializable{


/**
*
*/
private static final long serialVersionUID = 1L;
private int id;//模板id
private int cver;//模板内容的版本号,从1开始递增编号
private int funid;
private int pver;
private int cs;
private int platformid;
public int getPlatformid() {
return platformid;
}

public void setPlatformid(int platformid) {
this.platformid = platformid;
}

private String tname;
private String tcontent;
private String classname;
private String funname;
private String modifytime;
private int isspecial;


public int getIsspecial() {
return isspecial;
}

public void setIsspecial(int isspecial) {
this.isspecial = isspecial;
}

public int getFunid() {
return funid;
}

public void setFunid(int funid) {
this.funid = funid;
}

public int getPver() {
return pver;
}

public void setPver(int pver) {
this.pver = pver;
}

public int getCs() {
return cs;
}

public void setCs(int cs) {
this.cs = cs;
}

/**
* 为了保证模板名的全局唯一性,需要添加样式、协议版本等信息
* @return
*/
public String getTname() {
return tname;
}

/**
* 返回完整版的模板名
* @return
*/
public String wholeTname(){
StringBuilder sb=new StringBuilder();
sb.append(platformid);sb.append("_");
sb.append(cs);sb.append("_");
sb.append(pver);sb.append("_");
sb.append(funid);
if(StringUtils.hasText(tname))
{
sb.append("_");
sb.append(tname);
}
return sb.toString();

}
public void setTname(String tname) {
this.tname = tname;
}

public String getTcontent() {
return tcontent;
}

public void setTcontent(String tcontent) {
this.tcontent = tcontent;
}

public String getClassname() {
return classname;
}

public void setClassname(String classname) {
this.classname = classname;
}

public String getFunname() {
return funname;
}

public void setFunname(String funname) {
this.funname = funname;
}

public String getModifytime() {
return modifytime;
}

public void setModifytime(String modifytime) {
this.modifytime = modifytime;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getCver() {
return cver;
}

public void setCver(int cver) {
this.cver = cver;
}

@Override
public String toString() {
// TODO Auto-generated method stub
return "[funid="+funid+" pver="+pver+" cs="+cs+" tname="+tname+" tcontent="+tcontent+" classname="+classname
+" funname="+funname+" modifytime="+modifytime+" ]";
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值