1

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

相同的.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>
4

6 回答 6

2

There are two things I see wrong here:

First, To my understanding, using the 'this' scope in application.cfc doesn't work the way you're trying to do it. By setting your userGateway object to an application scoped value, it becomes globally available and really makes returning it in onApplicationStart unnecessary. In your application.cfc, change your returntype to boolean and just return true; that should fix your problem.

Second, if in your query, your arguments and conditionals are not proxies of what you actually have, you're referencing an argument 'personid' which does not exist in your function. When calling that query through an object call in the application scope, I've seen the java string error returned as an error before as opposed to the CF Friendly 'variable doesn't exist' error.

于 2009-06-18T04:42:03.180 回答
0

在 same.cfm 中,运行以下命令:

<cfset OnApplicationStart()>

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

于 2009-06-16T12:09:47.173 回答
0
<cffunction name="init" access="public" hint="constructor" output="false" returntype="UserGateway">

应该:

<cffunction name="init" access="public" hint="constructor" output="false" returntype="Any">
于 2009-06-16T12:14:27.947 回答
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 的错误。

EDIT: I also forgot to mention that onApplicationStart does not need to return anything. The return type should be void and no <return this/> needs to be present.

于 2009-06-16T13:52:46.547 回答
0

Could it be this:

http://kathylynnward.wordpress.com/2008/04/14/lyra-captcha-error-element-captcha-is-undefined-in-a-java-object-of-type-class-ljavalangstring/

(I'll elaborate the post if this is the problem)

于 2009-06-16T18:07:53.940 回答
0

restart your CF service might help.

于 2009-06-16T18:42:43.353 回答