【问题标题】:ColdFusion Class definition errorColdFusion 类定义错误
【发布时间】:2012-11-30 08:01:48
【问题描述】:

我有以下 Applicaton.cfc

<cffunction name="onApplicationStart" access="public" returntype="Object">
 <cfset application.dsn = "myDB" />
 <cfset application.userGateway = createObject("component","cfc.UserGateway").init(dsn = application.dsn) />
 <cfreturn this />
</cffunction>

这是我的组件 UserGateway.cfc

<cfcomponent name="UserGateway" hint="Data Access Object" output="false">
 <cffunction name="init" access="public" hint="constructor" output="false" returntype="UserGateway">
  <cfargument name="dsn" type="string" required="true" hint="datasource" />
   <cfset variables.dsn = arguments.dsn />
 <cfreturn this />
 </cffunction>

 <cffunction name="getUsers" access="public" output="false" returntype="query">
  <cfargument name="id" type="String" default="" />
  <cfargument name="name" type="String" default="" />
  <cfargument name="district" type="String" default="" />
  <cfset var qQuery = "" />
  <cfquery name="qQuery" datasource="#variables.dsn#">
    SELECT *
    FROM A INNER JOIN B
    ON A.X = B.Y
    WHERE 0=0
    <cfif "#arguments.id#" neq "">
     AND B.X LIKE '%#arguments.id#%'
    </cfif>
    <cfif "#arguments.name#" neq "">
     AND (A.I LIKE '#arguments.name#%'
      OR A.J LIKE '#arguments.name#%')
    </cfif>
    <cfif "#arguments.district#" neq "">
     AND A.O LIKE '%#arguments.district#%'
    </cfif>
  </cfquery>
  <cfreturn qQuery />
 </cffunction>
</cfcomponent>

这是我的同一个.cfm

<cfform action="same.cfm" method="post" preservedata="true">
 <p>ID: <cfinput type="text" name="id" size="20" maxlength="4" /></p>
 <p>Name: <cfinput type="text" name="name" size="20" maxlength="64" /></p>
 <p>District: <cfinput type="text" name="district" size="20" maxlength="3" /></p>
 <p><cfinput class="button" type="submit" name="submit" value="OK" /></p>
</cfform>

<cfif IsDefined("form.submit")>
 <table>
  <cfset qQuery = application.userGateway.getUsers(id = form.id, name = form.name, district = form.district) />
  <cfoutput query="qQuery">
   <tr>
    <td>#qQuery.currentRow#.</a></td>
    <td>#qQuery.I#</a></td>
    <td>#qQuery.M#, #qQuery.N#</a></td>
    <td>#qQuery.D#</a></td>
   </tr>
  </cfoutput>
 </table>
</cfif>

我收到以下错误:

Element USERGATEWAY is undefined in a Java object of type class [Ljava.lang.String;.
The error occurred in same.cfm: line 10

我错过了什么?

-------------------------------------------
-------------------------------------------

当我这样做时,它会起作用。这一定是我作为初学者没有得到的微不足道的东西。

应用程序.cfc

<cffunction name="onRequestStart" access="public" returntype="String">
 <cfset request.dsn="myDB" />
</cffunction>

same.cfm

    <cfset userGateway = createObject("component","cfc.UserGateway").init(dsn = request.dsn) />
    <cfset qGetUser = userGateway.getUsers(id = form.personid, name = form.name, district = form.district) />
  <cfoutput query="qQuery">
   <tr>
    <td>#qQuery.currentRow#.</a></td>
    <td>#qQuery.I#</a></td>
    <td>#qQuery.M#, #qQuery.N#</a></td>
    <td>#qQuery.D#</a></td>
   </tr>
  </cfoutput>

【问题讨论】:

  • 我假设你的项目中有一个 Application.cfc,而不是上面写的 Applicaton.cfc?
  • 问题很可能出在您的 application.cfc 中。你能发布整个 application.cfc 吗?如果您没有使用 This.name = "SomeName" 命名应用程序,则保存在应用程序范围内的变量可能会转到其他地方,因此当您尝试引用用户网关时会出现问题。

标签: coldfusion cfc application.cfc


【解决方案1】:

这里有两点我看错了:

首先,据我了解,在 application.cfc 中使用“this”范围并不像您尝试的那样工作。通过将您的 userGateway 对象设置为应用程序范围的值,它变得全局可用,并且真正不需要在 onApplicationStart 中返回它。在您的 application.cfc 中,将您的 returntype 更改为 boolean 并返回 true;这应该可以解决您的问题。

其次,如果在您的查询中,您的参数和条件不是您实际拥有的代理,那么您引用的参数“personid”在您的函数中不存在。当通过应用程序范围内的对象调用调用该查询时,我看到之前返回的 java 字符串错误是错误,而不是 CF 友好的“变量不存在”错误。

【讨论】:

  • personid 是一个错字,当然应该是唯一的 id
【解决方案2】:

在 same.cfm 中,运行:

<cfset OnApplicationStart()>

然后再次刷新页面。现在可以用了吗?

【讨论】:

    【解决方案3】:
    <cffunction name="init" access="public" hint="constructor" output="false" returntype="UserGateway">
    

    应该是:

    <cffunction name="init" access="public" hint="constructor" output="false" returntype="Any">
    

    【讨论】:

      【解决方案4】:

      以下行不正确:

      <cfset application.userGateway = createObject("component","cfc.UserGateway").init(dsn = application.dsn) />
      

      它应该在没有“cfc”的情况下读取。在你想要的组件名称的开头:

      <cfset application.userGateway = createObject("component","UserGateway").init(dsn = application.dsn) />
      

      此外,请仔细检查 application.cfc 的其余部分是否正确,因为某些内容运行不正确,因为您应该已经看到了找不到组件 cfc.UserGateway 的错误。

      编辑: 我也忘了提到 onApplicationStart 不需要返回任何东西。返回类型应为 void,并且不需要存在 &lt;return this/&gt;

      【讨论】:

      • 如果用户网关在一个名为cfc的目录中,这是正确的,但是,如果所有文件都在同一个目录中,这是不正确的。请发布您的 application.cfc,因为问题似乎存在。
      【解决方案5】:

      【讨论】:

        【解决方案6】:

        重新启动您的 CF 服务可能会有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多