velocity+struts2应用

模板:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
Hello $customer!
</body>
</html>

java引擎:
@Action(value = "/velocity",results={@Result(name="aaa", location="http://localhost:8080/Epai1/webpage/main/page35.html", type="redirect")})
public String charge1() throws Exception{
/**
* rem: 2.通过Velocity得到导出文件数据
*/
//获取模板基本路径
String basePath = this.getRequest().getSession().getServletContext().getRealPath("");
System.out.println("basePath:"+basePath);
Properties p = new Properties();
p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, basePath);
//初始化模板
Velocity.init(p); //VelocityEngine velocityEngine = new VelocityEngine(); velocityEngine.init(p);
//模板相对路径,相对于basePath而言。
String velocityTemplate = "\\webpage\\main\\page34.html";
Template template = null;
try{
//获取模板
template = Velocity.getTemplate(velocityTemplate,"GBK");
}catch(Exception e){
e.printStackTrace();
}

//定义Velocity上下文
VelocityContext context = new VelocityContext();
//设置变量值
context.put("customer","xgt111" );
/**
* 下面一段得到html是模板内容。
*/
// StringWriter writer = new StringWriter();
// template.merge(context, writer);
/* show the World */
// String html = writer.toString();
// System.out.println(html);//模板类容
/**
* 下面一段是将模板内容写入文件page35.html中去。
*/
// File file = new File("F:\\apache-tomcat-6.0.18\\webapps\\Epai1\\webpage\\main\\page35.html");
// FileWriter fw = new FileWriter(file);//new FileWriter(file,true)加上true后会在以后内容后面接着写,否则覆盖写
// template.merge(context, fw);//将模板内容写入文件中去
// fw.flush();
// fw.close();


/**
* 下面一段是将模板显示内容当道word中去下载。//这里得到的word里面和网页显示内容一致
*/
HttpServletResponse response = this.getResponse();
//注意中文转码
String filename = new String("模板下载".getBytes("GB2312"),"ISO-8859-1");
response.reset();
response.setContentType("application/vnd.ms-word"); //application/vnd.ms-excel application/msword charset=utf-8
//response.setCharacterEncoding("GBK");
response.setHeader("Content-disposition", "attachment; filename="+filename+".doc"); // inline

VelocityWriter vw = null;
Writer writer = response.getWriter();
vw = new VelocityWriter(writer, 4 * 1024, true);
template.merge(context, writer);
vw.flush();
vw.recycle(null);
vw = null;

// this.write("{success:true}");
return null;
}

需要的jar包:
[img]http://dl.iteye.com/upload/attachment/0071/6608/9dae00d9-31a4-3515-a74b-dac48626ea49.gif[/img]


方法调用:
一、
1、模板内容:
Hello $customer
2、java引擎:
context.put("customer",”姓名” );
3、显示内容:
Hello 姓名
二、
1、模板内容:
Hello $customer.name
2、java引擎:
Map.put(“name”,”姓名”);
context.put("customer",map);
3、显示内容:
Hello 姓名

三、
1、模板内容:
#foreach( $num in $nums )
第$num个数字是:$num<br>
#end
2、java引擎:
int[] nums = {1,2,3,4,5,6,7,8,9};
context.put("nums", nums);
3、显示内容:
第1个数字是:1
第2个数字是:2
第3个数字是:3
第4个数字是:4
第5个数字是:5
第6个数字是:6
第7个数字是:7
第8个数字是:8
第9个数字是:9
四、
1、模板内容:
#foreach( $num in $nums )
#if ( $model.check( $num ))
第$num个数字是:$num<br>
#end
#end
2、java引擎:
Map map = new HashMap();
int[] nums = {1,2,3,4,5,6,7,8,9};
context.put("nums", nums);
Model model = new Model();//model中有方法check,返回boolean
model.setName("name");
context.put("model", model);
3、显示内容:
第2个数字是:2
第4个数字是:4
第6个数字是:6
第8个数字是:8
//Check中能被2整除的数返回true

五、
1、模板内容:
#foreach( $num in $nums )
#if ( $model.check( $num ))
第$num个数字是:$num ; 两倍后是:$model.getNum( $num )<br>
#end
#end
2、java引擎:
Map map = new HashMap();
int[] nums = {1,2,3,4,5,6,7,8,9};
context.put("nums", nums);
Model model = new Model();//model中有方法check,返回boolean
model.setName("name");
context.put("model", model);
3、显示内容:
第2个数字是:2 ; 两倍后是:4
第4个数字是:4 ; 两倍后是:8
第6个数字是:6 ; 两倍后是:12
第8个数字是:8 ; 两倍后是:16
//Model中的方法getNum返回int数,其值为传入数值*2


一些需要注意的地方:
parse
例如:如果dofoo.vm包含如下行:
Count down.
#set ( $count = 8 )
#parse ( “parsefoo.vm” )
All done with dofoo.vm!
那么在parsefoo.vm模板中,你可以包含如下VTL:
$count
#set ( $count = $count – 1 )//注意:减号-左右必须空开,不然报错
#if ( $count > 0 )
#parse( “parsefoo.vm” )
#else
All done with parsefoo.vm!
#end
的显示结果为:
Count down.
8
7
6
5
4
3
2
1
0
All done with parsefoo.vm!
All done with dofoo.vm!


特别注意:
模板要保存为gbk或者utf-8格式,不然会导致中文乱码。

模板中reference的正是格式如下:
${mudSlinger} 变量
${customer.Address} 属性
${purchase.getTotal()} 方法
$!{mudSlinger}如果在前面加个!号,则表示当变量在加载时没有获得初始值是,不会将$!{mudSlinger}这个字样显示出来

Stop
#stop script element允许模板设计者停止执行模板引擎并返回。把它应用于debug是很有帮助的。
#stop

宏Velocimacro,一个参数是color另一个参数是array:
#macro ( tablerows $color $somelist )
#foreach ( $something in $somelist )
<tr><td bgcolor=$color>$something</td</tr>
#end
#end
调用#tablerows Velocimacro:
#set ( $greatlakes = [ “Superior”, “Michigan”, “Huron”, “Erie”, “Ontario” ] )
#set ( $color = “blue” )
<table>
#tablerows( $color $greatlakes )
</table>
经过以上的调用将产生如下的显示结果:
<table>
<tr><td bgcolor=” blue”> Superior </td></tr>
<tr><td bgcolor=” blue”> Michigan </td></tr>
<tr><td bgcolor=” blue”> Huron </td></tr>
<tr><td bgcolor=” blue”> Erie </td></tr>
<tr><td bgcolor=” blue”> Ontario </td></tr>
</table>


最后:在Velocity内使用数学计算公式时,只能使用像-n,-2,-1,0,1,2,n这样的整数,而不能使用其它类型数据。当一个非整型的对象被使用时它将被logged并且将以null作为输出结果。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值