【发布时间】:2014-10-06 15:52:51
【问题描述】:
我正在尝试编写一个简单的 Grails 控制器来返回用户的位置。
当我硬核 IP 地址时,位置部分可以正常工作。问题在于获取用户的IP地址。
运行以下代码时,出现以下错误:
“方法没有签名:helloworld.HelloController.getRemoteAddr() 适用于参数类型:() 值:[]”
错误似乎是由以下行引起的:
def ipAddress = getRemoteAddr()
我通过运行 grails 应用程序的机器上的 localhost:8090/helloworld/hello/index 和笔记本电脑上的 192.168.0.10:8090/helloworld/hello.index 进行连接。
我的其余代码:
类 HelloController {
def geoIpService
def index() {
def ip = getIpAddress()
def location = geoIpService.getLocation(ip)
render location.countryName + " " + location.city
}
def getIpAddress(javax.servlet.http.HttpServletRequest request) {
// def ipAddress = getRemoteAddr()
def ipAddress = getRemoteAddr()
if (ipAddress && InetAddressValidator.VALIDATOR.isValid(ipAddress)){
log.debug("Remote IP Address ::: " + ipAddress)
return ipAddress
}
ipAddress = request.getHeader("X-Forwarded-For")
if(ipAddress && InetAddressValidator.VALIDATOR.isValid(ipAddress)) {
log.debug("Remote IP Address ::: " + ipAddress)
return ipAddress
}
ipAddress = request.getHeader("Client-IP")
if(ipAddress && InetAddressValidator.VALIDATOR.isValid(ipAddress)) {
log.debug("Remote IP Address ::: " + ipAddress)
return ipAddress
}
return ipAddress
}
}
任何帮助将不胜感激!
【问题讨论】:
-
不应该是
request.getRemoteAddr()吗? -
@MarcB 嗯,现在我收到以下错误:无法在空对象上调用方法 getRemoteAddr() 这很简单。我对 Grails 和我目前正在做的任务非常陌生。