freemarker.template.Configuration.setAutoEscapingPolicy()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(5.0k)|赞(0)|评价(0)|浏览(163)

本文整理了Java中freemarker.template.Configuration.setAutoEscapingPolicy()方法的一些代码示例,展示了Configuration.setAutoEscapingPolicy()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Configuration.setAutoEscapingPolicy()方法的具体详情如下:
包路径:freemarker.template.Configuration
类名称:Configuration
方法名:setAutoEscapingPolicy

Configuration.setAutoEscapingPolicy介绍

[英]Sets when auto-escaping should be enabled depending on the current OutputFormat; default is #ENABLE_IF_DEFAULT_AUTO_ESCAPING_POLICY. Note that the default output format, UndefinedOutputFormat, is a non-escaping format, so there auto-escaping will be off. Note that the templates can turn auto-escaping on/off locally with directives like , which will ignore the policy.

About auto-escaping

Auto-escaping has significance when a value is printed with ${...} (or #{...}). If auto-escaping is on, FreeMarker will assume that the value is plain text (as opposed to markup or some kind of rich text), so it will escape it according the current output format (see #setOutputFormat(OutputFormat)and TemplateConfiguration#setOutputFormat(OutputFormat)). If auto-escaping is off, FreeMarker will assume that the string value is already in the output format, so it prints it as is to the output.

Further notes on auto-escaping:

  • When printing numbers, dates, and other kind of non-string values with ${...}, they will be first converted to string (according the formatting settings and locale), then they are escaped just like string values.
  • When printing TemplateMarkupOutputModel-s, they aren't escaped again (they are already escaped).
  • Auto-escaping doesn't do anything if the current output format isn't an MarkupOutputFormat. That's the case for the default output format, UndefinedOutputFormat, and also for PlainTextOutputFormat.
  • The output format inside a string literal expression is always PlainTextOutputFormat(regardless of the output format of the containing template), which is a non-escaping format. Thus for example, with <#assign s = "foo${bar}">, bar will always get into swithout escaping, but with <#assign s>foo${bar}<#assign> it may will be escaped.

Note that what you set here is just a default, which can be overridden for individual templates via #setTemplateConfigurations(TemplateConfigurationFactory). This setting is also overridden by the standard file extensions; see them at #setRecognizeStandardFileExtensions(boolean).
[中]根据当前OutputFormat设置应启用自动转义的时间;默认为#启用(如果)默认(自动)转义(策略)。请注意,默认输出格式UndefinedOutputFormat是非转义格式,因此自动转义将被禁用。请注意,模板可以使用如下指令在本地打开/关闭自动转义,这将忽略策略。
关于自动转义
当使用${...}(或#{...}打印值时,自动转义具有重要意义。如果“自动转义”处于启用状态,FreeMarker将假定该值为纯文本(与标记或某种富文本相反),因此它将根据当前输出格式(请参见#setOutputFormat(OutputFormat)和TemplateConfiguration#setOutputFormat(OutputFormat))转义该值。如果“自动转义”处于禁用状态,FreeMarker将假定字符串值已经是输出格式,因此它将按原样打印到输出中。
关于自动转义的进一步说明:
*使用${...}打印数字、日期和其他类型的非字符串值时,它们将首先转换为字符串(根据格式设置和区域设置),然后像字符串值一样转义。
*打印TemplateMarkupOutputModel-s时,它们不会再次转义(它们已经转义)。
*如果当前输出格式不是MarkupOutputFormat,则自动转义不会执行任何操作。默认输出格式UndefinedOutputFormat和PlainTextOutputFormat就是这种情况。
*字符串文字表达式中的输出格式始终为PlainTextOutputFormat(与包含模板的输出格式无关),这是一种非转义格式。因此,例如,使用<#assign s = "foo${bar}">时,bar将始终进入swithout而不转义,但使用<#assign s>foo${bar}<#assign>时,它可能会转义。
请注意,您在此处设置的只是一个默认值,可以通过#setTemplateConfigurations(TemplateConfigurationFactory)覆盖单个模板。此设置也被标准文件扩展名覆盖;在#setRecognizeStandardFileExtensions(布尔值)上查看它们。

代码示例

代码示例来源:origin: org.freemarker/freemarker

} else if (AUTO_ESCAPING_POLICY_KEY_SNAKE_CASE.equals(name) || AUTO_ESCAPING_POLICY_KEY_CAMEL_CASE.equals(name)) {
  if ("enable_if_default".equals(value) || "enableIfDefault".equals(value)) {
    setAutoEscapingPolicy(ENABLE_IF_DEFAULT_AUTO_ESCAPING_POLICY);
  } else if ("enable_if_supported".equals(value) || "enableIfSupported".equals(value)) {
    setAutoEscapingPolicy(ENABLE_IF_SUPPORTED_AUTO_ESCAPING_POLICY);
  } else if ("disable".equals(value)) {
    setAutoEscapingPolicy(DISABLE_AUTO_ESCAPING_POLICY);
  } else {
    throw invalidSettingValueException(name, value);

代码示例来源:origin: org.freemarker/freemarker-gae

} else if (AUTO_ESCAPING_POLICY_KEY_SNAKE_CASE.equals(name) || AUTO_ESCAPING_POLICY_KEY_CAMEL_CASE.equals(name)) {
  if ("enable_if_default".equals(value) || "enableIfDefault".equals(value)) {
    setAutoEscapingPolicy(ENABLE_IF_DEFAULT_AUTO_ESCAPING_POLICY);
  } else if ("enable_if_supported".equals(value) || "enableIfSupported".equals(value)) {
    setAutoEscapingPolicy(ENABLE_IF_SUPPORTED_AUTO_ESCAPING_POLICY);
  } else if ("disable".equals(value)) {
    setAutoEscapingPolicy(DISABLE_AUTO_ESCAPING_POLICY);
  } else {
    throw invalidSettingValueException(name, value);

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.freemarker

} else if (AUTO_ESCAPING_POLICY_KEY_SNAKE_CASE.equals(name) || AUTO_ESCAPING_POLICY_KEY_CAMEL_CASE.equals(name)) {
  if ("enable_if_default".equals(value) || "enableIfDefault".equals(value)) {
    setAutoEscapingPolicy(ENABLE_IF_DEFAULT_AUTO_ESCAPING_POLICY);
  } else if ("enable_if_supported".equals(value) || "enableIfSupported".equals(value)) {
    setAutoEscapingPolicy(ENABLE_IF_SUPPORTED_AUTO_ESCAPING_POLICY);
  } else if ("disable".equals(value)) {
    setAutoEscapingPolicy(DISABLE_AUTO_ESCAPING_POLICY);
  } else {
    throw invalidSettingValueException(name, value);

相关文章

Configuration类方法