【问题标题】:How does Coldfusion's createObject() function search for an component?Coldfusion 的 createObject() 函数如何搜索组件?
【发布时间】:2013-12-14 13:49:40
【问题描述】:

我在理解 createObject() 函数时遇到了一些问题,文档中说该函数的用法类似于 CreateObject("component", component-name)

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

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

在测试页面中,我是这样创建对象的:

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

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

【问题讨论】:

  • @Sunny:“自定义标签映射名称”是指哪个名称?
  • 您已经为在服务器设置中创建映射的组件创建了自定义标签路径 -> 映射
  • @Sunny:但在文档中提到Directories specified on the Custom Tag Paths page of ColdFusion Administrator 不在映射中?还是我错了?链接:help.adobe.com/en_US/ColdFusion/10.0/Developing/…

标签: coldfusion coldfusion-10 cfc


【解决方案1】:

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

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

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

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

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

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

【讨论】:

  • 在 ColdFusion 10 中你也可以只做obj = new cfc.subFolder.Item()
  • @MattBusche 我认为它是在 CF8 中引入的。
【解决方案2】:

根据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 管理员的自定义标签路径页面上指定的目录

确保名称正确,组件文件名以 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>

【讨论】:

    【解决方案3】:

    只需将 mycfcs.Item 更改为 Item。

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

    abc = CreateObject("component", "DW-ScheduleBookdan");
    WriteDump(abc);
    

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

    你有什么不同的做法?

    【讨论】:

    • 在什么情况下它不起作用?您是否收到相同的错误消息?
    • 是的,它显示相同的错误。我需要在服务器设置 -> 映射或自定义标签路径中创建映射吗?
    • 尝试在服务器设置中创建 -> 映射
    • 在映射中它正在工作,但在文档help.adobe.com/en_US/ColdFusion/10.0/Developing/… 中提到了自定义标签路径。有什么想法吗?
    • @DanBracuk:你是对的,但我的情况有点不同,在映射目录中有一个名为 checkout 的子目录,所以我使用这种语法 &lt;cfset testObj = createobject("component","checkout.Item")&gt; 它不工作,但是当我正在将 Item.cfc 移动到它正在工作的直接映射文件夹。请帮忙..
    猜你喜欢
    • 2014-03-31
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    相关资源
    最近更新 更多