Domanda

Ho il seguente 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>

Questo è il mio componente 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>

E questo è il mio stesso.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>

Ottengo il seguente errore:

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

Cosa mi sto perdendo?

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

Quando lo faccio in questo modo funziona. deve essere qualcosa di banale che io come principiante non capisco.

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>
È stato utile?

Soluzione

Ci sono due cose che vedo di sbagliato qui:

In primo luogo, a mio avviso, l'utilizzo dell'ambito 'this' in application.cfc non funziona nel modo in cui si sta tentando di farlo. Impostando l'oggetto userGateway su un valore con ambito applicativo, diventa disponibile a livello globale e rende davvero superfluo restituirlo in onApplicationStart. In application.cfc, modifica il tipo di ritorno in booleano e restituisci true; questo dovrebbe risolvere il tuo problema.

In secondo luogo, se nella tua query, i tuoi argomenti e le tue condizioni non sono proxy di ciò che hai effettivamente, stai facendo riferimento a un argomento 'personid' che non esiste nella tua funzione. Quando ho chiamato quella query tramite una chiamata di oggetto nell'ambito dell'applicazione, ho visto l'errore della stringa java restituito come errore prima dell'errore "variabile non esiste" di CF Friendly.

Altri suggerimenti

In same.cfm, esegui questo:

<cfset OnApplicationStart()>

Quindi aggiorna nuovamente la pagina. Ora funziona?

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

dovrebbe essere:

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

La seguente riga non è corretta:

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

Dovrebbe leggere senza " cfc. " all'inizio del nome del componente desiderato:

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

Inoltre, ricontrolla la correttezza del resto di application.cfc perché qualcosa non funziona correttamente, poiché avresti dovuto vedere questo errore che non è riuscito a trovare il componente cfc.UserGateway.

Modifica Ho anche dimenticato di menzionare che onApplicationStart non ha bisogno di restituire nulla. Il tipo restituito deve essere nullo e non è necessario che <return this/> sia presente.

potrebbe essere utile riavviare il servizio CF.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top