开发者

struts2 - How to get username on every page in my web application

I have used interceptors to check if the user is valid & if so send him to the action otherwise deny him from doing the action.I have added it into the default-stack so that it can occur on every action the user is invoking. Its working fine & I am getting the desired results, the only issue is that now I want to show the username on the header of every page. So is there a way I can send开发者_开发问答 some variable like username from the interceptor to every action I am invoking or directly to the jsp?


You need to persist it in a way each request can access the authenticated object. The way is to store the user object in session or at least store an identifier like username with which you could retrieve the user object again.


you can add the data into the session & access the session value in every page...


i don't know what library you are using. but i think the generic way to retrieve the current user's username is by invoking httpServletRequest.getUserPrincipal().getName()

it also works when i use spring security


Another option. In your base action add:

public String getLoggedInUser() {
  return ServletActionContext.getRequest().getRemoteUser();
}

And within your (jsp) views you will be able to access the currently logged in username via:

${loggedInUser}

or

<s:property value="loggedInUser"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜