Velocity is a Java-based template engine. It permits anyone to use a simple yet powerful template language to reference objects defined in Java code.
When Velocity is used for web development, Web designers can work in parallel with Java programmers to develop web sites according to the Model-View-Controller (MVC) model, meaning that web page designers can focus solely on creating a site that looks good, and programmers can focus solely on writing top-notch code. Velocity separates Java code from the web pages, making the web site more maintainable over its lifespan and providing a viable alternative to Java Server Pages (JSPs) or PHP.
以上內(nèi)容摘自velocity的官方首頁(yè)。
以下通過(guò)示例來(lái)說(shuō)明velocity的使用。
在http://velocity.apache.org/download.cgi中下載velocity-1.7、velocity-tools-2.0。
參考本手冊(cè)“從JSP開(kāi)始”所述,創(chuàng)建項(xiàng)目Project_0006_Velocity
,導(dǎo)入相關(guān)的jar,編寫(xiě)代碼。
項(xiàng)目結(jié)構(gòu)如下:
對(duì)于新增的jar,放到/WEB-INF/lib目錄即可。但當(dāng)多個(gè)webApp要使用時(shí),放入CLASSPATH或Servlet容器(如Tomcat)的頂層lib是最好的選擇.
web.xml(在這一節(jié),該文件可以忽略):
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
hello.vm:
<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>
HelloServlet.java:
package me.letiantian.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Properties;
import java.io.StringWriter;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.VelocityContext;
@WebServlet(name = "HelloServlet", urlPatterns = {"/hello"})
public class HelloServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Properties properties=new Properties();
properties.setProperty("resource.loader", "webapp");
properties.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.servlet.WebappLoader");
properties.setProperty("webapp.resource.loader.path", "/WEB-INF/template");
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);
velocityEngine.setApplicationAttribute("javax.servlet.ServletContext", request.getServletContext());
VelocityContext context=new VelocityContext();
context.put("name", "user01");
StringWriter sw = new StringWriter();
velocityEngine.mergeTemplate("hello.vm", "utf-8", context, sw);
// velocityEngine.mergeTemplate("hello.vm", "utf-8", context, sw); //如果這行不注釋,hello.vm的內(nèi)容會(huì)出現(xiàn)兩次
out.println(sw.toString());
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
運(yùn)行項(xiàng)目,用瀏覽器訪問(wèn)http://127.0.0.1:8084/Project_0006_Velocity/hello
:
在WEB-INF目錄下創(chuàng)建velocity.properties
文件,其內(nèi)容如下:
resource.loader=webapp
webapp.resource.loader.class=org.apache.velocity.tools.view.servlet.WebappLoader
webapp.resource.loader.path=/WEB-INF/template/
input.encoding=utf-8
output.encoding=utf-8
修改HelloServlet.java:
package me.letiantian.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Properties;
import java.io.StringWriter;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.VelocityContext;
@WebServlet(name = "HelloServlet", urlPatterns = {"/hello"})
public class HelloServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Properties properties=new Properties();
properties.load(getServletContext().getResourceAsStream("/WEB-INF/velocity.properties"));
VelocityEngine velocityEngine = new VelocityEngine(properties);
velocityEngine.setApplicationAttribute("javax.servlet.ServletContext", request.getServletContext());
VelocityContext context=new VelocityContext();
context.put("name", "user01");
StringWriter sw = new StringWriter();
velocityEngine.mergeTemplate("hello.vm", "utf-8", context, sw);
out.println(sw.toString());
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
更多建議: