1

I have a simple application in struts 2 that allows the user to input the name and submitted.In the server side in struts 2 action class a new session is created and stored the value of username in the session.In the user.jsp I tried to display the value stored in session but it is not displaying anything.

The code for Index.jsp is given below

<body>
<form action="welcomeUser.action">
    <input type="text" name="user">
    <input type="submit" value="Submit">
</form>
</body>

The code in struts.xml is given below

<action name="welcomeUser" class="hart.test.Welcome">
                    <result name="success">user.jsp </result>
 </action>

The code in hart.test.Welcome is given below

public class Welcome extends ActionSupport
{
    private String user;
    public String getUser() {
        return user;
    }
    public void setUser(String user) {
        this.user = user;
    }
    public String execute()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session=request.getSession();
        //Checking session is new and creating a session
        if (session.isNew() == false) {
            session.invalidate();
            session = request.getSession(true);
            } 
        session.setAttribute("user1", user);

        return SUCCESS;
    }
}

The code in User.jsp is given below

<body>
    Welcomeee <s:property value="%{#session.user1}"/>
</body>

The problem is that it is displaying only 'Welcomeee'.Can anyone please tell the reason for it

  • When I use JSP expression tag(<%=session.getAttribute("user1") in User.jsp to display the value stored in session,it is found to be working.But using struts 2 tags it is not being displayed.Anyone please suggest the reason for it. – Kiranmuralee Sep 27 '14 at 12:36
  • 1
    Sometimes evaluating an expression seems obvious but the same expression might not work due to it has nothing to display. – Roman C Sep 28 '14 at 14:04

2 Answers2

0

Add Struts 2 taglib definition: <%@ taglib prefix="s" uri="/struts-tags" %> at the beginning of th User.jsp file.

Kamil Chaber
  • 568
  • 1
  • 10
  • 26
-1

Struts2 uses a Session Map, that is a wrapper over the underlying HttpSession. Use that by implementing the SessionAware Interface, like described in this answer:

public class Welcome extends ActionSupport implements SessionAware {

    private Map<String,Object> session;    
    public void setSession(Map<String,Object> session) {
        this.session = session;
    }

    private String user;
    public String getUser() {
        return user;
    }
    public void setUser(String user) {
        this.user = user;
    }

    public String execute() {
        session.put("user1", user);    
        return SUCCESS;
    }
}
Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • In my original code if I wanted to display value stored in session,then I should have declared session variable outside the execute method(With getters and setters) and in user.JSP I should have written Welcomeee .Please correct me if i'm wrong – Kiranmuralee Sep 28 '14 at 17:16