2013-02-05 21 views
8

Sto tentando di utilizzare una funzione groovy all'interno di un GSP. Per favore, aiuto come sto per tare i capelli qui.Importazione e utilizzo del codice Groovy in GSP

Nella parte superiore del mio SPG ho <%@ page import = company.ConstantsFile %>

Dentro il mio SPG ho

<p> 
I have been in the heating and cooling business for <%(ConstantsFile.daysBetween())%> 
</p> 

e la mia ConstantsFile.groovy

package company 

import static java.util.Calendar.* 

class ConstantsFile { 

    def daysBetween() { 
     def startDate = Calendar.instance 
     def m = [:] 
     m[YEAR] = 2004 
     m[MONTH] = "JUNE" 
     m[DATE] = 26 
     startDate.set(m) 
     def today = Calendar.instance 

     render today - startDate 
    } 
} 

ho anche provato a cambiare affittuario agli attuali, system.out, ecc ma questo non è il mio problema principale.

Error 500: Internal Server Error 
URI 
/company/ 
Class 
java.lang.NullPointerException 
Message 
Cannot invoke method daysBetween() on null object 

così cerco

<p> 
    I have been in the heating and cooling business for <%(new ConstantsFile.daysBetween())%> 
    </p> 

ma poi mi

Class: org.codehaus.groovy.control.MultipleCompilationErrorsException 

unable to resolve class ConstantsFile.daysBetween @ line 37, column 1. (new ConstantsFile.daysBetween())^1 error 

per favore qualcuno mi aiuti o indicarlo a un sito web che mostra cosa fare .. Ho provato googling e tutto parla di AG: selezionare o qualche altro tipo di tag ... Voglio solo restituire il risultato della funzione come ero solito fare nei JSP.

risposta

17

In primo luogo, l'importazione del GSP dovrebbe essere:

<%@ page import="company.ConstantsFile" %> 

In secondo luogo, i tuoi daysBetween dovrebbe essere statico (ha più senso) e non si renda dal nulla, ma un controller:

class ConstantsFile { 

    static daysBetween() { 
     def startDate = Calendar.instance 
     def m = [:] 
     m[YEAR] = 2004 
     m[MONTH] = "JUNE" 
     m[DATE] = 26 
     startDate.set(m) 
     def today = Calendar.instance 

     return today - startDate 
    } 
} 

in terzo luogo, l'accesso nel modo seguente:

<p>I have been in the heating and cooling business for ${ConstantsFile.daysBetween}</p> 

E, infine, è necessario utilizzare un taglib per questo . Sto modificando il mio post ora di aggiungere un esempio

class MyTagLib { 

    static namespace = "my" 

    def daysBetween = { attr -> 
    out << ConstantsFile.daysBetween() 
    } 
} 

Poi utilizzare per le GSP

<p>I have been in the heating and cooling business for <my:daysBetween /></p> 
+0

Grazie per il vostro aiuto, ho provato il primo modo in cui mi ha suggerito ed ottengo il seguente errore: Errore 500: Internal Server Error URI /società/ Classe groovy.lang.MissingMethodException Messaggio Nessuna firma del metodo: company.ConstantsFile.daysBetween statico() è applicabile per i tipi di argomenti:() valori: [] Pos soluzioni possibili: giorni Tra() –

+0

OK. mi dispiace tanto farlo, ma era l'istanza e la matematica di Calendar che stavo cercando di fare su di essa. +1 e accetta per aiutarmi –