views:

550

answers:

2

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?

A: 

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

}
VinAy
A: 

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");
Todd Owen

related questions