我确信必须有一种基于用户信息在GSP中实现if / else逻辑块的更优雅的方法。
考虑以下:
<g:if test="(user logged in) && (this users post) && (post replied to)">
Show options for this post if replied to
</g:if>
<g:elseif test="(user logged in) && (this users post)">
show options if this users post
</g:elseif>
<g:elseif test="(user logged in)">
show options if not this users post
</g:elseif>
<g:else>
show options if no one is logged in
</g:else>
我必须使用以下命令确定用户是否已登录(sec.loggedInUserInfo(field:'id')!=“”)我知道,我讨厌它!
我不能使用漂亮的Spring Security标记,因为它们不能满足我的要求,并且我试图实现自定义标记,但是将无法实现if / else功能(或者可以)吗?
任何帮助将非常感激。
编辑:
我创建的标签:
<g:isOwnerAndStatusEquals post="${post}" status="${Post.REPLIED_TO}">
My post and is replied to
</g:isOwnerAndStatusEquals>
Tag lib的实现:
class AuthTagLib {
def springSecurityService
def isOwnerAndStatusEquals = { attrs, body ->
def loggedInUser = springSecurityService.currentUser
def post = attrs?.post
def postUser = post?.user
def postStatus = attrs?.status
if(loggedInUser?.id == postUser?.id && job?.status?.equals(thisStatus)) {
out << body()
}
}
}
上面的内容很好,但我不知道如何在同一个标签中添加if / else?
最佳答案
以下内容似乎等同于您的GSP代码,并且不需要您编写任何自定义标签:
<sec:ifLoggedIn>
<g:if test="(this users post) && (post replied to)">
Show options for this post if replied to
</g:if>
<g:elseif test="(this users post)">
show options if this users post
</g:elseif>
<g:else>
show options if not this users post
</g:else>
</sec:ifLoggedIn>
<sec:ifNotLoggedIn>
show options if no one is logged in
</sec:ifNotLoggedIn>
关于grails - Grails Spring Security:GSP中基于用户的if/else逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23351110/