velocity使用1.7版本。 在win7下使用intelliJ IDEA建立一基于tomcat的web app项目,命名为todo_web,设置path为/todo,导入velocity相关jar包。只导入velocity-1.7.jar这个包可能会报错,根据提示再导入velocity自带的其他包。 项目结构如下:
测试Tomcat
index.jsp内容如下:
复制代码 代码如下:
<%-- 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内容如下:
复制代码 代码如下:
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中加入以下内容:
复制代码 代码如下:
<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>
运行项目,在:8080/todo和:8080/todo/hi中可以看到效果。
使用velocity
下面开始使用velocity模板引擎,在src下建立目录templates,在templates目录下建立文件test.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>
在src目录下新建java文件MyVelocity01.java:
复制代码 代码如下:
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); //这样就会出现两次
out.println(sw.toString());
}
}
配置web.xml:
复制代码 代码如下: