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

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

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

Configuration.setNamingConvention介绍

[英]Sets the naming convention used for the identifiers that are part of the template language. The available naming conventions are legacy (directive (tag) names are all-lower-case likethis, others are snake case like_this), and camel case ( likeThis). The default is auto-detect, which detects the naming convention used and enforces that same naming convention for the whole template.

This setting doesn't influence what naming convention is used for the setting names outside templates. Also, it won't ever convert the names of user-defined things, like of data-model members, or the names of user defined macros/functions. It only influences the names of the built-in directives ( #elseIf VS elseif), built-ins ( ?upper_case VS ?upperCase ), special variables ( .data_model VS .dataModel).

Which convention to use: FreeMarker prior to 2.3.23 has only supported Configuration#LEGACY_NAMING_CONVENTION, so that's how most templates and examples out there are written as of 2015. But as templates today are mostly written by programmers and often access Java API-s which already use camel case, Configuration#CAMEL_CASE_NAMING_CONVENTION is the recommended option for most projects. However, it's no necessary to make a application-wide decision; see auto-detection below.

FreeMarker will decide the naming convention automatically for each template individually when this setting is set to #AUTO_DETECT_NAMING_CONVENTION (which is the default). The naming convention of a template is decided when the first core (non-user-defined) identifier is met during parsing (not during processing) where the naming convention is relevant (like for s?upperCase or s?upper_case it's relevant, but for s?length it isn't). At that point, the naming convention of the template is decided, and any later core identifier that uses a different convention will be a parsing error. As the naming convention is decided per template, it's not a problem if a template and the other template it #include-s/ #import uses a different convention.

FreeMarker always enforces the same naming convention to be used consistently within the same template "file". Additionally, when this setting is set to non- #AUTO_DETECT_NAMING_CONVENTION, the selected naming convention is enforced on all templates. Thus such a setup can be used to enforce an application-wide naming convention.

Non-strict tags (a long deprecated syntax from FreeMarker 1, activated via #setStrictSyntaxMode(boolean)) are only recognized as FTL tags when they are using the Configuration#LEGACY_NAMING_CONVENTION syntax, regardless of this setting. As they aren't exempt from the naming convention consistency enforcement, generally, you can't use strict Configuration#CAMEL_CASE_NAMING_CONVENTION tags mixed with non-strict tags.
[中]设置用于作为模板语言一部分的标识符的命名约定。可用的命名约定是遗留的(指令(标记)名称都是小写的,其他的都是蛇形的,像这样),以及驼峰式的(像这样)。默认设置为自动检测,它检测使用的命名约定,并对整个模板强制使用相同的命名约定。
此设置不影响模板外设置名称所使用的命名约定。此外,它永远不会转换用户定义的对象(如数据模型成员)的名称,也不会转换用户定义的宏/函数的名称。它只影响内置指令(#elseIf VS.elseIf)、内置(#大写VS?大写)、特殊变量(.data_model VS.dataModel)的名称。
使用哪种约定:2.3.23之前的FreeMarker只支持配置#传统#命名#约定,因此截至2015年,大多数模板和示例都是这样编写的。但是现在的模板大多是由程序员编写的,并且经常访问已经使用camel-case的javaapi-s,因此大多数项目推荐使用配置#camel(camel)case(camel)命名(NAMING)约定。但是,没有必要在应用程序范围内做出决定;请参阅下面的自动检测。
当此设置设置为#AUTO_DETECT_naming_convention(默认设置)时,FreeMarker将分别自动决定每个模板的命名约定。模板的命名约定是在解析过程中(而不是在处理过程中)满足第一个核心(非用户定义)标识符时确定的,在解析过程中,命名约定是相关的(如s大写或s大写,但s长度不相关)。此时,将决定模板的命名约定,任何稍后使用不同约定的核心标识符都将是解析错误。由于命名约定是由每个模板决定的,因此,如果一个模板和它所包含的另一个模板使用不同的约定,则不成问题。
FreeMarker始终强制在同一模板“文件”中一致使用相同的命名约定。此外,当此设置设置为“非自动检测命名”约定时,选定的命名约定将在所有模板上强制执行。因此,这样的设置可用于强制实施应用程序范围的命名约定。
非严格标记(FreeMarker 1中的一种不推荐使用的长语法,通过#setStrictSyntaxMode(布尔值)激活)仅在使用配置#传统#命名#约定语法时被识别为FTL标记,而不考虑此设置。由于它们不受命名约定一致性强制的约束,通常不能将严格配置#CAMEL_CASE_naming_约定标记与非严格标记混合使用。

代码示例

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

setNamingConvention(AUTO_DETECT_NAMING_CONVENTION);
} else if ("legacy".equals(value)) {
  setNamingConvention(LEGACY_NAMING_CONVENTION);
} else if ("camel_case".equals(value) || "camelCase".equals(value)) {
  setNamingConvention(CAMEL_CASE_NAMING_CONVENTION);
} else {
  throw invalidSettingValueException(name, value);

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

setNamingConvention(AUTO_DETECT_NAMING_CONVENTION);
} else if ("legacy".equals(value)) {
  setNamingConvention(LEGACY_NAMING_CONVENTION);
} else if ("camel_case".equals(value) || "camelCase".equals(value)) {
  setNamingConvention(CAMEL_CASE_NAMING_CONVENTION);
} else {
  throw invalidSettingValueException(name, value);

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

setNamingConvention(AUTO_DETECT_NAMING_CONVENTION);
} else if ("legacy".equals(value)) {
  setNamingConvention(LEGACY_NAMING_CONVENTION);
} else if ("camel_case".equals(value) || "camelCase".equals(value)) {
  setNamingConvention(CAMEL_CASE_NAMING_CONVENTION);
} else {
  throw invalidSettingValueException(name, value);

相关文章

Configuration类方法