アレについて記す

GrailsのCommandObjectでSessionを使う

Posted on February 16, 2014 at 15:56 (JST)

RESTな時代に、使う機会はほとんどない気もしますが、メモを残します。

[settings.groovy]

import org.springframework.web.context.request.RequestAttributes
import org.springframework.web.context.request.RequestContextHolder as RCH

class SomeCommand {
    String value  <br/>
    static constraints = {
        value(validator: {
            if (it != RCH.getRequestAttributes().getAttribute(MY_KEY,
                    RequestAttributes.SCOPE_SESSION))  <br/>
                return ['invalid.value']
            })
    }
}

上記は画面入力した値と、Sessionに保持していた値との凸合わせに使用した例です。