如何在jsp(struts)中打印会话属性 [英] How to print session attributes in jsp (struts)

查看:151
本文介绍了如何在jsp(struts)中打印会话属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的内容:

Java类(添加用户):

Java class (adding user):

public String addUser() throws NoSuchAlgorithmException {
    HttpSession currentSession = request.getSession();

        User u = new User();
        u.setUname(getUserName());
        u.setPassword(StringHash(getUserPass()));
        u.setUtype(getUserType());
        plResponse = iUserDAO.addUser(u);

        setActionMessage(plResponse.getMessage());
        currentSession.setAttribute("actionMessage", this.actionMessage);

        return SUCCESS; 
    }

Java类(添加关联):

Java class (adding associations):

    public String saveAssoc() throws Exception {
    HttpSession currentSession = request.getSession();

    try {
        plResponse = iUserDAO.saveUserAssoc(currentSession.getAttribute("selectedUser").toString(), countryId, langId);

        refreshUserAssociations();            

        setActionMessage(plResponse.getMessage());
        currentSession.setAttribute("actionMessage", this.actionMessage);
    }
    catch (Exception e) {
        throw new Exception(e.getMessage());
    }
    return SUCCESS;
}

JSP(两种情况相同):

JSP (for both cases the same):

<s:if test="actionMessage != null && actionMessage != ''">
    <div class="successMessage">
    <br/><s:property value="actionMessage"/>
    </div>
    <br />
</s:if>

我有两种情况将返回消息传递给页面。添加用户后,添加用户关联后。在这两种情况下,参数都正确地传递给会话(我对代码进行了调试),但它仅在第一种情况下显示(添加用户)。第二种情况假装在会话中没有actionMessage。

I have two cases of passing the return message to the page. After adding a user, and after adding user associations. In both cases the parameter is passed properly to session (I debuged the code), but it is being displayed only in the first case (adding user). The second case pretends like there is no actionMessage in session.

可能是什么原因?

推荐答案

实际上,第一种或第二种情况都不是从会话中显示消息。它正在寻找值堆栈中的变量。在第一种情况下,您具有返回值的action属性getter。在第二种情况下,它可能不是相同的值。在动作类中使用会话的正确方法是实现 SessionAware ,它通过 servletConfig 拦截一个会话映射到动作bean属性。然后使用该映射而不是http会话。请参见如何做我们可以访问会话

Really neither first nor second case is displaying a message from session. It's looking a variable in the value stack. In the first case you have the action property getter which returns a value. It could be not the same value in the second case. The right way to use a session in action class is to implement SessionAware that injects via servletConfig interceptor a session map to the action bean property. Then use that map instead of http session. See How do we get access to the session.

public String addUser() throws NoSuchAlgorithmException {
        HttpSession currentSession = request.getSession();
        Map currentSession = ActionContext.getContext().getSession();
        User u = new User();
        u.setUname(getUserName());
        u.setPassword(StringHash(getUserPass()));
        u.setUtype(getUserType());
        plResponse = iUserDAO.addUser(u);
        setActionMessage(plResponse.getMessage());
        currentSession.setAttribute("actionMessage", this.actionMessage);
        currentSession.put("actionMessage", getActionMessage());
        return SUCCESS; 
    }


在JSP中,您可以从上下文中访问会话对象。

In the JSP you can access the session object from the context.

<s:if test="#session.actionMessage != null && #session.actionMessage != ''">
    <div class="successMessage">
    <br/><s:property value="#session.actionMessage"/>
    </div>
    <br />
</s:if>

这篇关于如何在jsp(struts)中打印会话属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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