日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用velocity

發布時間:2024/2/28 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用velocity 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用maven引入jar

<dependency><groupId>org.apache.velocity</groupId><artifactId>velocity</artifactId><version>1.7</version> </dependency> <dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-tools</artifactId><version>2.0</version><exclusions><exclusion><groupId>org.apache.struts</groupId><artifactId>struts-tiles</artifactId></exclusion><exclusion><groupId>org.apache.struts</groupId><artifactId>struts-taglib</artifactId></exclusion><exclusion><groupId>org.apache.struts</groupId><artifactId>struts-core</artifactId></exclusion><exclusion><groupId>sslext</groupId><artifactId>sslext</artifactId></exclusion><exclusion><groupId>oro</groupId><artifactId>oro</artifactId></exclusion></exclusions> </dependency><dependency><groupId>commons-configuration</groupId><artifactId>commons-configuration</artifactId><version>1.10</version></dependency><dependency><groupId>commons-net</groupId><artifactId>commons-net</artifactId><version>3.3</version></dependency> 創建TemplateUtil工具類

package com.os.core.util.web;import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine;import java.io.*; import java.util.HashMap; import java.util.Map; import java.util.Properties;public class TemplateUtil {public static void main(String[] args) {test();}/*** 測試方法*/public static void test() {try {String vmContent = "${dataMap.value1}中文<br/>${dataMap.value2}";String vmFileUrl = "";//CommonConstants.templateFileDir;//生成模板文件String fileName = createTemplateFile(vmContent, vmFileUrl);//模板上下文Map<String, Object> dataMap = new HashMap<String, Object>();dataMap.put("value1", "ssssssssssssssssssssss");dataMap.put("value2", "中文");VelocityContext context = new VelocityContext();context.put("dataMap", dataMap);//獲取模板生成器VelocityEngine engine = initEngine("");//CommonConstants.templateFileDir//html文件生成全路徑File file = new File("", "UTF-8");//CommonConstants.htmlFileDir+"/index.html"boolean createSuccess = createHtmlFile(engine, fileName, file, context);if (createSuccess) {System.out.println("文件生成完成");} else {System.out.println("文件生成失敗");}} catch (Exception e) {e.printStackTrace();}}/*** 生成靜態的HTML文件** @param engine 模板生成器* @param tempFileName 模板文件* @param file HTML生成的全路徑* @param context 模板上下文* @return true生成成功 false生成失敗* @throws Exception*/public static boolean createHtmlFile(VelocityEngine engine, String tempFileName, File file, VelocityContext context) {try {if (file.getParentFile().exists() == false) {file.getParentFile().mkdirs();}Template temp = engine.getTemplate(tempFileName, "UTF-8");FileOutputStream tempFos = new FileOutputStream(file);BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(tempFos, "UTF-8"));temp.setEncoding("UTF-8");temp.merge(context, writer);writer.close();tempFos.close();return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 初始化模板生成器** @return VelocityEngine* @throws Exception*/public static VelocityEngine initEngine(String genDir) throws Exception {VelocityEngine engine = new VelocityEngine();Properties properties = new Properties();properties.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, genDir);properties.setProperty(VelocityEngine.INPUT_ENCODING, "UTF-8");properties.setProperty(VelocityEngine.OUTPUT_ENCODING, "UTF-8");engine.init(properties);return engine;}/**** 創建模板文件** @param fileContext 模板內容* @param createDirUrl 模板保存目錄*/public static String createTemplateFile(String fileContext, String createDirUrl) {BufferedWriter bw = null;try {String fileDirUrl = createDirUrl + ".vm";File file = new File(fileDirUrl);if (file.getParentFile().exists() == false) {file.getParentFile().mkdirs();}bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));bw.write(fileContext);return fileDirUrl;} catch (Exception e) {System.out.println("生成模板文件出錯");e.printStackTrace();return null;} finally {if (bw != null) {try {bw.close();} catch (IOException e) {e.printStackTrace();}}}} }


總結

以上是生活随笔為你收集整理的使用velocity的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。