在Struts 2中每个页面的标题上显示用户名 [英] Show the username on the header of every page in Struts 2

查看:84
本文介绍了在Struts 2中每个页面的标题上显示用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用拦截器对我的应用程序进行身份验证.我已将其添加到defaultStackHibernate其工作正常&我得到了预期的结果,唯一的问题是,现在我想在每个页面的标题上显示用户名.

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.

我已经尝试过这个<s:property value="name"></s:property>,但是仅在欢迎页面上有效,所以有什么办法可以将诸如用户名之类的变量从拦截器发送到我正在调用的每个动作中,或者直接发送到jsp吗?

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?

如果我可以访问其他人,则会话为null 谢谢.

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

以下是我在jsp头页中的代码的一部分:

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>

以下是我的

LoginAction类:

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类:

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.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>

推荐答案

您可以在每页包含的标题页中显示包含该用户名的用户名

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>

这篇关于在Struts 2中每个页面的标题上显示用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆