貼上代碼,這是一個工具類,自己為了方便管理信息頁面
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;/** * 生成門市聯系信息頁面 */public class CreateStoresShtml { public static final String DEFAULT_AREA_TYPE = "總部前臺"; // 默認顯示的區域類型 public static Properties properties = null; public static VelocityEngine velocityEngine = null; /*** * 一鍵生成 shtml 頁面 * @param list(所有門市聯系方式集合) * @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(); // 初始化參數 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(); // 實例化一個VelocityEngine對象 } 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");//這里我們就可以設置編碼了 ps.print(writer.toString()); ps.flush(); ps.close(); fos.close(); } /** * 刪除單個文件(舊頁面) * @param sPath 被刪除文件的路徑 * @return 單個文件刪除成功返回true,否則返回false */ public static boolean deleteFile(String sPath) throws Exception{ boolean flag = false; File file = new File(sPath); // 路徑為文件且不為空則進行刪除 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=='總部前臺') <li> <h3>總部前臺</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>下面貼上調用方法/*** 一鍵生成門店聯系頁面 */ public void createShtmlFnc() { try { // 查詢所有有效門市的聯系方式 List < SigningStore > list = managerSigningStoreService.findAll(new HashMap < String, Object > ()); String webPath = getClass().getResource("/").getFile().toString(); String vmPath = webPath + "/vm"; // VM模版所在文件夾的絕對路徑 webPath = webPath.substring(0, webPath.length() - 16); // 項目的絕對路徑 String footerPath = webPath + "footer-stores.shtml"; // 底部 門市聯系信息頁面路徑 String footerVmName = "create_footer_stores.vm"; // 底部模版路徑 CreateStoresShtml.createShtmlFnc(list, vmPath, footerVmName, footerPath); // 生成頭部頁面 writeSuccess("操作成功"); } catch(Exception e) { setExceptionMessage("一鍵生成門店聯系頁面時異常!", e); writeError("系統異常"); } }
新聞熱點
疑難解答