模板语言velocity项目例子

来源:互联网 发布:sql 2005 32位 下载 编辑:程序博客网 时间:2024/06/05 06:50

模板语言很简单,这里只是提一下

index.vm文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Velocity Demo</title>    
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="keywords" content="velocity,模板">
 </head>
    <body >        
        #set($name1="first!")
        Hello, $name1  <br>
        Hello, $name2  <br/>
        Hello, $name3  <br/>
        Hello, $name3
        <hr>
        
        <table border='1' width='200' >
        <tr>
        <td>
            yy
        </td>
        </tr>
        #foreach ($iii in $theList)
        <tr>
          <td bgcolor="#eeeeee">$iii</td>
        </tr>
        #end
        </table>
    </body>
</html>

后台java类:

package com.velocity;

import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.Vector;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.servlet.VelocityServlet;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import org.apache.velocity.VelocityContext;

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

    protected Properties loadConfiguration(ServletConfig config)
            throws IOException, FileNotFoundException {

        VelocityEngine engine = new VelocityEngine();

        Properties p = new Properties();

        String path = config.getServletContext().getRealPath("/");

        if (path == null) {
            path = "/";
        }

        p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);

        p.setProperty("runtime.log", path + "velocity.log");
        

        try {
            engine.init(p); // 载入模板的路径path ,即上下文路径
        } catch (Exception e) {           
            e.printStackTrace();
        }

        return p;
    }

    public Template handleRequest(HttpServletRequest request,
            HttpServletResponse response, Context ctx) {
        
        try {
            request.setCharacterEncoding("UTF8");
        } catch (UnsupportedEncodingException e1) {
            
            e1.printStackTrace();
        }
        
        response.setCharacterEncoding("UTF8");
        Template template = null;
        Template template2 = null;

        try {
            /**
             * 主要代码
             */
            Velocity.init();
           
           // VelocityContext context = new VelocityContext();

            String p1 = "啊啊啊啊啊啊啊啊";
            String p2 = "啊啊啊啊啊啊啊啊";
            String p3 = "啊啊啊啊啊啊啊啊y";
            String p4 = "啊啊啊啊啊啊啊啊";
            Vector<String> personList = new Vector<String>();
            personList.addElement(p1);
            personList.addElement(p2);
            personList.addElement(p3);
            personList.addElement(p4);

            /**
             * 将模板数据name, list 放置到上下文环境 context 中去
             */
            ctx.put("name2", " 啊啊啊啊啊啊啊啊");
            ctx.put("name3", " 啊啊啊啊啊啊啊啊 ");
            ctx.put("theList", personList);

            template = Velocity.getTemplate("/index.vm");

        } catch (Exception e) {
            e.printStackTrace();
        }
        
        // 以下一段代码主要是获得模板的HTML内容 在后台显示
        try {
            template2 = Velocity.getTemplate("/index.vm");
            VelocityContext context = new VelocityContext();
            context.put("name2", "啊啊啊啊啊啊啊啊");
            StringWriter writer = new StringWriter();
            template2.merge(context, writer);
            System.out.println(writer.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return template;
    }
}

web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>velocity</display-name>
  <servlet>
  <servlet-name>velocityServlet</servlet-name>
  <servlet-class>com.velocity.MyVelocityServlet</servlet-class>
  <init-param>
      <param-name>org.apache.velocity.properties</param-name>
      <param-value>/WEB-INF/classes/velocity.properties</param-value>
    </init-param>
 </servlet>
 <servlet-mapping>
  <servlet-name>velocityServlet</servlet-name>
  <url-pattern>/*</url-pattern>
 </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.vm</welcome-file>
  </welcome-file-list>
</web-app>


原创粉丝点击