velocity例子
来源:互联网 发布:mysql有什么用知乎 编辑:程序博客网 时间:2024/06/06 01:53
jsp页面:index。
servlet::Vlt.java
创建Html的java类:CreateHtml.java
生成的Html:test.html
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body><pre name="code" class="html"> This is my JSP page. <br> <a href="Vlt?method=create">test</a> <a href="html/test.html">vm</a> </body></html>
servlet::Vlt.java
package demo;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.velocity.VelocityContext;public class Vlt extends HttpServlet {/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String flag=request.getParameter("method");if("create".equals(flag)){//模板路径String realPath=request.getRealPath("/");String url="demo.vm";//生成静态页面路径String url2="E:/Demo/VelocityDemo/WebRoot/html/test.html";VelocityContext context=new VelocityContext(); context.put("name", "张三"); String[] hobbyArray={"吃饭","喝水","洗澡"}; context.put("hobby", "爱好"); context.put("hobbyArray", hobbyArray); /*context.put("title", "welcome to my page你好!");*/CreateHtml.createHtml(realPath,url, url2, context);response.sendRedirect("/index.jsp");}else{response.sendRedirect("/index.jsp");}}}
创建Html的java类:CreateHtml.java
package demo;import java.io.FileNotFoundException;import java.io.PrintWriter;import java.util.Properties;import org.apache.velocity.Template;import org.apache.velocity.VelocityContext;import org.apache.velocity.app.Velocity;import org.apache.velocity.exception.ParseErrorException;import org.apache.velocity.exception.ResourceNotFoundException;public class CreateHtml {public static void createHtml(String realPath,String url,String url2,VelocityContext context) { try { Properties properties = new Properties(); //设置加载的路径 properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH,realPath); Velocity.init(properties); String s=realPath+url; //初始化vm模板 Template template=Velocity.getTemplate(url,"UTF-8"); //生成html页面 PrintWriter pw=new PrintWriter(url2); template.merge(context, pw); //关闭流 pw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (ResourceNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParseErrorException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
Velocity模板:demo.vm
##宏定义 #macro( d $name)<a href="#">$name</a>#end $name$hobby:#foreach($hobby in $hobbyArray)${hobby}#end ##宏调用 #foreach($hobby in $hobbyArray) #d($hobby) #end #set ($i=0)##定义变量 #foreach($hobby in $hobbyArray) 序号:$i#set($i=$i+1) #end #*文件包含 如:#parse("/blog/top.html")或#include("/blog/top.html")#include( "one.gif","two.txt","three.htm" )parse与include的区别在于,若包含的文件中有Velocity脚本标签,将会进一步解析,而include将原样显示。*##parse("testInclude.vm")##条件语句#set($foo="deng")#if( $foo )<strong>找到${foo}!</strong>#else查找失败了!#end
生成的Html:test.html
张三爱好:吃饭喝水洗澡 <a href="#">吃饭</a> <a href="#">喝水</a> <a href="#">洗澡</a> 序号:0 序号:1 序号:2 say:hello world<strong>找到deng!</strong>
0 0
- velocity例子
- Velocity例子
- velocity例子
- velocity例子
- velocity例子
- velocity简单例子
- Velocity 例子介绍
- velocity 教程及例子
- Velocity Tools例子
- velocity简单例子
- velocity入门例子
- 运行velocity的官方例子
- 模板语言velocity项目例子
- 运行velocity的官方例子
- Velocity第一个应用例子
- 学习Velocity Tools的一个例子
- 关于模板语言Velocity的简单例子
- velocity第四个应用例子---遍历Map集合
- 南阳oj 士兵杀敌(二) 题目116 NYOJ 数据结构
- 优先队列用法
- ZOJ - 3124 Celebrity jeopardy
- HDU 3033 I love sneakers!(分组背包变形)
- 文章标题
- velocity例子
- 堆分配存储的串的表示和实现
- FindBugs规则整理(转载)
- 【Java】(1)Base64加密技术
- 简略Spark输出
- POJ 1416 Shredding Company(简单DFS)
- KafkaFlume-联合配置数---据源是Syslog
- NoSql——mongoDB(分片)
- 九度oj 1056