ColdFusion类定义错误

时间:2009-06-16 10:16:38

标签: coldfusion cfc application.cfc

我有以下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>

这是我的same.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

我缺少什么?

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

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

的Application.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>

6 个答案:

答案 0 :(得分:2)

我认为有两件事是错误的:

首先,据我所知,在application.cfc中使用'this'作用域并不像你尝试的那样工作。通过将userGateway对象设置为应用程序作用域值,它将变为全局可用,并且确实无需在onApplicationStart中返回它。在你的application.cfc中,将你的返回类型改为boolean并返回true;应该解决你的问题。

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

答案 1 :(得分:0)

在same.cfm中,运行:

<cfset OnApplicationStart()>

然后再次刷新页面。它现在有效吗?

答案 2 :(得分:0)

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

应该是:

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

答案 3 :(得分:0)

以下行不正确:

<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不需要返回任何东西。返回类型应该是无效的,并且不需要存在<return this/>

答案 4 :(得分:0)

答案 5 :(得分:0)

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