Show the username on the header of every page in Struts 2

做~自己de王妃 提交于 2019-12-11 03:46:04

问题


I have used interceptors to Authentication to my app.I have added it into the defaultStackHibernate 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.

I have try this <s:property value="name"></s:property> but is work only for the welcome 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?

if I access to others action I get session null thanks.

The following is part of my 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 is my

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>

回答1:


You can show the user name you include it in the header page, which is included on every page

<s:if test="#session.USER != null">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown">
  <b><s:property value="#session.USER.name"/></b><b class="caret"></b></a>
</s:if>



回答2:


Once the user is in session you can access the name via:

#session.USER.name

Using only name works only when there's something on the stack exposing a name property. You'd want to either (a) check if the user is in session, or (b) use a different template for non-logged-in users.



来源:https://stackoverflow.com/questions/31592040/show-the-username-on-the-header-of-every-page-in-struts-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!