Reputation: 2149
How can we access request object in src/groovy
files in grails. Is it possible?.
I don't want to pass request object from controller to src/groovy
files through method parameter because it's an existing framework, is there any alternative way to do it.
I am only need request ContentType.
If getting request object is not possible in src/groovy
then i am planning to put that in ThreadLocal
class and get there, is this a good idea?
Upvotes: 1
Views: 2161
Reputation: 75671
I don't understand your point about this being an existing framework - whether you change it to pass in the request or to access it in a more convenient way, you're still changing it, right?
There's already a ThreadLocal to hold this. Add this import
import org.springframework.web.context.request.RequestContextHolder
and then you can access the request as
def request = RequestContextHolder.currentRequestAttributes().request
Upvotes: 3