【问题标题】:Show the username on the header of every page in Struts 2在 Struts 2 中每个页面的页眉上显示用户名
【发布时间】:2015-07-23 15:38:37
【问题描述】:

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

我已经尝试过这个<s:property value="name"></s:property>,但仅适用于欢迎页面。那么有没有办法可以将一些变量(如用户名)从拦截器发送到我正在调用的每个操作或直接发送到 jsp?

如果我访问其他操作,我会得到 session null 谢谢。

以下是我在页眉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类:

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

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>

【问题讨论】:

    标签: jsp session struts2 interceptor struts2-interceptors


    【解决方案1】:

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

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

    【讨论】:

    • @Student 如果它也有效,则将其标记为已接受。我知道它也有效,因为它也适用于我。
    【解决方案2】:

    用户进入会话后,您可以通过以下方式访问该名称:

    #session.USER.name
    

    仅使用 name 仅在堆栈中有某些内容暴露 name 属性时才有效。您可能希望 (a) 检查用户是否在会话中,或者 (b) 为未登录的用户使用不同的模板。

    【讨论】:

    • 这对我有用 谢谢
    • @Student 可以简写为&lt;s:property value="#session.USER.name" /&gt;。您也许还可以使用 JSP EL,例如,${USER.name}
    猜你喜欢
    • 1970-01-01
    • 2016-06-25
    • 2011-04-07
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 2022-10-28
    • 2016-01-06
    • 1970-01-01
    相关资源
    最近更新 更多