spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Provide a callback for customising Freemarker variables

Open ohalo opened this issue 9 years ago • 35 comments

boot automatic configuration freemarker lack freemarkerVariables

ohalo avatar Apr 21 '17 03:04 ohalo

What do you mean by "freemarkerVariables"? Can you please show what you're currently having to configure that the auto-configuration should be able to configure for you?

wilkinsona avatar Apr 21 '17 07:04 wilkinsona

in freemarker automatic configuration , it lack of freemarkerVariables configuration

ohalo avatar Apr 25 '17 03:04 ohalo

Uploading image.png…

it lack of freemarkerVariables configuration

ohalo avatar Apr 25 '17 03:04 ohalo

  freemarker:
    settings:
      object_wrapper: com.control.back.halo.basic.utils.FreemarkerObjectWrapper
      datetime_format: yyyy-MM-dd HH:mm:ss
      date_format: yyyy-MM-dd
      time_format: HH:mm:ss

ohalo avatar Apr 25 '17 03:04 ohalo

@ConfigurationProperties(prefix = "spring.freemarker")
public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {   

it is no freemarkerVariables

ohalo avatar Apr 25 '17 03:04 ohalo

but FreeMarkerConfigurationFactory has freemarkerVariables,i need it

ohalo avatar Apr 25 '17 03:04 ohalo

I need a solution

ohalo avatar Apr 25 '17 03:04 ohalo

Ok, so you'd like to be able to set the freemarkerVariables property on either the auto-configured FreeMarkerConfigurationFactoryBean or on the auto-configured FreeMarkerConfigurer bean, correct? Both are subclasses of FreeMarkerConfigurationFactory.

What variables do you want to be able to set? Can you please provide some specific examples? Please note that values other than maps, lists and scalar values are not well-suited to being set via application.properties.

wilkinsona avatar Apr 25 '17 12:04 wilkinsona

#shiro

shiro:
  realm: com.control.back.halo.authorization.shiro.realm.UserRealm
  login-url: /login.html
  success-url: /index.html
  unauthorized-url: /
   #filter chian
  filterChainDefinitions:  
    "/login": anon
    "/login/**": anon
    "/logout": logout
    "/css/**": anon
    "/fonts/**": anon
    "/img/**": anon
    "/js/**": anon
    "/**": authc

i use shiro ,and definition a ShiroProperties,i hope freemarkerVariables use map definition . yes?

@ConfigurationProperties(prefix = "shiro")
public class ShiroProperties {
	/**
	 * Custom Realm 
	 */
	private String realm;
	/**
	 * URL of login
	 */
	private String loginUrl;
	/**
	 * URL of success
	 */
	private String successUrl;
	/**
	 * URL of unauthorized
	 */
	private String unauthorizedUrl;
	/**
	 * filter chain
	 */
	private Map<String, String> filterChainDefinitions;
	
	/**
	 * 
	 */
	private String ehacheConfig;

ohalo avatar Apr 28 '17 02:04 ohalo

According to the yaml's document,it can configure the map ,Why not do it

ohalo avatar Apr 28 '17 02:04 ohalo

@ohalo Please take the time to format your comments as it will make them much easier to read. Also, please keep this issue focused on FreeMarker. Your latest example appears to be solely about Shiro. I still need some examples of the freemarker variables that you are trying to set.

wilkinsona avatar Apr 28 '17 06:04 wilkinsona


@ConfigurationProperties(prefix = "freemarker")
public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {

	public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";

	public static final String DEFAULT_PREFIX = "";

	public static final String DEFAULT_SUFFIX = ".ftl";

	/**
	 * Well-known FreeMarker keys which will be passed to FreeMarker's Configuration.
	 */
	private Map<String, String> settings = new HashMap<String, String>();

        private Map<String, String> freemarkerVariables = new HashMap<String, String>();

and configuration is

#freemarker
freemarker:
  settings:
    object_wrapper: com.control.back.halo.basic.utils.FreemarkerObjectWrapper
    datetime_format: yyyy-MM-dd HH:mm:ss
    date_format: yyyy-MM-dd
    time_format: HH:mm:ss    
  freemarker-variables:
    base: http://127.0.0.1:80

ohalo avatar Apr 28 '17 08:04 ohalo

in FreeMarkerAutoConfiguration ,and join factory.setFreemarkerVariables(this.properties.getFreemarkerVariables());

protected static class FreeMarkerConfiguration {

      @Autowired
      protected FreeMarkerProperties properties;

      protected void applyProperties(FreeMarkerConfigurationFactory factory) {
          factory.setTemplateLoaderPaths(this.properties.getTemplateLoaderPath());
          factory.setPreferFileSystemAccess(this.properties.isPreferFileSystemAccess());
          factory.setDefaultEncoding(this.properties.getCharsetName());
          Properties settings = new Properties();
          settings.putAll(this.properties.getSettings());
          factory.setFreemarkerSettings(settings);
          factory.setFreemarkerVariables(this.properties.getFreemarkerVariables());
      }

  }

ohalo avatar Apr 28 '17 08:04 ohalo

Is that ok?sorry my english

ohalo avatar Apr 28 '17 08:04 ohalo

The formatting is much better, thank you. Unfortunately, it's not really what it was looking for.

You've shown how what you're asking for could be implemented in Spring Boot and I already know how to do that. What I don't know is what values you want to set in freemarkerVariables. You've shown one example which is setting base to http://127.0.0.1:80. What other variables would you like to be able to set and what values might they have? I am asking because the values in freemarkerVariables can be anything (it's a Map<String, Object>) and application.properties or application.yml aren't a good way to set some types of Object. As I said above, it's only suited to setting maps, lists and scalar values.

If you want to configure something that's more complex, or you just want to do this without an enhancement to Spring Boot, then registering a BeanPostProcessor for the FreeMarkerConfigurer or FreeMarkerConfigurationFactoryBean may be what you need to do.

wilkinsona avatar Apr 28 '17 08:04 wilkinsona

ok , i understand you,because application.properties or application.yml aren't a good way to set some types of Object,thank you

ohalo avatar Apr 28 '17 09:04 ohalo

because application.properties or application.yml aren't a good way to set some types of Object

Yes, that's right. Can you please share some examples of the values that you would like to set?

wilkinsona avatar Apr 28 '17 09:04 wilkinsona

i want set String values ,not Object value,like: url(static resource url:img,js,css;and other system's url)

ohalo avatar Apr 28 '17 09:04 ohalo

@ohalo It's still not clear what properties you're setting and what you expect to happen. I think the best way to proceed is if you could provide a small focused sample application that shows the problem.

philwebb avatar May 01 '17 19:05 philwebb

@druidDataSource  com.alibaba.druid.pool.DruidDataSource
@driverClass com.mysql.jdbc.Driver

#freemarker
freemarker:
  settings:
    object_wrapper: com.control.back.halo.basic.utils.FreemarkerObjectWrapper
    datetime_format: yyyy-MM-dd HH:mm:ss
    date_format: yyyy-MM-dd
    time_format: HH:mm:ss
    template_update_delay : 0
  freemarker-variables:
    base: http://127.0.0.1:8080
    siteName: speedbuy 
    druidDataSource: *druidDataSource
    driverClass: *driverClass

ohalo avatar May 05 '17 03:05 ohalo