我是条汉子 发表于 2015-8-7 13:25:20

tomcat下部署Velocity模板(转贴)

http://cllovelf.blog.chinajavaworld.com/images/weather/hn2_ptcl.gifhttp://cllovelf.blog.chinajavaworld.com/images/weather/hn2_t_ptcl.gif http://cllovelf.blog.chinajavaworld.com/images/level3.gif




http://cllovelf.blog.chinajavaworld.com/servlet/AttachmentServlet?attachImage=true&contentType=application%2Foctet-stream&attachment=1242


velocity-1.5.jar(382.9 K)


http://cllovelf.blog.chinajavaworld.com/servlet/AttachmentServlet?attachImage=true&contentType=application%2Foctet-stream&attachment=1243


velocity-dep-1.5.jar(682.2 K)





切换到幻灯片模式
首先创建一个vtl项目,在项目下生成一个servlet
testVtl.java:



1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66




package com.cl.vtl;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
/**
* @version 1.0
* @author
*/
public class testVtl extends HttpServlet {
/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
Properties props = new Properties();
props.setProperty(Velocity.INPUT_ENCODING, "GBK");
props.setProperty(Velocity.OUTPUT_ENCODING, "GBK");
props.setProperty(Velocity.RESOURCE_LOADER, "file");
props.setProperty("file.resource.loader.description",
" Velocity File Resource Loader");
props.setProperty("file.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.FileResourceLoader");
props.setProperty("file.resource.loader.path", "D:/Program Files/Apache Software Foundation/Tomcat 5.0/webapps/vtl");
props.setProperty("file.resource.loader.cache", "true");
props.setProperty("file.resource.loader.modificationCheckInterval", "1");
props.setProperty("directive.foreach.counter.name", "velocityCount");
props.setProperty("directive.foreach.counter.initial.value", "1");
Velocity.init(props);//初始化模板设置
//Velocity.init("velocity.properties");
Template template = Velocity.getTemplate("/tm/index.vm", "GBK");//调用模板文件
VelocityContext context = new VelocityContext();//建立上下文链接
context.put("s", new String [] { "redwood", "maple", "oak", "pine" });
BufferedWriter writer = writer = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(),"GBK"));
if ( context != null)
template.merge(context, writer);//输出
writer.flush();
writer.close();
}catch(Exception ex){ex.printStackTrace();}
}
/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
this.doGet(req,resp) ;
}
}





web.xml:



1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37







Welcome to Tomcat

Welcome to Tomcat




org.apache.jsp.index_jsp
org.apache.jsp.index_jsp


org.apache.jsp.index_jsp
/index.jsp


testVtl
com.cl.vtl.testVtl


testVtl
/index








/*在D:/Program Files/Apache Software Foundation/Tomcat 5.0/webapps/vtl下建立tm文件夹
tm文件夹下存放模板文件*/
建立模板文件index.vm
index.vm:



1

2

3

4

5

6

7





#foreach($n in $s)

$n is a big tree!

#end






**注:将附件的两个jar包放到项目的lib文件夹下
页: [1]
查看完整版本: tomcat下部署Velocity模板(转贴)