简体   繁体   中英

Grails - Access request object in domain class

Is there a possibility to access the properties of the request object within a method of a domain class?

Thus, I would like to access request.getRemoteAddr() inside beforeInsert() and beforeUpdate() of my domain base class in order to save the IP-Address automatically and would not have to code it within every controller.

Thank you in advance.

You shouldn't access the request or session in your domain class directly, as you can never know, in which context the domain object gets saved/updated. This is by design.

If the situation is really desperate, you can use a workaround:

import org.springframework.web.context.request.RequestContextHolder 

def request = RequestContextHolder.currentRequestAttributes()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM