Mam metodę statyczną w klasie domeny, która zwraca adres URL. Muszę dynamicznie budować ten URL, ale g.link nie działa.Nie można użyć grails g.link w domenie klasy
static Map options() {
// ...
def url = g.link(controller: "Foo", action: "bar")
// ...
}
otrzymuję następujące błędy:
Apparent variable 'g' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'g' but left out brackets in a place not allowed by the grammar.
@ line 17, column 19.
def url = g.link(controller: "Foo", action: "bar")
^
1 error
Oczywiście moim problemem jest to, że staram się dostęp g
z kontekstu statycznego, więc jak mogę to obejść?
nie rób tego w zakresie statycznego. Użyj metody instancji lub, jeszcze lepiej, umieść ten kod w usłudze, aby mógł zostać wstrzyknięty. – OverZealous
Nie mam instancji w momencie, gdy to robię. – ubiquibacon
Tak, zgadzam się z OverZealous. Naprawdę zły pomysł, aby zrobić to w ten sposób. – Gregg