您现在的位置是:网站首页> 编程资料编程资料
JAVA velocity模板引擎使用实例_JSP编程_
                     2023-05-25
                207人已围观
                
                2023-05-25
                207人已围观
            
简介 JAVA velocity模板引擎使用实例_JSP编程_
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" %>
<%
out.print("hi,todo");
%>
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("");
out.println("");
out.println("
out.println("");
out.println("");
out.println("
Hello World!!!
");out.println("");
out.println("");
}
}
在web.xml中加入以下内容:
运行项目,在http://localhost:8080/todo和http://localhost:8080/todo/hi中可以看到效果。
使用velocity
下面开始使用velocity模板引擎,在src下建立目录templates,在templates目录下建立文件test.vm,内容如下:
#set( $this = "Velocity")
$this is great!
$name
hi , i am letian
你好
在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:
重新部署,浏览器访问http://localhost:8080/todo/ve可以看到效果。
简单介绍velocity
velocity是一个基于java的模板引擎,有三种文件加载模板方式: 1、从文件路径加载 2、从类路径(MyVelocity01.java使用该方法) 3、从jar文件加载 开始接触velocity时可能会在加载模板上遇到问题。
如何向模板文件传递变量: 模板本身可以定义变量,例如在test.vm中定义了变量$this,java代码也可以给模板传递变量,例如test.vm中的变量$name便是VelocityContext实例传递过去的。同时velocity也支持迭代对象,例如: 我们在MyVelocity01.java中导入java.util.Vector,将代码:
context.put("name", "test");
改为:
Vector v = new Vector();
v.addElement("Harry");
v.addElement("John");
String[] names = {"Harry", "John"};
context.put("names1", v);
context.put("names2", names);
将test.vm内容改为:
hello
#foreach($name in $names1)
$name
#end
#foreach($name in $names2)
$name
#end
velocity还支持map容器,支持使用#include("")引入静态模板,#parse("模板名")引入动态模板。
如果想不开要用java MVC写网站的话,使用servlet + velocity是一个小巧灵活的选择。
- 详解使用Mybatis-plus + velocity模板生成自定义的代码
- c#基于NVelocity实现代码生成
- SiteMesh如何结合Freemarker及velocity使用
- Vue中JS动画与Velocity.js的结合使用
- 如何解决SpringBoot2.x版本对Velocity模板不支持的方案
- SpringBoot与velocity的结合的示例代码
- 聊聊JS动画库 Velocity.js的使用
- springMVC+velocity实现仿Datatables局部刷新分页方法
- 详解velocity模板使javaWeb的html+js实现模块化
- Mybatis velocity脚本的使用教程详解(推荐)
- html文件中jquery与velocity变量中的$冲突的解决方法
- Java 如何使用Velocity引擎生成代码
相关内容
- JSP中用回车监听按钮事件兼容火狐 IE等主流浏览器_JSP编程_
- servlet中session简介和使用例子_JSP编程_
- jsp中使用javabean实例介绍_JSP编程_
- struts2中一个表单中提交多个请求的例子(多个提交按钮)_JSP编程_
- java操作mysql入门代码实例(含插入、更新和查询)_JSP编程_
- jsp跳转getRequestDispatcher()和sendRedirect()的区别_JSP编程_
- jsp和servlet操作mysql中文乱码问题的解决办法_JSP编程_
- jsp中调用java代码小结_JSP编程_
- 使用jsp调用javabean实现超简单网页计算器示例_JSP编程_
- Jsp中如何让图片在div中居中_JSP编程_
 
                                
                                                         
                                
                                                         
                                
                                                         
 
    