[英]LinkGenerator Object is null after initialization Grails 2.4.3
I am trying to generate the serverBaseUrl and then populate a field with it. 我正在尝试生成serverBaseUrl,然后用它填充一个字段。 I looked over quite a few instructions on how to use LinkGenerator, but I keep getting a null pointer exception on the LinkGenerator Object. 我查看了很多有关如何使用LinkGenerator的说明,但是我在LinkGenerator对象上不断收到空指针异常。
What I am working with: 我正在使用的是:
import org.codehaus.groovy.grails.web.mapping.LinkGenerator
MyService{
def linkGenerator
String client_url = getServerUrl()
String getServerUrl(){
linkGenerator.serverBaseURL
}
}
I have tried using LinkGenerator linkGenerator, relocating it, and adding a value to grails.serverURL in Config.groovy. 我尝试使用LinkGenerator linkGenerator,将其重新放置,然后在Config.groovy中将值添加到grails.serverURL中。 The string client_url is on a trigger, so the method does not get called until after the program is running. 字符串client_url在触发器上,因此直到程序运行之后才调用该方法。
I am using Grails 2.4.3 and Java 1.8 我正在使用Grails 2.4.3和Java 1.8
The bean is actually called grailsLinkGenerator
, so you need to declare it as LinkGenerator grailsLinkGenerator
(or def grailsLinkGenerator
). 该bean实际上称为grailsLinkGenerator
,因此您需要将其声明为LinkGenerator grailsLinkGenerator
(或def grailsLinkGenerator
)。
I couldn't find the official documentation for it, but check for example http://mrhaki.blogspot.de/2012/01/grails-goodness-generate-links-outside.html . 我找不到它的官方文档,但请查看例如http://mrhaki.blogspot.de/2012/01/grails-goodness-generate-links-outside.html 。
There are a couple of problems with your approach. 您的方法有两个问题。 One is that you probably want to access the grailsLinkGenerator
bean. 一种是您可能要访问grailsLinkGenerator
bean。
class MyService {
def grailsLinkGenerator
String getServerUrl(){
grailsLinkGenerator.serverBaseURL
}
}
In your code you have this: 在您的代码中,您有以下内容:
class MyService{
def linkGenerator
String client_url = getServerUrl()
String getServerUrl(){
linkGenerator.serverBaseURL
}
}
Aside from the bean name problem, yo uare trying to initialize client_url
before dependency injection has happened. 除了Bean名称问题外,您client_url
在依赖项注入发生之前尝试初始化client_url
。 It isn't clear why you need that property to begin with but if you really do want something like that, try this... 尚不清楚为什么需要该属性开头,但是如果您确实想要这样的内容,请尝试以下方法...
import org.springframework.beans.factory.InitializingBean
class MyService implements InitializingBean {
def grailsLinkGenerator
String client_url
String getServerUrl(){
grailsLinkGenerator.serverBaseURL
}
void afterPropertiesSet() {
client_url = getServerUrl()
}
}
I hope that helps. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.