我是Grails的新手。我正在使用Spring Security Grails插件进行身份验证。我想在我的视图gsp文件中获取当前用户。
我尝试这样做...
<g:if test="${post.author == Person.get(springSecurityService.principal.id).id }">
<g:link controller="post" action="edit" id="${post.id}">
Edit this post
</g:link>
</g:if>
我想展示“编辑此文章”链接,但只希望该链接针对已登录用户自己创建的文章可见。然而,出现了错误 -
Error 500: Internal Server Error
URI
/groovypublish/post/list
Class
java.lang.NullPointerException
Message
Cannot get property 'principal' on null object
这是我的Post.groovy文件——
class Post {
static hasMany = [comments:Comment]
String title
String teaser
String content
Date lastUpdated
Boolean published = false
SortedSet comments
Person author
....... more code ....
这是我的Person.groovy领域类文件--
class Person {
transient springSecurityService
String realName
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
byte[] avatar
String avatarType
static hasMany = [followed:Person, posts:Post]
static searchable = [only: 'realName']
........ more code ......
Please help.
<sec:ifLoggedIn>
是有效的。我遵循了你的代码。现在,“编辑此帖子”不会显示在任何帖子中。 - Free-Minded