2011-05-26 46 views

回答

2

如果您想要gsp中的用户对象,只需将其作为模型映射的一部分从控制器传回即可。在控制器的行动做

def user = springSecurityService.getCurrentUser() 
render view: 'yourgsp', model: [user: user] 
0

我不知道,如果我们可以直接使用这个标签,我能不早找到它,所以我做了我的自定义标签用于此目的

<m:userName id="${session.SPRING_SECURITY_CONTEXT?.authentication?.principal?.id}"/>

def userName = { attrs -> 
Long id = attrs['id'] ? attrs['id'].toLong() : null 
User user = User?.get(id); 
out << user?.firstName 

}

10

这是简单,对我来说工作得很好:

<g:set var="user" value="${sec.username()}" /> 
0

我已创建了一个标签库作为其中添加上与loggedInUser gsp页面为:

Welcome <g:loggedInUser/> ! 

这是显示登录在我的项目中每个gsp顶部的用户名。在自定义标签库我的代码如下:

def springSecurityService 

def loggedInUser={ 
    if(springSecurityService.getPrincipal().equals("anonymousUser")){ 
     response.sendRedirect("${request.contextPath}/login/auth") 
    }else{ 
     out <<"${springSecurityService.currentUser.username}" 
     out << """ [${link(controller:"logout"){"Logout"}}]""" 
    } 
} 

所以它显示的每个页面上:欢迎您帐号[退出]!