ColdFusion 静态键/值列表?

发布于 2024-08-29 14:40:03 字数 1397 浏览 1 评论 0原文

我有一个数据库表,它是定义术语的字典——键、值。我想从数据库加载应用程序范围中的字典,并将其保留在那里以提高性能(它不会改变)。

我认为这可能是某种“结构”,但我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

め七分饶幸 2024-09-05 14:40:03

您提出了很多单独的问题,但我会尽力回答它们!您还没有说明您正在使用的 ColdFusion 版本,因此我将使用适用于 ColdFusion 8 及更高版本的代码来回答。

ColdFusion 使用一个名为 Application.cfc 的特殊文件,您可以将其放入 Web 应用程序的路径中(类似于 ASP.Net 中的 Global.asax)。它有一个名为 onApplicationStart 的方法,该方法仅在应用程序启动时执行(因此不是在每个请求时执行)。这是放置任何常量的好地方。下面是一个简单的示例,它使用 {} 语法设置结构体(类似于其他语言中的映射):

Application.cfc

<cfcomponent>
  <cffunction name="onApplicationStart">
    <!--- set global constants here --->
    <cfset application.foo = { a=1, b=2, c="my string" }>
  </cffunction>
</cfcomponent>

如果您想从数据库中获取数据,这里有一个简单的方法(有很多其他可能更好的方法,但这应该让您开始!)

<cfcomponent>
  <cffunction name="onApplicationStart">
    <!--- set global constants here --->
    <cfset application.datasource = "mydsn">

    <cfquery name="qryConstants" datasource="#application.datasource#">
      select key, value
      from tblConstants
    </cfquery>

    <cfset application.constants = {}>
    <cfloop query="qryConstants">
      <cfset application.constants[ qryConstants.key ] = qryConstants.value>
    </cfloop>
  </cffunction>

</cfcomponent>

至于替换字符串中的值,您可以执行以下操作:

somescript.cfm

<cfsavecontent variable="somestring">
Hello, ${key1} how are you? My name is ${key2} 
</cfsavecontent>

<!--- replace the ${key1} and ${key2} tokens --->
<cfloop collection="#application.constants#" item="token">
  <cfset somestring = ReplaceNoCase( somestring, "${#token#}", application.constants[ token ], "all" )>
</cfloop>

<!--- show the string with tokens replaced --->
<cfoutput>#somestring#</cfoutput>

正如我所说,有很多方法可以解决您的问题,但希望您会发现这是一个很好的起点(尽管我还没有测试过!)。

祝您好运,欢迎来到 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

<cfcomponent>
  <cffunction name="onApplicationStart">
    <!--- set global constants here --->
    <cfset application.foo = { a=1, b=2, c="my string" }>
  </cffunction>
</cfcomponent>

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!)

<cfcomponent>
  <cffunction name="onApplicationStart">
    <!--- set global constants here --->
    <cfset application.datasource = "mydsn">

    <cfquery name="qryConstants" datasource="#application.datasource#">
      select key, value
      from tblConstants
    </cfquery>

    <cfset application.constants = {}>
    <cfloop query="qryConstants">
      <cfset application.constants[ qryConstants.key ] = qryConstants.value>
    </cfloop>
  </cffunction>

</cfcomponent>

As for replacing values in a string then you can do something like this:

somescript.cfm

<cfsavecontent variable="somestring">
Hello, ${key1} how are you? My name is ${key2} 
</cfsavecontent>

<!--- replace the ${key1} and ${key2} tokens --->
<cfloop collection="#application.constants#" item="token">
  <cfset somestring = ReplaceNoCase( somestring, "${#token#}", application.constants[ token ], "all" )>
</cfloop>

<!--- show the string with tokens replaced --->
<cfoutput>#somestring#</cfoutput>

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!

  • John
〆凄凉。 2024-09-05 14:40:03

另外,请修复 Application.cfc 中的数组声明。它应该是ArrayNew(1)而不是Array(1)。然后尝试重新初始化应用程序变量。一种方法是使用 cfinvoke:

<cfinvoke component="Application" method="OnApplicationStart" />

完成后,并进行 Ben 提到的一些更改。该功能应该可以工作。注意:如果您使用 CF8+,则可以使用较短的 <= 和 ++ 运算符

<cfscript>
   // Added variable scoping
   function ReplaceDefinitions(inputstring) {
      var thisdef = "";
      var newString = arguments.inputstring;
       for (thisdef EQ 1 ; thisdef LTE ArrayLen(application.definedterms); thisdef = thisdef+1) {
           newString = Replace(   newString, 
                                application.definedterms[thisdef].term, 
                                application.definedterms[thisdef].definition, 
                                "ALL" );
       }   
       return newString;
   }
</cfscript>

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:

<cfinvoke component="Application" method="OnApplicationStart" />

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+

<cfscript>
   // Added variable scoping
   function ReplaceDefinitions(inputstring) {
      var thisdef = "";
      var newString = arguments.inputstring;
       for (thisdef EQ 1 ; thisdef LTE ArrayLen(application.definedterms); thisdef = thisdef+1) {
           newString = Replace(   newString, 
                                application.definedterms[thisdef].term, 
                                application.definedterms[thisdef].definition, 
                                "ALL" );
       }   
       return newString;
   }
</cfscript>
天荒地未老 2024-09-05 14:40:03

无需深入研究,我就会看到:

application.definedterms(thisdef).term

应该是这样:

application.definedterms[thisdef].term

在 CF(像许多语言一样)中,括号意味着函数调用,方括号意味着数组引用。

Without looking too much deeper, I see that this:

application.definedterms(thisdef).term

should be this:

application.definedterms[thisdef].term

In CF (like many languages) parens imply a function call, and square brackets imply an array reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文