欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JAVA velocity模板引擎使用實(shí)例

 更新時(shí)間:2014年04月26日 13:34:25   作者:  
這篇文章主要介紹了JAVA velocity模板引擎使用實(shí)例,需要的朋友可以參考下

velocity使用1.7版本。 在win7下使用intelliJ IDEA建立一基于tomcat的web app項(xiàng)目,命名為todo_web,設(shè)置path為/todo,導(dǎo)入velocity相關(guān)jar包。只導(dǎo)入velocity-1.7.jar這個(gè)包可能會(huì)報(bào)錯(cuò),根據(jù)提示再導(dǎo)入velocity自帶的其他包。 項(xiàng)目結(jié)構(gòu)如下:



測(cè)試Tomcat

index.jsp內(nèi)容如下:

復(fù)制代碼 代碼如下:

<%-- Created by IntelliJ IDEA. --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title></title>
  </head>
  <body>
           <%
               out.print("hi,todo");
           %>
  </body>
</html>

HelloWorld.java內(nèi)容如下:
復(fù)制代碼 代碼如下:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;   
public class HelloWorld extends HttpServlet {
    /**
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hi!</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Hello World!!!</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

在web.xml中加入以下內(nèi)容:
復(fù)制代碼 代碼如下:

<servlet>
    <servlet-name>hi</servlet-name>
    <servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>hi</servlet-name>
    <url-pattern>/hi</url-pattern>
</servlet-mapping>

運(yùn)行項(xiàng)目,在http://localhost:8080/todo和http://localhost:8080/todo/hi中可以看到效果。


使用velocity

下面開始使用velocity模板引擎,在src下建立目錄templates,在templates目錄下建立文件test.vm,內(nèi)容如下:

復(fù)制代碼 代碼如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
#set( $this = "Velocity")
$this is great!  <br/>
$name  <br/>
hi  , i am letian
<h1>你好</h1>
</body>
</html>

在src目錄下新建java文件MyVelocity01.java:
復(fù)制代碼 代碼如下:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.VelocityContext;
import java.util.Properties;
public class MyVelocity01 extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Properties properties=new Properties();
        properties.setProperty("resource.loader", "class");
        properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        //properties.setProperty("input.encoding", "UTF-8");
        //properties.setProperty("output.encoding", "UTF-8");
        properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
        properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
        properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
        VelocityEngine velocityEngine = new VelocityEngine(properties);
        VelocityContext context=new VelocityContext();
        context.put("name", "test");
        StringWriter sw = new StringWriter();
        velocityEngine.mergeTemplate("templates/test.vm", "utf-8", context, sw);
        //velocityEngine.mergeTemplate("templates/test.vm", "utf-8", context, sw);      //這樣就會(huì)出現(xiàn)兩次
        out.println(sw.toString());
    }
}

配置web.xml:

復(fù)制代碼 代碼如下:

<!--MyVelocity-->
<servlet>
    <servlet-name>ve</servlet-name>
    <servlet-class>MyVelocity01</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ve</servlet-name>
    <url-pattern>/ve</url-pattern>
</servlet-mapping>

重新部署,瀏覽器訪問http://localhost:8080/todo/ve可以看到效果。

簡(jiǎn)單介紹velocity

velocity是一個(gè)基于java的模板引擎,有三種文件加載模板方式: 1、從文件路徑加載 2、從類路徑(MyVelocity01.java使用該方法) 3、從jar文件加載 開始接觸velocity時(shí)可能會(huì)在加載模板上遇到問題。

如何向模板文件傳遞變量: 模板本身可以定義變量,例如在test.vm中定義了變量$this,java代碼也可以給模板傳遞變量,例如test.vm中的變量$name便是VelocityContext實(shí)例傳遞過去的。同時(shí)velocity也支持迭代對(duì)象,例如: 我們?cè)贛yVelocity01.java中導(dǎo)入java.util.Vector,將代碼:

復(fù)制代碼 代碼如下:

context.put("name", "test");

改為:
復(fù)制代碼 代碼如下:

Vector v = new Vector(); 
v.addElement("Harry"); 
v.addElement("John"); 
String[] names = {"Harry", "John"};
context.put("names1", v);
context.put("names2", names);

將test.vm內(nèi)容改為:
復(fù)制代碼 代碼如下:

<h1>hello</h1>
#foreach($name in $names1)
    $name   <br/>
#end
#foreach($name in $names2)
    $name   <br/>
#end

velocity還支持map容器,支持使用#include("")引入靜態(tài)模板,#parse("模板名")引入動(dòng)態(tài)模板。

如果想不開要用java MVC寫網(wǎng)站的話,使用servlet + velocity是一個(gè)小巧靈活的選擇。

相關(guān)文章

最新評(píng)論