當前位置: 首頁>>代碼示例>>Java>>正文


Java Template.setEncoding方法代碼示例

本文整理匯總了Java中freemarker.template.Template.setEncoding方法的典型用法代碼示例。如果您正苦於以下問題:Java Template.setEncoding方法的具體用法?Java Template.setEncoding怎麽用?Java Template.setEncoding使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在freemarker.template.Template的用法示例。


在下文中一共展示了Template.setEncoding方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: freeMarkerRender

import freemarker.template.Template; //導入方法依賴的package包/類
/**
 * freemarker渲染html
 */
public static String freeMarkerRender(Map<String, Object> data, String htmlTmp) {
	Writer out = new StringWriter();
	try {
		// 獲取模板,並設置編碼方式
		Template template = freemarkerCfg.getTemplate(htmlTmp);
		template.setEncoding("UTF-8");
		// 合並數據模型與模板
		template.process(data, out);
		// 將合並後的數據和模板寫入到流中,這裏使用的字符流
		out.flush();
		return out.toString();
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			out.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	return null;
}
 
開發者ID:Sunny0715,項目名稱:java_pdf_demo,代碼行數:26,代碼來源:JavaToPdfHtmlFreeMarker.java

示例2: freeMarkerRender

import freemarker.template.Template; //導入方法依賴的package包/類
/**
 * freemarker渲染html
 */
public static String freeMarkerRender(Map<String, Object> data, String htmlTmp) {
	Writer out = new StringWriter();
	try {
		// 獲取模板,並設置編碼方式
		Template template = freemarkerCfg.getTemplate(htmlTmp);
		template.setEncoding("UTF-8");
		// 合並數據模型與模板
		template.process(data, out); // 將合並後的數據和模板寫入到流中,這裏使用的字符流
		out.flush();
		return out.toString();
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			out.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	return null;
}
 
開發者ID:Sunny0715,項目名稱:java_pdf_demo,代碼行數:25,代碼來源:JavaToPdfHtmlFreeMarker.java

示例3: crateHTML

import freemarker.template.Template; //導入方法依賴的package包/類
/**
 * 生成靜態頁麵主方法
 * 
 * @param context
 *            ServletContext
 * @param data
 *            一個Map的數據結果集
 * @param templatePath
 *            ftl模版路徑
 * @param targetHtmlPath
 *            生成靜態頁麵的路徑
 */
public static void crateHTML(ServletContext context, Object data, String templatePath, String targetHtmlPath) {
    Configuration freemarkerCfg = new Configuration();
    // 加載模版
    freemarkerCfg.setServletContextForTemplateLoading(context, "/");
    freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8");
    try {
        // 指定模版路徑
        Template template = freemarkerCfg.getTemplate(templatePath, "UTF-8");
        template.setEncoding("UTF-8");
        // 靜態頁麵路徑
        String htmlPathString = context.getRealPath("/") + "/" + targetHtmlPath;
        File htmlFile = new File(htmlPathString);

        if (!htmlFile.getParentFile().exists()) {
            htmlFile.getParentFile().mkdirs();
        }
        htmlFile.createNewFile();
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8"));
        // 處理模版
        template.process(data, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        logger.error("error occured where generate Html file.", e);
    }
}
 
開發者ID:luckyyeah,項目名稱:YiDu-Novel,代碼行數:39,代碼來源:StaticUtils.java

示例4: getTemplate

import freemarker.template.Template; //導入方法依賴的package包/類
protected Template getTemplate(String tmplFileName)
		throws IOException {
	Configuration cfg = new Configuration();
	cfg.setEncoding(DEFAULT_LOCALE, DEFAULT_ENCODING);
	this.logger.debug("reading ftl " + tmplFileName
			+ " from class path " + freemarkerTemplateDefaultFilePath);
	cfg.setClassForTemplateLoading(this.getClass(),
			freemarkerTemplateDefaultFilePath);
	Template tmpl = cfg.getTemplate(tmplFileName);
	tmpl.setEncoding(DEFAULT_ENCODING);
	return tmpl;
}
 
開發者ID:WinRoad-NET,項目名稱:wrdocletbase,代碼行數:13,代碼來源:FreemarkerWriter.java

示例5: executeInternal

import freemarker.template.Template; //導入方法依賴的package包/類
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
    logger.debug("SendSubscribeMailJob start.");
    try {
        // 查找所有有更新的訂閱信息
        SubscribeSearchBean searchBean = new SubscribeSearchBean();
        // 獲取檢查期間
        Range<Date> dateRange = this.getDateRange();
        searchBean.setDateRange(dateRange);

        List<SubscribeDTO> subscribeList = this.subscribeService.findAllData(searchBean);

        // 整理訂閱信息
        Map<TUser, List<Integer>> subscribeMap = new HashMap<TUser, List<Integer>>();

        for (SubscribeDTO subscribeDTO : subscribeList) {
            TUser user = new TUser();
            BeanUtils.copyProperties(subscribeDTO, user);
            List<Integer> articlenoList = subscribeMap.get(user);
            if (!Utils.isDefined(articlenoList)) {
                articlenoList = new ArrayList<Integer>();
            }
            articlenoList.add(subscribeDTO.getArticleno());
            subscribeMap.put(user, articlenoList);
        }

        // 按用戶發郵件
        for (Map.Entry<TUser, List<Integer>> entry : subscribeMap.entrySet()) {
            ChapterSearchBean chapterSearchBean = new ChapterSearchBean();
            chapterSearchBean.setArticlenoList(entry.getValue());
            chapterSearchBean.setDateRange(dateRange);
            List<TChapter> chapterList = chapterService.find(chapterSearchBean);

            // 獲取推薦小說
            List<TArticle> randomRecommendArticleList = articleService.findRandomRecommendArticleList(10);
            // 創建
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("chapterList", chapterList);
            dataMap.put("user", entry.getKey());
            dataMap.put("randomRecommendArticleList", randomRecommendArticleList);

            // 讀取語言文件
            PropertiesConfiguration languageConf = new PropertiesConfiguration(Thread.currentThread()
                    .getContextClassLoader().getResource("language/package.properties"));

            dataMap.put("sitename", languageConf.getProperty(YiDuConfig.NAME));
            dataMap.put("siteuri", YiDuConstants.yiduConf.getString(YiDuConfig.URI));
            dataMap.put("mobileuri", YiDuConstants.yiduConf.getString(YiDuConfig.MOBILESITE_DOMIAN));

            Configuration freemarkerCfg = new Configuration();

            // 獲取郵件模版
            String classBasePath = SendSubscribeMailJob.class.getClassLoader().getResource("").getPath();
            String siteRootPath = classBasePath + "../../";
            String templatePath = siteRootPath
                    + MessageFormat.format(THEMES_DIR,
                            new Object[] { YiDuConstants.yiduConf.getString("themeName") });

            freemarkerCfg.setDirectoryForTemplateLoading(new File(templatePath));

            // 指定模版路徑
            Template template = freemarkerCfg.getTemplate(TEMPLATE_NAME, "UTF-8");
            template.setEncoding("UTF-8");

            Writer out = new StringWriter();
            template.process(dataMap, out);

            // 發送郵件
            MailUtils.sendMail(entry.getKey().getEmail(),
                    MessageFormat.format(MAIL_TITLE, new Object[] { languageConf.getProperty(YiDuConfig.NAME) }),
                    out.toString(), false);

        }

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    logger.debug("SendSubscribeMailJob normally end.");

}
 
開發者ID:luckyyeah,項目名稱:YiDu-Novel,代碼行數:81,代碼來源:SendSubscribeMailJob.java


注:本文中的freemarker.template.Template.setEncoding方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。