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

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

问题描述

这是我所拥有的:

Java 类(添加用户):

public String addUser() 抛出 NoSuchAlgorithmException {HttpSession currentSession = request.getSession();用户 u = 新用户();u.setUname(getUserName());u.setPassword(StringHash(getUserPass()));u.setUtype(getUserType());plResponse = iUserDAO.addUser(u);setActionMessage(plResponse.getMessage());currentSession.setAttribute(actionMessage", this.actionMessage);返回成功;}

Java 类(添加关联):

public String saveAssoc() 抛出异常 {HttpSession currentSession = request.getSession();尝试 {plResponse = iUserDAO.saveUserAssoc(currentSession.getAttribute("selectedUser").toString(), countryId, langId);refreshUserAssociations();setActionMessage(plResponse.getMessage());currentSession.setAttribute(actionMessage", this.actionMessage);}捕获(异常 e){抛出新异常(e.getMessage());}返回成功;}

JSP(两种情况相同):

<div class="successMessage"><br/><s:property value=actionMessage"/>

<br/></s:if>

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

可能是什么原因?

解决方案

实际上第一种和第二种情况都没有显示来自会话的消息.它正在寻找值堆栈中的一个变量.

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

在动作类中使用会话的正确方法是实现通过servletConfiginterceptor 一个会话映射到 action bean 属性.

然后使用该映射而不是 http 会话.请参阅我们如何访问会话.

public String addUser() 抛出 NoSuchAlgorithmException {映射 currentSession = ActionContext.getContext().getSession();用户 u = 新用户();u.setUname(getUserName());u.setPassword(StringHash(getUserPass()));u.setUtype(getUserType());plResponse = iUserDAO.addUser(u);setActionMessagee(plResponse.getMessage(currentSession.put("actionMessage",getActionMessagee()));返回成功;}

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

<div class="successMessage"><br/><s:property value="#session.actionMessage"/>

<br/></s:if>

Here is what I have:

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 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 (for both cases the same):

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

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.

What may be the reason?

解决方案

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 {    
    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; 
}

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

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆