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 pa开发者_开发百科ss values using SessionAware interface?
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;
}
}
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");
精彩评论