jsp - Show the username on the header of every page in Struts 2 -
i have used interceptors authentication app.i have added defaultstackhibernate
working fine & getting desired results, issue want show username on header of every page.
i have try <s:property value="name"></s:property>
work welcome page.so there way can send variable username interceptor every action invoking or directly jsp?
if access others action session null thanks.
the following part of code in header page jsp :
<li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown"> <b><s:property value="name"></s:property></b> <b class="caret"></b></a> <ul class="dropdown-menu"> <li><a href="user_profile.html">my profile</a></li> <li class="divider"></li> <li><a href="logout">log out</a></li> </ul> </li>
the following
loginaction
class:
public class loginaction extends actionsupport implements sessionaware, modeldriven<user>{ private static final long serialversionuid = -3369875299120377549l; private user user = new user(); private sessionmap<string, object> sessionattributes = null; private list<user> userlist = new arraylist<user>(); private userdao userdao = new userdaoimpl(); @override public string execute(){ system.out.println("inside execute"); if(user.getname().equals(user.getname()) && user.getname().equals(user.getpassword())){ system.out.println("name"+user.getname() +" password"+user.getpassword()); sessionattributes.put("user", user); return success; } return input; } @override public void setsession(map<string, object> sessionattributes) { this.sessionattributes = (sessionmap<string, object>)sessionattributes; } @override public user getmodel() { return user; } public string logout(){ if(sessionattributes!=null){ sessionattributes.clear(); sessionattributes.invalidate(); } return "success"; } }
authenticationinterceptor
class :
public class authenticationinterceptor implements interceptor { private static final long serialversionuid = -5011962009065225959l; @override public void destroy() { //release resources here } @override public void init() { // create resources here } @override public string intercept(actioninvocation actioninvocation) throws exception { system.out.println("inside auth interceptor"); map<string, object> sessionattributes = actioninvocation.getinvocationcontext().getsession(); user user = (user) sessionattributes.get("user"); if(user == null){ return action.login; }else{ action action = (action) actioninvocation.getaction(); if(action instanceof useraware){ ((useraware) action).setuser(user); } return actioninvocation.invoke(); } }
struts.xml
:
<struts> <constant name="struts.enable.dynamicmethodinvocation" value="false" /> <constant name="struts.devmode" value="false" /> <package name="default" extends="hibernate-default"> <interceptors> <interceptor name="authentication" class="com.inwi.interceptors.authenticationinterceptor"></interceptor> <interceptor-stack name="authstack"> <interceptor-ref name="authentication"></interceptor-ref> <interceptor-ref name="defaultstackhibernate"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="authstack"></default-interceptor-ref> <global-results> <result name="loginaction" type="redirect">/home.action</result> </global-results> <action name="home"> <interceptor-ref name="defaultstack"></interceptor-ref> <result>/login.jsp</result> </action> <action name="loginaction" class="com.inwi.action.loginaction"> <interceptor-ref name="defaultstack"></interceptor-ref> <result name="success">/dashboard.jsp</result> <result name="input">/loginerror.jsp</result> </action> <action name="welcome" class="com.inwi.action.welcomeaction"> <result name="success">/dashboard.jsp</result> </action> <action name="saveorupdatetypesites" method="saveorupdate" class="com.inwi.action.typesitesaction"> <result name="success" type="redirect">listtypesites</result> </action> <action name="listtypesites" method="list" class="com.inwi.action.typesitesaction"> <result name="success">/typesitemain.jsp</result> </action>
once user in session can access name via:
#session.user.name
using name
works when there's on stack exposing name
property. you'd want either (a) check if user in session, or (b) use different template non-logged-in users.
Comments
Post a Comment