如何使用Struts 2在JSP中打印会话属性 [英] How to print session attributes in JSP with Struts 2

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

问题描述

这就是我所拥有的:

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 .

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.

可能是什么原因?

推荐答案

实际上,第一种情况和第二种情况都不显示来自会话的消息.在值堆栈中查找变量.

Really neither first nor second case is displaying a message from session. It's looking a variable in the value stack.

在第一种情况下,您具有操作属性getter,它返回一个值.在第二种情况下,该值可能不相同.

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.

然后使用该映射而不是http会话.请参见我们如何获得对会话的访问权限.

Then use that map instead of http session. See How do we get access to the session.

public String addUser() throws NoSuchAlgorithmException {    
    Map currentSession = ActionContext.getContext().getSession();
    User u = new User();
    u.setUname(getUserName());
    u.setPassword(StringHash(getUserPass()));
    u.setUtype(getUserType());
    plResponse = iUserDAO.addUser(u);
    setActionMessagee(plResponse.getMessage(currentSession.put("actionMessage",getActionMessagee()));
    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>

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

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