2012-06-25 14 views
6

में grails g.link का उपयोग नहीं कर सकता है मेरे पास एक डोमेन क्लास में स्थैतिक विधि है जो एक यूआरएल देता है। मुझे उस यूआरएल को गतिशील रूप से बनाने की जरूरत है, लेकिन g.link काम नहीं कर रहा है।डोमेन क्लास

static Map options() { 
    // ... 
    def url = g.link(controller: "Foo", action: "bar") 
    // ... 
} 

मैं निम्नलिखित त्रुटियाँ मिलती है:

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 

जाहिर है मेरी समस्या यह है कि मैं स्थिर संदर्भ से g का उपयोग करने की कोशिश कर रहा हूँ, इसलिए मैं इस के आसपास कैसे मिलता है है?

+2

एक स्थिर दायरे में ऐसा मत करो: सबसे अच्छा तरीका है शायद वह आपके डोमेन वर्ग के domainClass संपत्ति बंद हड़पने के लिए है। एक उदाहरण विधि का प्रयोग करें, या इससे भी बेहतर, इस कोड को एक सेवा में रखें ताकि इसे इंजेक्शन दिया जा सके। – OverZealous

+0

मेरे पास ऐसा कोई उदाहरण नहीं है जब मैं यह कर रहा हूं। – ubiquibacon

+1

हाँ, मैं अति उत्साही से सहमत हूं। इस तरह से ऐसा करने के लिए वास्तव में बुरा विचार है। – Gregg

उत्तर

9

g ऑब्जेक्ट एक टैगलिब है, जो डोमेन क्लास के अंदर उपलब्ध नहीं है जैसे कि यह नियंत्रक में होगा। How To Call A Taglib As A Function In A Domain Class

Grails में यह करने के लिए 2 + एक बेहतर तरीका, grailsLinkGenerator सेवा के माध्यम से है, इसलिए की तरह: जैसा कि यहाँ दिखाया आप grailsApplication के माध्यम से इसे प्राप्त कर सकते हैं

def grailsLinkGenerator 

def someMethod() { 
    def url = grailsLinkGenerator.link(controller: 'foo', action: 'bar') 
} 

दोनों ही मामलों में, आप करेंगे एक स्थिर संदर्भ से grailsApplication/grailsLinkGenerator प्राप्त करने के लिए कुछ अतिरिक्त कार्य करने की आवश्यकता है।

def grailsApplication = new MyDomain().domainClass.grailsApplication 
def grailsLinkGenerator = new MyDomain().domainClass.grailsApplication.mainContext.grailsLinkGenerator 
+0

मैं बहुत करीब था। मैंने लिंकनरेटर को आजमाया, लेकिन मुझे स्थिर संदर्भ चेतावनी मिल रही थी। जैसा कि आपने सुझाव दिया है कि यह अच्छा काम करता है, इसे डोमेन क्लास से बाहर ले जाना। धन्यवाद! – ubiquibacon

5

यदि आप Grails 2.x का उपयोग कर रहे हैं तो आप LinkGenerator API का उपयोग कर सकते हैं। यहां एक उदाहरण दिया गया है, मैं एक डोमेन क्लास का पुन: उपयोग कर रहा हूं जिसे मैं पहले परीक्षण कर रहा था इसलिए गैर-यूआरएल संबंधित कार्यक्षमता को अनदेखा करें।

class Parent { 
    String pName 

    static hasMany = [children:Child] 

    static constraints = { 
    } 
    static transients = ['grailsLinkGenerator'] 

    static Map options() { 
     def linkGen = ContextUtil.getLinkGenerator(); 
     return ['url':linkGen.link(controller: 'test', action: 'index')] 
    } 
} 

उपयोगिता कक्षा स्टेटिक विधि

@Singleton 
class ContextUtil implements ApplicationContextAware { 
    private ApplicationContext context 

    void setApplicationContext(ApplicationContext context) { 
     this.context = context 
    } 

    static LinkGenerator getLinkGenerator() { 
     getInstance().context.getBean("grailsLinkGenerator") 
    } 

} 

बीन डेफ के साथ नई उपयोगिता बीन

beans = { 
    contextUtil(ContextUtil) { bean -> 
     bean.factoryMethod = 'getInstance' 
    } 
} 

आप बुनियादी URL की जरूरत है के लिए, लिंक कॉल करने के लिए absolute:true जोड़ें।

संबंधित मुद्दे