Coldfusion 的 createObject() 函数如何搜索组件?

Dee*_*dhy 5 coldfusion cfc coldfusion-10

我在理解该createObject()函数时遇到了一些问题,文档说它像CreateObject("component", component-name).

文档中提到Coldfusion在“ColdFusion Administrator的Custom Tag Paths页面上指定的目录”中搜索组件

但它在我的情况下不起作用。我有一个在 CF admin 中映射的文件夹用于自定义标签,在该文件夹内我放置了一个名为“mycfcs”的文件夹,其中我的 cfc 被命名为Item.cfc

在测试页面中,我以这种方式创建对象:

<cfset testObj = createobject("component","mycfcs.Item")>

但它抛出错误“找不到 ColdFusion 组件或接口”。

Sco*_*roz 5

在 Application.cfc 中创建指向包含 CFC 的文件夹的每个应用程序映射

this.mappings["/cfc"] = {path to your CFCs};
Run Code Online (Sandbox Code Playgroud)

然后在您的createObject()通话中,使用点分隔的 CFC 路径。

createObject("component", "cfc.Item");
Run Code Online (Sandbox Code Playgroud)

如果您有子文件夹,您可以这样访问它

createObject("component", "cfc.subFolder.Item");
Run Code Online (Sandbox Code Playgroud)

  • 在 ColdFusion 10 中,您也可以只执行 `obj = new cfc.subFolder.Item()` (4认同)

小智 5

根据此 Adob​​e 链接

实例化或调用组件时,可以只指定组件名称,也可以指定限定路径。要指定限定路径,请用句点分隔目录名称,而不是反斜杠。例如,myApp.cfcs.myComponent 指定在 myApp\cfcs\myComponent.cfc 中定义的组件。有关其他信息,请参阅保存和命名 ColdFusion 组件。

ColdFusion 使用以下规则来查找指定的 CFC: ? 如果使用 cfinvoke 或 cfobject 标记或 CreateObject 函数从 CFML 页面访问 CFC,ColdFusion 将按以下顺序搜索目录:

  1. 调用CFML页面的本地目录
  2. 网站根
  3. 在 ColdFusion Administrator 的自定义标记路径页面上指定的目录

确保名称正确,组件文件名以 CFC(不是 CFM)结尾,createObject 命令中的路径引用正确,并且您的情况正确(取决于操作系统)。

这是我用来动态加载 CFC 的一些代码:

<cffunction name="getNewObject" hint="Gets a new object with the specified type, module, project and settings" access="private">
  <cfargument name="myDocObj" required="yes" hint="Document Object to create a report from">     
  <cfscript>
      //Create path based on arguments
      var objectPath = createPath(arguments.myDocObj);
        var tmpObj = createObject("component", "#objectPath#").init(this.Settings)

      // return new object based on objectPath, which uses module and type to derive the name of the cfc to load
      return tmpObj;
  </cfscript>
</cffunction>

<cffunction name="createPath" access="private">
  <cfargument name="myDocObj" required="yes">
  <cfscript>
    var module = LCase(arguments.myDocObj.get('module'));
    var type = LCase(arguments.myDocObj.get('documentType'));
    // return the name of the cfc to load based on the module and type
    return "plugins.#module#_#type#";
  </cfscript>
</cffunction>
Run Code Online (Sandbox Code Playgroud)


Dan*_*cuk -2

只需将 mycfcs.Item 更改为 Item 即可。

在我们的开发服务器上,我们将“D:\DW\CF_stuff\CustomTags”指定为自定义标记位置。我有一个文件位于“I:\CF_stuff\CustomTags\Components\CompareData\DW-ScheduleBookdan.cfc”。如果我运行这段代码:

abc = CreateObject("component", "DW-ScheduleBookdan");
WriteDump(abc);
Run Code Online (Sandbox Code Playgroud)

我看到了对象的属性和方法。

你在做什么不同的事情?