1

リストに組み込みたい if host.machine == 1 then action=Set else action=Create

次の出力が欲しい Type:Machine;Action:Set;Attributes[Name:machine1~NodeManager.ListenAddress:10.104.17.70~NodeManager.ListenPort:5558]<BR> Type:Machine;Action:Create;Attributes[Name:machine2~NodeManager.ListenAddress:10.104.17.71~NodeManager.ListenPort:5558]<BR>

私のデータ

hosts:[{"name": "trfuoemlpa004v", "node": 0, "server": 1, "Machine": 1, "ManagedPort": "7002", "SSLPort": 1081}, {"name": "trfuoemlpa007v", "node": 1, "server": 2, "Machine": 2, "ManagedPort": "7002", "SSLPort": 1081}]

さまざまなテンプレートを作成しましたが、すべて失敗します:

1) <#list hosts as host><#assign machine=${host.machine}><#if machine == 1><#assign action="Set"><#else><#assign action="Create"></#if>Type:Machine;Action:${action};Attributes[Name:${host.machine}~NodeManager.ListenAddress:${host.name}~NodeManager.ListenPort:${nodeManagerPort}]<BR></#list>

**freemarker.core.ParseException: Encountered "}" at line 8, column 40 in J2EE.properties. Was expecting one of:">" ... "." ... "[" ... "(" ... "?" ... "!" ... <TERMINATING_EXCLAM> ... "??" ... "+" ...**

2) <#list hosts as host><#if host.machine == 1><#assign action="Set"><#else><#assign action="Create"></#if>Type:Machine;Action:${action};Attributes[Name:${host.machine}~NodeManager.ListenAddress:${host.name}~NodeManager.ListenPort:${nodeManagerPort}]<BR></#list>

**freemarker.core.InvalidReferenceException: Expression host.machine is undefined**

3) <#list hosts as host><#if $host.machine} == 1 > ...

**freemarker.core.ParseException: Encountered "}" at line 8, column 40 in J2EE.properties. Was expecting one of:">" ... "." ... "[" ... "(" ... "?" ... "!" ... <TERMINATING_EXCLAM> ... "??" ... "+" ...**

4

1 に答える 1

0

FreeMarker 式内 (文字列リテラル内を除く) に$特別な意味がないため、2 番目が正しいものです。式の値を静的テキスト(または文字列リテラル)に挿入するためにのみ使用されます。このエラー メッセージは、変数にサブ変数が呼び出されていないか、それが であることを意味します。最後のケースでは、 のようなデフォルトを指定する必要がありました。${expression}hostmachinenullhost.machine!0

ところで、これ:

<#if host.machine == 1><#assign action="Set"><#else><#assign action="Create"></#if>Type:Machine;Action:${action};

次のように記述できます。

Type:Machine;Action:<#if host.machine == 1>Set<#else>Create</#if>;

于 2012-09-12T12:14:00.323 に答える