Newbie
Newbie

Reputation: 11

Struts 2 session values

I need to pass some field values from one jsp to another jsp using Struts2 and action classes. Can any one suggest me the best way to do it. How to pass values using SessionAware interface?

Upvotes: 1

Views: 11314

Answers (2)

Todd Owen
Todd Owen

Reputation: 16228

If you implement SessionAware then your actions will receive a Map containing the session variables. If one action puts a value into the map:

session.put("username", "Newbie");

then later actions can retrieve that value from the map:

String username = session.get("username");

Upvotes: 0

Vinay Lodha
Vinay Lodha

Reputation: 2223

Implement SessionAware interface and unimplemented methods. After this you just need to add parameter in Map. The Map will contain all session variable vales as Key value pair. you can add, remove values from Map.

Here is a Example of Action Class

public class SampleForm implements SessionAware{
  //Fields that hold data
  private String Welcome1="";
  // This Map will contain vales in Session
  private Map session;

  public String execute() throws Exception {
        return SUCCESS;
  }
  public void setWelcome1(String s) {
    this.Welcome1= s;
  }
  public String getWelcome1() {
    return Welcome1;
  }
  public void setSession(Map session) {
    this.session = session;
  }

  public Map getSession() {
    return session;
  }

}

Upvotes: 2

Related Questions