velocity学习

1

#include只包含不render

#parse包含并render

 

2

定义

#macro(aaa)

....

#end

使用

#aaa()

3

#foreach($i in [0..3])

$obj.name

#end

此时obj.name被调用3次

 

4 application示例

代码

public class ApplicationTest {
	public static void main(String[] args) throws Exception {
		Velocity velocity = new Velocity();
		velocity.init();

		Template tp = velocity.getTemplate("helloapp.vm");
		VelocityContext context = new VelocityContext();
		context.put("name","severus");
		
		StringWriter writer = new StringWriter();
		tp.merge(context, writer);
		
		System.out.println(writer.toString());
	}
}

 模板

Hello world,$name!

 

5 web示例

servlet

@SuppressWarnings("deprecation")
public class MyVelocityServlet extends VelocityServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected Properties loadConfiguration(ServletConfig config)
			throws IOException, FileNotFoundException {
		Properties properties = new Properties();
		String path = config.getServletContext().getRealPath("/");
		properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
		properties.setProperty("runtime.log", path + "velocity.log");
		return properties;
	}

	@Override
	public Template handleRequest(HttpServletRequest request,
			HttpServletResponse response, Context ctx) throws Exception {
		Velocity.init();
		Template template = Velocity.getTemplate("helloweb.vm");
		ctx.put("name", "severus");
		return template;
	}
}

 模板

<html>
    <body >        
        Hello, $name 
    </body>
</html>

web.xml

  <servlet>
  	<servlet-name>myVelocityServlet</servlet-name>
  	<servlet-class>net.sx.velocity.web.MyVelocityServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>myVelocityServlet</servlet-name>
  	<url-pattern>/myVelocityServlet</url-pattern>
  </servlet-mapping>
 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值