How to print session attributes in jsp (struts)

前提是你 提交于 2020-01-27 08:31:26

问题


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?


回答1:


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



回答2:


To access attribute stored in the session use the following code:

<s:property value="%{#session.actionMessage}" />

Check Accessing scoped objects in property tag page to see some more details.



来源:https://stackoverflow.com/questions/24783219/how-to-print-session-attributes-in-jsp-struts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!