4

I'm using <cfproperty /> to make use of implicit getters and setters in ColdFusion (Railo).

However, for more complex values like structs and arrays, how can I append to these?

<cfproperty name="settings" type="struct" />

How can I append an item into the property called settings? If I do the following:

<cfset setSettings(structAppend(getSettings(), { "hello" = "world" })) />

I get the following error:

java.lang.NullPointerException

Am I missing something here? I'm new to the cfproperty tag and thought it would be a time saver, but I can't figure this out.

Also, as a bonus how would I set a default value to these complex data types?

Thanks, Mikey

4

1 回答 1

5

这里有几件事...

<cfset setSettings(structAppend(getSettings(), { "hello" = "world" })) />

设置是一个structstructAppend()返回一个布尔值。在此行之前添加您的结构。其次,结构总是通过引用传递,这意味着,如果你getSettings()得到 a struct,你可以对其进行更改。另一个调用getSettings()将返回相同struct的更新设置。

你只需要这样:

<cfset structAppend(getSettings(), { "hello" = "world" }) />

最后一件事。getSettings()由于未初始化,您可能会收到空指针异常。在您的 cfc 中,在构造函数区域(在您的属性之后),您应该设置一个初始设置struct,如下所示:

<cfset setSettings({}) />
于 2013-05-09T20:04:29.783 回答