java – 如何在jsp(struts)中打印会话属性

这是我有的:

Java类(添加用户):

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类(添加关联):

    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(两种情况相同):

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

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

可能是什么原因?

实际上,第一种或第二种情况都不是从会话中显示消息.它正在寻找值堆栈中的变量.在第一种情况下,您具有返回值的action属性getter.在第二种情况下,它可能不是相同的值.在动作类中使用会话的正确方法是实现SessionAware,它通过servletConfig拦截器向会话映射注入动作bean属性.然后使用该映射而不是http会话.见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中,您可以从上下文中访问会话对象.

<s:if test="#session.actionMessage != null && #session.actionMessage != ''">
    <div class="successMessage">
    <br/><s:property value="#session.actionMessage"/>
    </div>
    <br />
</s:if>
https://stackoverflow.com/questions/24783219/how-to-print-session-attributes-in-jsp-struts

转载注明原文:java – 如何在jsp(struts)中打印会话属性