protected Configuration createConfiguration(ServletContext servletContext) throws TemplateException { Configuration configuration = null; //Configuration configuration = super.createConfiguration(servletContext); ApplicationContext applicationContext = SpringHelper.getApplicationContext(servletContext); //configuration FreeMarkerConfigurer freeMarkerConfigurer = (FreeMarkerConfigurer) applicationContext .getBean(FREE_MARKER_CONFIGURER); configuration = freeMarkerConfigurer.getConfiguration(); //directive Map<String, TemplateDirectiveModel> beans = applicationContext .getBeansOfType(TemplateDirectiveModel.class); for (Map.Entry<String, TemplateDirectiveModel> entry : beans.entrySet()) { String key = entry.getKey(); TemplateDirectiveModel value = entry.getValue(); if (value instanceof TemplateDirectiveModel) { configuration.setSharedVariable(key, value); } } // return configuration; }
private void doConfigure() { Map<String, TemplateDirectiveModel> qlDirectives = new HashMap<String, TemplateDirectiveModel>(); qlDirectives.put(SetDirective.DIRECTIVE_NAME, new SetDirective()); qlDirectives.put(TrimDirective.DIRECTIVE_NAME, new TrimDirective()); qlDirectives.put(WhereDirective.DIRECTIVE_NAME, new WhereDirective()); try { freemarkerConfiguration.setSharedVariable(qlDirectivePrefix, qlDirectives); } catch (TemplateModelException ex) { throw new IllegalStateException("Failed to configurate freemarkerConfiguration.", ex); } }
private void verifyTemplateDirectiveRegistration() throws TemplateModelException { verify(freemarkerConfiguration).setSharedVariable(eq("Q"), templateDirectivesCaptor.capture()); Map<String, TemplateDirectiveModel> templateDirectives = templateDirectivesCaptor.getValue(); assertThat(templateDirectives.keySet(), hasSize(3)); assertThat(templateDirectives.keySet(), hasItems(SetDirective.DIRECTIVE_NAME, TrimDirective.DIRECTIVE_NAME, WhereDirective.DIRECTIVE_NAME)); }