org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer Java Examples

The following examples show how to use org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MailAutoConfiguration.java    From cola-cloud with MIT License 7 votes vote down vote up
@Bean
@ConditionalOnBean(JavaMailSender.class)
public MailSender adiMailSender1(JavaMailSender sender, FreeMarkerConfigurer freeMarkerConfigurer) {
    if (mailProperties.getPreferIPv4Stack()) {
        System.setProperty("java.net.preferIPv4Stack", "true");
    }

    JavaMailSenderImpl js= (JavaMailSenderImpl) sender;
    Properties props = new Properties();
    props.put("mail.smtp.auth", true);
    props.put("mail.smtp.ssl.enable", true);
    props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.timeout", 25000);
    js.setJavaMailProperties(props);
    MailSender mailSender = new MailSender();
    mailSender.setJavaMailSender(js);
    mailSender.setFreeMarkerConfigurer(freeMarkerConfigurer);
    mailSender.setName(mailProperties.getName());
    mailSender.setFrom(mailProperties.getFrom());
    return mailSender;
}
 
Example #2
Source File: WebFtlsContextConfig.java    From onetwo with Apache License 2.0 6 votes vote down vote up
@Bean
	@ConditionalOnMissingBean({FreeMarkerConfig.class, FreeMarkerViewResolver.class})
	public FreeMarkerConfigurer freeMarkerConfigurer() {
		PluginFreeMarkerConfigurer configurer = new PluginFreeMarkerConfigurer();
		applyProperties(configurer);
		String[] paths = this.properties.getTemplateLoaderPath();
//		paths = ArrayUtils.add(paths, WEBFTLS_PATH);
		configurer.setTemplateLoaderPaths(paths);
		
		List<WithAnnotationBeanData<FreeMarkerViewTools>> tools = SpringUtils.getBeansWithAnnotation(applicationContext, FreeMarkerViewTools.class);
		tools.forEach(t->{
			String name = t.getAnnotation().value();
			if(StringUtils.isBlank(name)){
				name = t.getBean().getClass().getSimpleName();
			}
			configurer.setFreemarkerVariable(name, t.getBean());
			logger.info("registered FreeMarkerViewTools : {}", name);
		});
		return configurer;
	}
 
Example #3
Source File: WebMvcAutoConfiguration.java    From halo with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Configuring freemarker template file path.
 *
 * @return new FreeMarkerConfigurer
 */
@Bean
public FreeMarkerConfigurer freemarkerConfig(HaloProperties haloProperties) throws IOException, TemplateException {
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setTemplateLoaderPaths(FILE_PROTOCOL + haloProperties.getWorkDir() + "templates/", "classpath:/templates/");
    configurer.setDefaultEncoding("UTF-8");

    Properties properties = new Properties();
    properties.setProperty("auto_import", "/common/macro/common_macro.ftl as common,/common/macro/global_macro.ftl as global");

    configurer.setFreemarkerSettings(properties);

    // Predefine configuration
    freemarker.template.Configuration configuration = configurer.createConfiguration();

    configuration.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);

    if (haloProperties.isProductionEnv()) {
        configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    }

    // Set predefined freemarker configuration
    configurer.setConfiguration(configuration);

    return configurer;
}
 
Example #4
Source File: SmartGlobalErrorController.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	if (log.isInfoEnabled()) {
		log.info("Initializing smart global error controller ...");
	}

	try {
		FreeMarkerConfigurer fmcr = new FreeMarkerConfigurer();
		fmcr.setTemplateLoaderPath(config.getBasePath());
		Properties settings = new Properties();
		settings.setProperty("template_update_delay", "0");
		settings.setProperty("default_encoding", "UTF-8");
		settings.setProperty("number_format", "0.####");
		settings.setProperty("datetime_format", "yyyy-MM-dd HH:mm:ss");
		settings.setProperty("classic_compatible", "true");
		settings.setProperty("template_exception_handler", "ignore");
		fmcr.setFreemarkerSettings(settings);
		fmcr.afterPropertiesSet();

		// Initial errors template.
		Configuration fmc = fmcr.getConfiguration();
		if (!isErrorRedirectURI(config.getNotFountUriOrTpl())) {
			this.tpl404 = fmc.getTemplate(config.getNotFountUriOrTpl(), UTF_8.name());
			notNull(tpl404, "Default 404 view template must not be null");
		}
		if (!isErrorRedirectURI(config.getUnauthorizedUriOrTpl())) {
			this.tpl403 = fmc.getTemplate(config.getUnauthorizedUriOrTpl(), UTF_8.name());
			notNull(tpl403, "Default 403 view template must not be null");
		}
		if (!isErrorRedirectURI(config.getErrorUriOrTpl())) {
			this.tpl50x = fmc.getTemplate(config.getErrorUriOrTpl(), UTF_8.name());
			notNull(tpl50x, "Default 500 view template must not be null");
		}
	} catch (Exception e) {
		throw new IllegalStateException(e);
	}
}
 
Example #5
Source File: ViewResolverRegistryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	StaticWebApplicationContext context = new StaticWebApplicationContext();
	context.registerSingleton("freeMarkerConfigurer", FreeMarkerConfigurer.class);
	context.registerSingleton("velocityConfigurer", VelocityConfigurer.class);
	context.registerSingleton("tilesConfigurer", TilesConfigurer.class);
	context.registerSingleton("groovyMarkupConfigurer", GroovyMarkupConfigurer.class);
	context.registerSingleton("scriptTemplateConfigurer", ScriptTemplateConfigurer.class);
	this.registry = new ViewResolverRegistry();
	this.registry.setApplicationContext(context);
	this.registry.setContentNegotiationManager(new ContentNegotiationManager());
}
 
Example #6
Source File: ViewResolverRegistry.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Register a FreeMarker view resolver with an empty default view name
 * prefix and a default suffix of ".ftl".
 *
 * <p><strong>Note</strong> that you must also configure FreeMarker by adding a
 * {@link org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer} bean.
 */
public UrlBasedViewResolverRegistration freeMarker() {
	if (this.applicationContext != null && !hasBeanOfType(FreeMarkerConfigurer.class)) {
		throw new BeanInitializationException("In addition to a FreeMarker view resolver " +
				"there must also be a single FreeMarkerConfig bean in this web application context " +
				"(or its parent): FreeMarkerConfigurer is the usual implementation. " +
				"This bean may be given any name.");
	}
	FreeMarkerRegistration registration = new FreeMarkerRegistration();
	this.viewResolvers.add(registration.getViewResolver());
	return registration;
}
 
Example #7
Source File: ViewResolverRegistry.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Register a FreeMarker view resolver with an empty default view name
 * prefix and a default suffix of ".ftl".
 * <p><strong>Note</strong> that you must also configure FreeMarker by adding a
 * {@link org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer} bean.
 */
public UrlBasedViewResolverRegistration freeMarker() {
	if (!checkBeanOfType(FreeMarkerConfigurer.class)) {
		throw new BeanInitializationException("In addition to a FreeMarker view resolver " +
				"there must also be a single FreeMarkerConfig bean in this web application context " +
				"(or its parent): FreeMarkerConfigurer is the usual implementation. " +
				"This bean may be given any name.");
	}
	FreeMarkerRegistration registration = new FreeMarkerRegistration();
	this.viewResolvers.add(registration.getViewResolver());
	return registration;
}
 
Example #8
Source File: ViewResolverRegistry.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Register a FreeMarker view resolver with an empty default view name
 * prefix and a default suffix of ".ftl".
 * <p><strong>Note</strong> that you must also configure FreeMarker by adding a
 * {@link org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer} bean.
 */
public UrlBasedViewResolverRegistration freeMarker() {
	if (!checkBeanOfType(FreeMarkerConfigurer.class)) {
		throw new BeanInitializationException("In addition to a FreeMarker view resolver " +
				"there must also be a single FreeMarkerConfig bean in this web application context " +
				"(or its parent): FreeMarkerConfigurer is the usual implementation. " +
				"This bean may be given any name.");
	}
	FreeMarkerRegistration registration = new FreeMarkerRegistration();
	this.viewResolvers.add(registration.getViewResolver());
	return registration;
}
 
Example #9
Source File: StaticPageServiceImpl.java    From halo with GNU General Public License v3.0 5 votes vote down vote up
public StaticPageServiceImpl(PostService postService,
                             PostCategoryService postCategoryService,
                             PostTagService postTagService,
                             PostMetaService postMetaService,
                             SheetService sheetService,
                             CategoryService categoryService,
                             TagService tagService,
                             LinkService linkService,
                             PhotoService photoService,
                             JournalService journalService,
                             ThemeService themeService,
                             HaloProperties haloProperties,
                             OptionService optionService,
                             FreeMarkerConfigurer freeMarkerConfigurer,
                             StaticDeployHandlers staticDeployHandlers) throws IOException {
    this.postService = postService;
    this.postCategoryService = postCategoryService;
    this.postTagService = postTagService;
    this.postMetaService = postMetaService;
    this.sheetService = sheetService;
    this.categoryService = categoryService;
    this.tagService = tagService;
    this.linkService = linkService;
    this.photoService = photoService;
    this.journalService = journalService;
    this.themeService = themeService;
    this.haloProperties = haloProperties;
    this.optionService = optionService;
    this.freeMarkerConfigurer = freeMarkerConfigurer;
    this.staticDeployHandlers = staticDeployHandlers;

    pagesDir = Paths.get(haloProperties.getWorkDir(), PAGES_FOLDER);
    FileUtils.createIfAbsent(pagesDir);
    Files.createDirectories(Paths.get(STATIC_PAGE_PACK_DIR));
}
 
Example #10
Source File: ContentFeedController.java    From halo with GNU General Public License v3.0 5 votes vote down vote up
public ContentFeedController(PostService postService,
                             CategoryService categoryService,
                             PostCategoryService postCategoryService,
                             OptionService optionService,
                             FreeMarkerConfigurer freeMarker) {
    this.postService = postService;
    this.categoryService = categoryService;
    this.postCategoryService = postCategoryService;
    this.optionService = optionService;
    this.freeMarker = freeMarker;
}
 
Example #11
Source File: GlobalConfig.java    From pacbot with Apache License 2.0 5 votes vote down vote up
@Bean
public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
    FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
    factory.setTemplateLoaderPaths("classpath:templates", "src/main/resources/templates");
    factory.setDefaultEncoding("UTF-8");
    FreeMarkerConfigurer result = new FreeMarkerConfigurer();
    result.setConfiguration(factory.createConfiguration());
    return result;
}
 
Example #12
Source File: WebAppConfig.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** Configures Freemarker */
@Override
public FreeMarkerConfigurer freeMarkerConfigurer() {
  FreeMarkerConfigurer result = super.freeMarkerConfigurer();
  // Look up unknown templates in the FreemarkerTemplate repository
  result.setPostTemplateLoaders(new RepositoryTemplateLoader(dataService));
  return result;
}
 
Example #13
Source File: MolgenisWebAppConfig.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Configure freemarker. All freemarker templates should be on the classpath in a package called
 * 'freemarker'
 */
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
  FreeMarkerConfigurer result =
      new FreeMarkerConfigurer() {
        @Override
        protected void postProcessConfiguration(Configuration config) {
          config.setObjectWrapper(new MolgenisFreemarkerObjectWrapper(VERSION_2_3_23));
        }

        @Override
        protected void postProcessTemplateLoaders(List<TemplateLoader> templateLoaders) {
          templateLoaders.add(new ClassTemplateLoader(FreeMarkerConfigurer.class, ""));
        }
      };
  result.setPreferFileSystemAccess(false);
  result.setTemplateLoaderPath("classpath:/templates/");
  result.setDefaultEncoding("UTF-8");
  Properties freemarkerSettings = new Properties();
  freemarkerSettings.setProperty(Configuration.LOCALIZED_LOOKUP_KEY, Boolean.FALSE.toString());
  freemarkerSettings.setProperty(Configuration.NUMBER_FORMAT_KEY, "computer");
  result.setFreemarkerSettings(freemarkerSettings);
  Map<String, Object> freemarkerVariables = Maps.newHashMap();
  freemarkerVariables.put("hasPermission", new HasPermissionDirective(permissionService));
  freemarkerVariables.put("notHasPermission", new NotHasPermissionDirective(permissionService));
  addFreemarkerVariables(freemarkerVariables);

  result.setFreemarkerVariables(freemarkerVariables);

  return result;
}
 
Example #14
Source File: ViewResolverRegistryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() {
	StaticWebApplicationContext context = new StaticWebApplicationContext();
	context.registerSingleton("freeMarkerConfigurer", FreeMarkerConfigurer.class);
	context.registerSingleton("tilesConfigurer", TilesConfigurer.class);
	context.registerSingleton("groovyMarkupConfigurer", GroovyMarkupConfigurer.class);
	context.registerSingleton("scriptTemplateConfigurer", ScriptTemplateConfigurer.class);

	this.registry = new ViewResolverRegistry(new ContentNegotiationManager(), context);
}
 
Example #15
Source File: ViewResolverRegistry.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Register a FreeMarker view resolver with an empty default view name
 * prefix and a default suffix of ".ftl".
 * <p><strong>Note</strong> that you must also configure FreeMarker by adding a
 * {@link org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer} bean.
 */
public UrlBasedViewResolverRegistration freeMarker() {
	if (!checkBeanOfType(FreeMarkerConfigurer.class)) {
		throw new BeanInitializationException("In addition to a FreeMarker view resolver " +
				"there must also be a single FreeMarkerConfig bean in this web application context " +
				"(or its parent): FreeMarkerConfigurer is the usual implementation. " +
				"This bean may be given any name.");
	}
	FreeMarkerRegistration registration = new FreeMarkerRegistration();
	this.viewResolvers.add(registration.getViewResolver());
	return registration;
}
 
Example #16
Source File: ViewResolverRegistryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() {
	StaticWebApplicationContext context = new StaticWebApplicationContext();
	context.registerSingleton("freeMarkerConfigurer", FreeMarkerConfigurer.class);
	context.registerSingleton("tilesConfigurer", TilesConfigurer.class);
	context.registerSingleton("groovyMarkupConfigurer", GroovyMarkupConfigurer.class);
	context.registerSingleton("scriptTemplateConfigurer", ScriptTemplateConfigurer.class);

	this.registry = new ViewResolverRegistry(new ContentNegotiationManager(), context);
}
 
Example #17
Source File: SpringWebReactiveConfig.java    From Spring-5.0-Cookbook with MIT License 4 votes vote down vote up
@Bean(name = "freemarkerConfig")
public FreeMarkerConfigurer getFreemarkerConfig() {
    FreeMarkerConfigurer config = new FreeMarkerConfigurer();
   config.setTemplateLoaderPath("/WEB-INF/templates/");
    return config;
}
 
Example #18
Source File: StaticPageSvcImpl.java    From Lottery with GNU General Public License v2.0 4 votes vote down vote up
public void setFreeMarkerConfigurer(
		FreeMarkerConfigurer freeMarkerConfigurer) {
	this.conf = freeMarkerConfigurer.getConfiguration();
}
 
Example #19
Source File: EmailConfiguration.java    From tutorials with MIT License 4 votes vote down vote up
@Bean 
public FreeMarkerConfigurer freemarkerConfig() { 
    FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); 
    freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/mail");
    return freeMarkerConfigurer; 
}
 
Example #20
Source File: FreemarkerConfiguration.java    From tutorials with MIT License 4 votes vote down vote up
@Bean 
public FreeMarkerConfigurer freemarkerConfig() { 
    FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); 
    freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/");
    return freeMarkerConfigurer; 
}
 
Example #21
Source File: MailSender.java    From voj with GNU General Public License v3.0 4 votes vote down vote up
/**
 * MailSender的构造函数.
 * @param freeMarkerConfigurer - FreeMarker的配置器
 * @param mailSender - JavaMailSender对象
 */
@Autowired
private MailSender(FreeMarkerConfigurer freeMarkerConfigurer, JavaMailSender mailSender) {
	this.freeMarkerConfigurer = freeMarkerConfigurer;
	this.mailSender = mailSender;
}
 
Example #22
Source File: SpringWebConfig.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
public FreeMarkerConfigurer freemarkerConfig() {
    FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
    freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/ftl/");
    return freeMarkerConfigurer;
}
 
Example #23
Source File: OghamFreemarkerConfiguration.java    From ogham with Apache License 2.0 4 votes vote down vote up
@Bean
@Qualifier("sms")
@ConditionalOnMissingBean(name = "smsFreemarkerConfiguration")
public freemarker.template.Configuration smsFreemarkerConfiguration(FreeMarkerConfigurer configurer) throws IOException, TemplateException {
	return configurer.createConfiguration();
}
 
Example #24
Source File: OghamFreemarkerConfiguration.java    From ogham with Apache License 2.0 4 votes vote down vote up
@Bean
@Qualifier("email")
@ConditionalOnMissingBean(name = "emailFreemarkerConfiguration")
public freemarker.template.Configuration emailFreemarkerConfiguration(FreeMarkerConfigurer configurer) throws IOException, TemplateException {
	return configurer.createConfiguration();
}
 
Example #25
Source File: WebConfig.java    From spring4-sandbox with Apache License 2.0 4 votes vote down vote up
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
	FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
	configurer.setTemplateLoaderPath("/WEB-INF/templates/");
	return configurer;
}
 
Example #26
Source File: CrafterFreeMarkerConfigurer.java    From engine with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void postProcessTemplateLoaders(List<TemplateLoader> templateLoaders) {
    // Overwrote to get rid of the log.info
    templateLoaders.add(new ClassTemplateLoader(FreeMarkerConfigurer.class, ""));
}
 
Example #27
Source File: ViewResolutionIntegrationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
	FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
	configurer.setTemplateLoaderPath("/WEB-INF/");
	return configurer;
}
 
Example #28
Source File: ViewResolutionIntegrationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
	FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
	configurer.setTemplateLoaderPath("/WEB-INF/");
	return configurer;
}
 
Example #29
Source File: SpringContextConfig.java    From Spring-5.0-Cookbook with MIT License 4 votes vote down vote up
@Bean(name = "freemarkerConfig")
public FreeMarkerConfigurer getFreemarkerConfig() {
    FreeMarkerConfigurer config = new FreeMarkerConfigurer();
   config.setTemplateLoaderPath("/WEB-INF/templates/");
    return config;
}
 
Example #30
Source File: SpringContextConfig.java    From Spring-5.0-Cookbook with MIT License 4 votes vote down vote up
@Bean(name = "freemarkerConfig")
public FreeMarkerConfigurer getFreemarkerConfig() {
    FreeMarkerConfigurer config = new FreeMarkerConfigurer();
   config.setTemplateLoaderPath("/WEB-INF/templates/");
    return config;
}