`
snoopy7713
  • 浏览: 1124466 次
  • 性别: Icon_minigender_2
  • 来自: 火星郊区
博客专栏
Group-logo
OSGi
浏览量:0
社区版块
存档分类
最新评论

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics