Grails,如何获取请求对象 [英] Grails, how to get the request object
问题描述
Grails有一个请求对象,它的定义是这里。
问题是当我尝试使用它时,我得到:
没有这样的属性:请求类:xxx
只产生了一个建议:
import javax.servlet.http.HttpServletRequest
import org.springframework.web.context。 request.ServletRequestAttributes
:
def my(){
HttpServletRequest request =((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes())。getRequest();
}
然而,这给出:
groovy.lang.MissingPropertyException:No such property:RequestContextHolder for class:net.ohds.ReportService
- 如何在Grails中获取请求对象的句柄?
- 您如何了解这一点?所以很少有人问这个问题,它必须记录在某个地方,或者在某些例子中,但我找不到。 >解决方案
在Grails 3.0中,从 service 获取请求
对象:
grails-app / services / com / example / MyService.groovy
import org.grails.web.util.WebUtils
...
def request = WebUtils.retrieveGrailsWebRequest()。getCurrentRequest()
def ip = request.getRemoteAddr()
文档:
http://docs.grails.org/latest/api/org/grails/web/ util / WebUtils.html
注意:
旧的 codehaus
包已被弃用。
Grails has a request object which is defined here.
The problem is when I try to use it, I get:
No such property: request for class:xxx
Reading the first 100 hits googling this error only produced one suggestion:
import javax.servlet.http.HttpServletRequest
import org.springframework.web.context.request.ServletRequestAttributes
:
def my() {
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
}
However, this gives:
groovy.lang.MissingPropertyException: No such property: RequestContextHolder for class: net.ohds.ReportService
- How does one get a handle on the request object in Grails?
- How do you find out about this? So few people have asked this question, it must be documented somewhere, or in some example, but I can't find either.
In Grails 3.0, from a service get the request
object using:
grails-app/services/com/example/MyService.groovy
import org.grails.web.util.WebUtils
...
def request = WebUtils.retrieveGrailsWebRequest().getCurrentRequest()
def ip = request.getRemoteAddr()
Documentation:
http://docs.grails.org/latest/api/org/grails/web/util/WebUtils.html
Note:
The old codehaus
package has been deprecated.
这篇关于Grails,如何获取请求对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!