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

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

Velocity 模版引擎的使用

2019-11-11 07:04:46
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

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;/** * 生成門(mén)市聯(lián)系信息頁(yè)面 */public class CreateStoresShtml {	public static final String DEFAULT_AREA_TYPE = "總部前臺(tái)"; // 默認(rèn)顯示的區(qū)域類(lèi)型	public static Properties properties = null;	public static VelocityEngine velocityEngine = null;		/***	 * 一鍵生成 shtml 頁(yè)面	 * @param list(所有門(mén)市聯(lián)系方式集合)	 * @param vmPath(模版路徑)	 * @param vmName(模版名稱(chēng))	 * @param shtmlPath(生成頁(yè)面的路徑)	 * @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);									// 刪除原頁(yè)面				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è)文件(舊頁(yè)面)	 * @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)用方法

/*** 一鍵生成門(mén)店聯(lián)系頁(yè)面 */    public void createShtmlFnc() {        try {            // 查詢所有有效門(mén)市的聯(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"; // 底部 門(mén)市聯(lián)系信息頁(yè)面路徑            String footerVmName = "create_footer_stores.vm"; // 底部模版路徑            CreateStoresShtml.createShtmlFnc(list, vmPath, footerVmName, footerPath); // 生成頭部頁(yè)面            writeSuccess("操作成功");        } catch(Exception e) {            setExceptionMessage("一鍵生成門(mén)店聯(lián)系頁(yè)面時(shí)異常!", e);            writeError("系統(tǒng)異常");        }    }


上一篇:C#之MySql刪除

下一篇:n&amp;1判斷奇偶

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 陵川县| 阳江市| 孟津县| 广水市| 萨迦县| 华亭县| 沧州市| 五寨县| 长治县| 共和县| 高尔夫| 吉木乃县| 凤冈县| 电白县| 东丰县| 天柱县| 营口市| 玉溪市| 香港| 黄平县| 托克托县| 名山县| 惠东县| 宁海县| 襄城县| 泰宁县| 乌拉特中旗| 石家庄市| 吉木乃县| 梁河县| 余干县| 汪清县| 临海市| 德令哈市| 郧西县| 海原县| 德阳市| 万宁市| 江油市| 赞皇县| 元氏县|