`
gatusso52
  • 浏览: 109946 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

velocity学习

    博客分类:
  • j2ee
阅读更多

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>
 

 

 

 

 

 

 

分享到:
评论

相关推荐

    velocity学习笔记

    该文档为velocity学习笔记,包含一些常见问题

    velocity学习笔记与struts2整合

    velocity学习笔记与struts2整合 velocity学习笔记与struts2整合 velocity学习笔记与struts2整合

    velocity学习资料.zip

    Velocity学习资料,从网上找到的其中学习笔记由个人编写,希望大家借鉴,后续继续完善中。

    Velocity学习Web项目

    使用struts配置含有velocity的Web项目,并且成功跳转,并使用Velocity展示数据

    Velocity学习指南(中文版).rar

    Velocity用户指南旨在帮助页面设计者和内容提供者了解Velocity和其简单而又强大的脚本语言(Velocity Template Language (VTL))。本指南中有很多示例展示了用Velocity来讲动态内容嵌入到网站之中,但是所有的VTL ...

    velocity学习资料

    velocity学习资料 word文档,希望对大家的学习有帮助

    Velocity学习指南

    Velocity学习指南

    Velocity学习指南.doc

    Velocity 入门资料,适合 初学者学习,内容浅显易懂

    velocity学习指南

    velocity学习指南 Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。

    velocity中文

    velocity中文手册 velocity学习资料

    Velocity

    Velocity学习

    Velocity学习资料

    Velocity是前段的技术,它非常的灵活,跟JSP相差不多,但是动态的显示数据,值的我们学习!

    velocity中文版教程

    很不错的velocity学习资料 Velocity 是一个基于java 的模板引擎(template engine)。它允许任何人仅仅简单的使 用模板语言(template language)来引用由java 代码定义的对象。 当Velocity 应用于web 开发时,界面...

    velocity的学习资料

    关于velocity的学习资料,velocity教程和velocity用户手册

    velocity学习文档

    《velocity Java开发指南中文版》 《Velocity Web应用开发指南中文版》 《Velocity模板使用指南中文版》

Global site tag (gtag.js) - Google Analytics