@BeforeMethod public void setUp() throws Exception { _uriInfo = new MockUriInfo(); _secMaster = new InMemorySecurityMaster(); _secLoader = new SecurityLoader() { @Override public Map<ExternalIdBundle, UniqueId> loadSecurity( Collection<ExternalIdBundle> identifiers) { throw new UnsupportedOperationException("load security not supported"); } @Override public SecurityMaster getSecurityMaster() { return _secMaster; } }; HistoricalTimeSeriesMaster htsMaster = new InMemoryHistoricalTimeSeriesMaster(); addSecurity(WebResourceTestUtils.getEquitySecurity()); addSecurity(WebResourceTestUtils.getBondFutureSecurity()); _webSecuritiesResource = new WebSecuritiesResource(_secMaster, _secLoader, htsMaster); MockServletContext sc = new MockServletContext("/web-engine", new FileSystemResourceLoader()); Configuration cfg = FreemarkerOutputter.createConfiguration(); cfg.setServletContextForTemplateLoading(sc, "WEB-INF/pages"); FreemarkerOutputter.init(sc, cfg); _webSecuritiesResource.setServletContext(sc); _webSecuritiesResource.setUriInfo(_uriInfo); }
/** * 获取servlet上下文件的Configuration * * @param pageFolder * @return */ public static Configuration getServletCfg(String pageFolder) { Configuration cfg = new Configuration(); cfg.setServletContextForTemplateLoading( ThreadContextHolder.getHttpRequest().getSession().getServletContext(), pageFolder); cfg.setObjectWrapper(new DefaultObjectWrapper()); return cfg; }
/** * 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(); } }
private Configuration getCfg() { if (cfg == null) { cfg = FreeMarkerUtil.getCfg(); } pathPrefix = pathPrefix == null ? "" : pathPrefix; if (pageFolder == null) { // 默认使用挂件所在文件夹 // System.out.println(" folder null use "+ this.clazz.getName() ); cfg.setClassForTemplateLoading(this.clazz, pathPrefix); } else { // System.out.println(" folder not null use "+ pageFolder); cfg.setServletContextForTemplateLoading( ThreadContextHolder.getHttpRequest().getSession().getServletContext(), pageFolder); } cfg.setObjectWrapper(new DefaultObjectWrapper()); cfg.setDefaultEncoding("UTF-8"); cfg.setLocale(java.util.Locale.CHINA); cfg.setEncoding(java.util.Locale.CHINA, "UTF-8"); return cfg; }
public void init() { cfg = new Configuration(); cfg.setServletContextForTemplateLoading(getServletContext(), "WEB-INF/templates"); }
public void init() throws ServletException { cfg = new Configuration(Configuration.VERSION_2_3_23); cfg.setServletContextForTemplateLoading(getServletContext(), "./"); cfg.setDefaultEncoding("UTF-8"); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); }