我有一个具有多个角色的用户,我想在标题的下拉列表中显示他的可用角色。一种方法是编写我自己的自定义标签,但是还有其他简单的方法可以做到这一点。
最佳答案
您可以创建一个custom tag library:
class RolesTagLib {
static namespace = 'my'
def springSecurityService
def currentUserRoleSelect = { attrs ->
def user = springSecurityService.getCurrentUser()
def roles = UserRole.findAllByUser(user).collect { it.role } as Set
attrs.from = roles
attrs.optionKey = attrs.optionKey ?: 'id'
attrs.optionValue = attrs.optionValue ?: 'authority'
out << g.select(attrs)
}
}
然后在您的GSP中:
<my:currentUserRoleSelect name="role"/>
关于grails - 如何在Grails中显示在选择框中登录的用户的角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6789828/