国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Velocity 模版引擎的使用

2019-11-11 06:12:05
字體:
供稿:網(wǎng)友

貼上代碼,這是一個(gè)工具類,自己為了方便管理信息頁面

import java.io.File;import java.io.FileOutputStream;import java.io.PRintStream;import java.io.StringWriter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.Properties;import org.apache.velocity.Template;import org.apache.velocity.VelocityContext;import org.apache.velocity.app.Velocity;import org.apache.velocity.app.VelocityEngine;import com.***.front.mode.SigningStore;import com.***.manager.model.TempStores;/** * 生成門市聯(lián)系信息頁面 */public class CreateStoresShtml {	public static final String DEFAULT_AREA_TYPE = "總部前臺(tái)"; // 默認(rèn)顯示的區(qū)域類型	public static Properties properties = null;	public static VelocityEngine velocityEngine = null;		/***	 * 一鍵生成 shtml 頁面	 * @param list(所有門市聯(lián)系方式集合)	 * @param vmPath(模版路徑)	 * @param vmName(模版名稱)	 * @param shtmlPath(生成頁面的路徑)	 * @throws Exception	 */	public static void createShtmlFnc(List<SigningStore> list,String vmPath,String vmName,String shtmlPath) throws Exception{				if(properties == null){						properties = new Properties();						// 初始化參數(shù)			properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH,vmPath);			properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");			properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");			properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");			velocityEngine = new VelocityEngine(); 				// 實(shí)例化一個(gè)VelocityEngine對(duì)象		}		VelocityContext context = new VelocityContext();		List<TempStores> tempStories =  initHeaderStoresData(list);		context.put("tempStories", tempStories);		deleteFile(shtmlPath);									// 刪除原頁面				velocityEngine.init(properties);	    Template t = velocityEngine.getTemplate(vmName); 			    context.put("tempStories", tempStories);	    StringWriter writer = new StringWriter();         	    t.merge(context,writer);      	    FileOutputStream fos = new FileOutputStream(new File(shtmlPath));	    PrintStream ps = new PrintStream(fos, true, "UTF-8");//這里我們就可以設(shè)置編碼了	    ps.print(writer.toString());	    ps.flush();	    ps.close();	    fos.close(); 	}		/** 	 * 刪除單個(gè)文件(舊頁面)	 * @param   sPath    被刪除文件的路徑 	 * @return 單個(gè)文件刪除成功返回true,否則返回false 	 */  	public static boolean deleteFile(String sPath) throws Exception{  	    boolean flag = false;  	    File file = new File(sPath);  	    // 路徑為文件且不為空則進(jìn)行刪除  	    if (file.isFile() && file.exists()) {  	        file.delete();  	        flag = true;  	    }  	    return flag;  	} 	}下面貼上模版存放路徑

/resources/vm/create_footer_stores.vm

具體信息參考下面模版

<table>    <tbody>        <tr>            <td width="25%" id="stores_cells_1">                <ul class="stores_a">#foreach( $store1 in $tempStories ) #if($store1.typeName=='總部前臺(tái)')                    <li>                        <h3>總部前臺(tái)</h3>                    </li>#foreach( $store in $store1.stores )                    <li>                        <p class="phone">電話:${store.landline}</p>                        <p class="address">${store.address}</p>                    </li>#end #end #end                    <li>                        <h3>分社</h3>                    </li>#foreach( $store1 in $tempStories ) #if($store1.typeName=='分社') #foreach( $store in $store1.stores )                    <li>                        <h4>${store.name}</h4>                        <p class="phone">電話:${store.landline}</p>                        <p class="address">${store.address}</p>                    </li>#end #end #end</ul>            </td>        </tr>    </tbody></table>

下面貼上調(diào)用方法

/*** 一鍵生成門店聯(lián)系頁面 */    public void createShtmlFnc() {        try {            // 查詢所有有效門市的聯(lián)系方式            List < SigningStore > list = managerSigningStoreService.findAll(new HashMap < String, Object > ());            String webPath = getClass().getResource("/").getFile().toString();            String vmPath = webPath + "/vm"; // VM模版所在文件夾的絕對(duì)路徑            webPath = webPath.substring(0, webPath.length() - 16); // 項(xiàng)目的絕對(duì)路徑            String footerPath = webPath + "footer-stores.shtml"; // 底部 門市聯(lián)系信息頁面路徑            String footerVmName = "create_footer_stores.vm"; // 底部模版路徑            CreateStoresShtml.createShtmlFnc(list, vmPath, footerVmName, footerPath); // 生成頭部頁面            writeSuccess("操作成功");        } catch(Exception e) {            setExceptionMessage("一鍵生成門店聯(lián)系頁面時(shí)異常!", e);            writeError("系統(tǒng)異常");        }    }


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 枣庄市| 长丰县| 武鸣县| 江津市| 泾阳县| 泗阳县| 沧州市| 高密市| 天峨县| 湟源县| 文化| 准格尔旗| 惠东县| 千阳县| 武宣县| 永清县| 东源县| 通州市| 荃湾区| 孟津县| 阜城县| 车险| 大关县| 宁津县| 册亨县| 旅游| 东港市| 潍坊市| 苍南县| 开化县| 星子县| 嵊泗县| 襄垣县| 新闻| 漯河市| 清水河县| 鄱阳县| 上思县| 崇信县| 正阳县| 靖边县|