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

问题描述 投票:4回答:3

我在理解createObject()函数时遇到了一些问题,文档说这些函数就像CreateObject("component", component-name)一样。

在文档中,提到Coldfusion在“ColdFusion Administrator的自定义标记路径”页面上指定的目录中搜索组件

但它不适用于我的情况。我在CF管理员中为自定义标签映射了一个文件夹,在该文件夹中我放置了一个名为“mycfcs”的文件夹,其中我的cfc名为Item.cfc

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

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

但它抛出一个错误“无法找到ColdFusion组件或接口”。

coldfusion coldfusion-10 cfc
3个回答
4
投票

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

this.mappings["/cfc"] = {path to your CFCs};

然后在你的createObject()调用中,使用指向CFC的点分隔路径。

createObject("component", "cfc.Item");

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

createObject("component", "cfc.subFolder.Item");

4
投票

this Adobe link说:

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

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

  1. 调用CFML页面的本地目录
  2. Web根目录
  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>

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

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

你有什么不同的做法?

© www.soinside.com 2019 - 2024. All rights reserved.