2009-06-16 48 views
1

我有以下Applicaton.cfcColdFusion的類定義錯誤

<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> 
+0

我假設你的項目中有一個Application.cfc,而不是上面寫的Applicaton.cfc? – 2009-06-16 12:23:31

+0

是的,在stackoverflow錯字 – mrt181 2009-06-16 15:05:33

+0

最有可能的問題是你的application.cfc。你可以發佈整個application.cfc嗎?如果您沒有使用This.name =「SomeName」命名該應用程序,那麼保存在應用程序範圍中的變量可能會在其他位置,因此您嘗試引用usergateway時會出現問題。 – Jayson 2009-06-16 19:03:50

回答

2

有兩兩件事是我錯了,請參閱:

首先,據我瞭解,使用的Application.cfc的「這個」範圍不工作,你正在努力做到這一點。通過將userGateway對象設置爲應用程序作用域值,它將成爲全局可用的,並且真正使其不必在onApplicationStart中返回。在您的application.cfc中,將您的returntype改爲布爾值並返回true;那應該解決你的問題。其次,如果在你的查詢中,你的參數和條件不是你實際擁有的代理的代理,那麼你引用了一個參數'personid',它不存在於你的函數中。當通過應用程序範圍中的對象調用來調用該查詢時,我發現前面返回的錯誤返回的是java字符串錯誤,而不是CF友好'變量不存在'錯誤。

0

在same.cfm,運行此:

<cfset OnApplicationStart()> 

然後再刷新頁面。現在有用嗎?

+0

不,它沒有幫助 – mrt181 2009-06-16 15:31:37

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

應該是:

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

對不起,沒有幫助。我得到相同的錯誤 – mrt181 2009-06-16 15:13:14

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/>

0

重新啓動CF服務可能幫助。