velocity初体验

VelocityServlet:

Java代码   收藏代码
  1. package  org.liufei.velocity;  
  2.   
  3. import  java.io.FileNotFoundException;  
  4. import  java.io.IOException;  
  5. import  java.util.Properties;  
  6.   
  7. import  javax.servlet.ServletConfig;  
  8. import  javax.servlet.http.HttpServletRequest;  
  9. import  javax.servlet.http.HttpServletResponse;  
  10.   
  11. import  org.apache.velocity.Template;  
  12. import  org.apache.velocity.app.Velocity;  
  13. import  org.apache.velocity.context.Context;  
  14. import  org.apache.velocity.servlet.VelocityServlet;  
  15.   
  16. @SuppressWarnings ( "deprecation" )  
  17. public   class  AddServletVelocity  extends  VelocityServlet {  
  18.   
  19.     private   static   final   long  serialVersionUID = -5343161778899944087L;  
  20.   
  21.     @Override   
  22.     protected  Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx)  throws  Exception {  
  23.         Template template = null  ;  
  24.         try {  
  25.             int  a =  11  ;  
  26.             int  b =  22  ;  
  27.             int  c = a + b ;  
  28.             ctx.put("a"new  Integer(a)) ;  
  29.             ctx.put("b"new  Integer(b)) ;  
  30.             ctx.put("c"new  Integer(c)) ;  
  31.             template = getTemplate("add.vm" ) ;  
  32.         }catch  (Exception e) {  
  33.             System.out.println(e.getLocalizedMessage());  
  34.         }  
  35.         return  template;  
  36.     }  
  37.   
  38.     @Override   
  39.     protected  Properties loadConfiguration(ServletConfig servletConfig)  throws  IOException, FileNotFoundException {  
  40.         Properties properties = new  Properties() ;  
  41.         String path = servletConfig.getServletContext().getRealPath("/" ) ;  
  42.         if (path ==  null ){  
  43.             System.out.println("Error !" );  
  44.             path = "/"  ;  
  45.         }  
  46.         properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path) ;  
  47.         properties.setProperty("runtime.log" , path +  "velocity.log" ) ;  
  48.         properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8" );  
  49.         properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8" );  
  50.         properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8" );    
  51.         return  properties ;  
  52.     }  
  53.   
  54. }  



vm模板:

Java代码   收藏代码
  1. <html>  
  2.   <head>  
  3.     <title>Velocity Test</title>  
  4.   </head>  
  5.   <body>  
  6.     <h1>Velcity Excemple</h1>  
  7.     <p>$a + $b = $c</p>  
  8.   </body>  
  9. </html>  



web.xml配置:

Java代码   收藏代码
  1. <?xml version= "1.0"  encoding= "UTF-8" ?>  
  2. <web-app version="2.5"  xmlns= "http://java.sun.com/xml/ns/javaee"   
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">   
  6.       
  7.     <servlet>  
  8.         <servlet-name>add</servlet-name>  
  9.         <servlet-class >org.liufei.velocity.AddServletVelocity</servlet- class >  
  10.     </servlet>  
  11.     <servlet-mapping>  
  12.         <servlet-name>add</servlet-name>  
  13.         <url-pattern>/add</url-pattern>  
  14.     </servlet-mapping>  
  15.       
  16.     <!--  
  17.     <welcome-file-list>  
  18.         <welcome-file>index.jsp</welcome-file>  
  19.     </welcome-file-list>  
  20.     -->  
  21. </web-app>  


运行结果:
Velcity Excemple

11 + 22 = 33

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值