public static void init() { cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER); cfg.setCacheStorage(new freemarker.cache.MruCacheStorage(20, 250)); cfg.setNumberFormat("#"); cfg.setEncoding(Locale.getDefault(), "UTF-8"); cfg.setObjectWrapper(new DefaultObjectWrapper()); cfg.clearTemplateCache(); }
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ClassNotFoundException, SQLException, TemplateException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); Configuration c = new Configuration(); c.setDefaultEncoding("ISO-8859-1"); // setta il decoding della pagina in ingresso c.setOutputEncoding("ISO-8859-1"); // in output c.setNumberFormat(""); // per i numeri in virgola mobile e non c.setObjectWrapper( ObjectWrapper.BEANS_WRAPPER); // fa si che se voi passate al template un oggetto // viene visto come un associazione chiave valore da usare nel template c.setServletContextForTemplateLoading( getServletContext(), "templates"); // vatti a caricare questo template in base al contesto c c.setTemplateExceptionHandler( TemplateExceptionHandler .IGNORE_HANDLER); // handler di errori (default:statti zitto) cambia se vuoi debuggare // carica il template(il template non si reinderizza senza modellodati!) Template t = c.getTemplate("add_invitato.ftl.html"); Map<String, Object> data = new HashMap<String, Object>(); if (request.getSession(false) == null) { response.sendRedirect("index"); } if (request.getSession(false) != null) { data.put("login", 1); HttpSession s = request.getSession(false); int idtipo = (Integer) s.getAttribute("idtipo"); String username = (String) s.getAttribute("usersession"); int idutente = (Integer) s.getAttribute("idutente"); data.put("username", username); DataLayer dl = new DataLayerImpl(); UtenteDataLayer udl = dl.getUtenteDataLayer(dl.getC()); Utente utente = udl.readUtente(idutente); if (idtipo == 1) { data.put("tipo", "1"); // setta pannello admin } if (idtipo == 2) { data.put("tipo", "2"); // setta pannello organizzatore } String tmp = request.getParameter("id"); int idevento = Integer.parseInt(tmp); InvitatoDataLayer idl = dl.getInvitatoDataLayer(dl.getC()); List<Invitato> lista = idl.getRubricaAndInvitati(idutente, idevento); data.put("invitati", lista); data.put("idevento", idevento); } try { t.process(data, response.getWriter()); } finally { out.close(); } }
/** * 创建历史成交数据 * * @param buildingTab * @param estateTab */ private void createChartXmlFile( TransanctionStatManager transanctionStatManager, String xmlFilePath, String xmlAvgPriceFile, String xmlCountFile, List tenYears) { try { // 生成所有交易年数据文件 String filesp = File.separator; String templatePath = Constants.REALPATH + "WEB-INF" + filesp + "pages" + filesp + "maker" + filesp + "chart" + filesp; String templateAvgPriceFileName = "chart_avgprice_xml.ftl"; String templateCountFileName = "chart_count_xml.ftl"; Configuration cfg = new Configuration(); cfg.setNumberFormat("0.######"); cfg.setDirectoryForTemplateLoading(new File(templatePath)); cfg.setObjectWrapper(new DefaultObjectWrapper()); Map root = new HashMap(); root.put("tenYears", tenYears); // 生成十年交易平均价格数据文件 Template templateAvgPrice = cfg.getTemplate(templateAvgPriceFileName, "UTF-8"); Writer avgpriceOut = new OutputStreamWriter(new FileOutputStream(xmlAvgPriceFile), "UTF-8"); try { templateAvgPrice.process(root, avgpriceOut); } catch (TemplateException e) { e.printStackTrace(); } avgpriceOut.flush(); avgpriceOut.close(); // 生成十年交易总数数据文件 Template templateCount = cfg.getTemplate(templateCountFileName, "UTF-8"); Writer countOut = new OutputStreamWriter(new FileOutputStream(xmlCountFile), "UTF-8"); try { templateCount.process(root, countOut); } catch (TemplateException e) { e.printStackTrace(); } countOut.flush(); countOut.close(); } catch (Exception e) { e.printStackTrace(); } }
/** Initialize Freemarker Configuration */ public void initializeFreemarkerConfiguration() { configuration = new Configuration(); // make maps work in Freemarker when map key is not a String BeansWrapper beansWrapper = BeansWrapper.getDefaultInstance(); beansWrapper.setSimpleMapWrapper(true); configuration.setObjectWrapper(beansWrapper); // make sure that numbers are not formatted as 1,000 but as 1000 instead configuration.setNumberFormat("computer"); // TODO: make configurable? configuration.setDateFormat(ISO8601_FORMAT); configuration.setAutoFlush(true); }
private void build(Writer writer) throws Exception { InputStream in = null; try { Configuration config = new Configuration(); config.setNumberFormat("#"); config.setDefaultEncoding("utf-8"); Map<String, Object> con = new HashMap<String, Object>(); StringWriter stringWriter = new StringWriter(); GridView gridView = getGridView(true); new GridViewWriter(stringWriter).writeGridView(gridView); con.put("content", stringWriter.toString()); con.put("mainViewId", gridView.getViewId()); in = TemplateManager.class .getClassLoader() .getResourceAsStream( "com/gree/mobile/wf/taskinfo/template/grid/billDetailTemplate.ftl"); Template tpl = new Template("", new InputStreamReader(in, "utf-8"), config, "utf-8"); tpl.process(con, writer); } finally { IOUtils.closeQuietly(in); } }