我确信必须有一种基于用户信息在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> 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!