ColdFusion 静态键/值列表?
我有一个数据库表,它是定义术语的字典——键、值。我想从数据库加载应用程序范围中的字典,并将其保留在那里以提高性能(它不会改变)。
我认为这可能是某种“结构”,但我对 ColdFusion 非常陌生(帮助另一个团队)。
然后,我想对输出到浏览器的一些字符串进行一些简单的字符串替换,循环遍历定义的术语并用一些 HTML 替换这些术语来定义术语(悬停或链接,详细信息稍后制定) ,不重要)。
这是 application.cfc 文件中当前的代码:
<cffunction name="onApplicationStart">
<cfquery name="qryDefinedTerms" datasource="mydsn">
SELECT term, definition FROM definedterms
</cfquery>
<cfset application.definedterms = Array(1)>
<cfloop query="qryDefinedTerms">
<cfset myHash = structNew()>
<cfset myHash.put("term", qryDefinedTerms.term)>
<cfset myHash.put("definition", qryDefinedTerms.definition)>
<cfset ArrayAppend(application.definedterms, myHash)>
</cfloop>
</cffunction>
调用页面尝试按如下方式使用它:
function ReplaceDefinitions(inputstring) {
for (thisdef = 1 ;
thisdef LTE ArrayLen(application.definedterms);
thisdef = (thisdef+1)) {
inputstring = Replace(inputstring,
application.definedterms(thisdef).term,
application.definedterms(thisdef).definition, "ALL");
}
return inputstring;
}
当我调用该函数时,我得到:“元素 DEFINEDTERMS 在 APPLICATION 中未定义”。
编辑:强制调用 OnApplicationStart() 有效,显然 Cold Fusion 的 application.cfc 与 ASP.NET 的 web.config 不同,更改它不会重置应用程序。
I have a database table that is a dictionary of defined terms -- key, value. I want to load the dictionary in the application scope from the database, and keep it there for performance (it doesn't change).
I gather this is probably some sort of "struct," but I'm extremely new to ColdFusion (helping out another team).
Then, I'd like to do some simple string replacement on some strings being output to the browser, looping through the defined terms and replacing the terms with some HTML to define the terms (a hover or a link, details to be worked out later, not important).
This is the code currently in the application.cfc file:
<cffunction name="onApplicationStart">
<cfquery name="qryDefinedTerms" datasource="mydsn">
SELECT term, definition FROM definedterms
</cfquery>
<cfset application.definedterms = Array(1)>
<cfloop query="qryDefinedTerms">
<cfset myHash = structNew()>
<cfset myHash.put("term", qryDefinedTerms.term)>
<cfset myHash.put("definition", qryDefinedTerms.definition)>
<cfset ArrayAppend(application.definedterms, myHash)>
</cfloop>
</cffunction>
The calling page attempts to use it as follows:
function ReplaceDefinitions(inputstring) {
for (thisdef = 1 ;
thisdef LTE ArrayLen(application.definedterms);
thisdef = (thisdef+1)) {
inputstring = Replace(inputstring,
application.definedterms(thisdef).term,
application.definedterms(thisdef).definition, "ALL");
}
return inputstring;
}
When I call the function, I get back: "Element DEFINEDTERMS is undefined in APPLICATION".
Edit: forcing a call to OnApplicationStart() worked, apparently Cold Fusion's application.cfc isn't like ASP.NET's web.config, changing it doesn't reset the application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您提出了很多单独的问题,但我会尽力回答它们!您还没有说明您正在使用的 ColdFusion 版本,因此我将使用适用于 ColdFusion 8 及更高版本的代码来回答。
ColdFusion 使用一个名为 Application.cfc 的特殊文件,您可以将其放入 Web 应用程序的路径中(类似于 ASP.Net 中的 Global.asax)。它有一个名为 onApplicationStart 的方法,该方法仅在应用程序启动时执行(因此不是在每个请求时执行)。这是放置任何常量的好地方。下面是一个简单的示例,它使用 {} 语法设置结构体(类似于其他语言中的映射):
Application.cfc
如果您想从数据库中获取数据,这里有一个简单的方法(有很多其他可能更好的方法,但这应该让您开始!)
至于替换字符串中的值,您可以执行以下操作:
somescript.cfm
正如我所说,有很多方法可以解决您的问题,但希望您会发现这是一个很好的起点(尽管我还没有测试过!)。
祝您好运,欢迎来到 ColdFusion!
There are a lot of separate questions you've asked but I'll have a go at answering them! You also haven't said what version of ColdFusion you are using, so I'm going to answer with code that will work in ColdFusion 8 and higher.
ColdFusion uses a special file called Application.cfc that you put in the route of your web application (similar to Global.asax in ASP.Net). It has a method in in called onApplicationStart that is only executed when the application starts (so not on each request). This is a great place to put any constants. Here is a simple example which sets a struct (like a map in other languages) using the {} syntax:
Application.cfc
If you want to get the data from a database, here is a simple way to do it (there are lots of other ways that are probably better but this should get you started!)
As for replacing values in a string then you can do something like this:
somescript.cfm
As I said there are lots of ways to solve your question, however hopefully you'll find this a good starting point (although I haven't tested it!).
Good luck and welcome to ColdFusion!
另外,请修复 Application.cfc 中的数组声明。它应该是ArrayNew(1)而不是Array(1)。然后尝试重新初始化应用程序变量。一种方法是使用 cfinvoke:
完成后,并进行 Ben 提到的一些更改。该功能应该可以工作。注意:如果您使用 CF8+,则可以使用较短的 <= 和 ++ 运算符
Also, fix your array declaration in the Application.cfc. It should be ArrayNew(1) rather than Array(1). Then try reinitializing the application variables. One way is using cfinvoke:
Once you have done that, and made some of the changes Ben mentioned. The function should work. Note: You can use the shorter <= and ++ operators if you are using CF8+
无需深入研究,我就会看到:
应该是这样:
在 CF(像许多语言一样)中,括号意味着函数调用,方括号意味着数组引用。
Without looking too much deeper, I see that this:
should be this:
In CF (like many languages) parens imply a function call, and square brackets imply an array reference.