-2

How to pass Action class variable value into another Action class in Struts 2?

I wanted to use that retrieved in the query variable in another action class.

Roman C
  • 49,761
  • 33
  • 66
  • 176
user
  • 29
  • 1
  • 4
  • i wanted that in struts2 – user Jun 13 '13 at 13:09
  • What's the use case? How are you connecting the two actions? – Dave Newton Jun 13 '13 at 13:22
  • @Dave Newton i don't know about use case. i declared three parameters in LoginAction class and i was inputting values for the three parameters through jsp page. now i want to retrieve one of the three values in to InboxAction class. if you want i can post my code also. – user Jun 13 '13 at 13:34
  • @user Why don't you pass them with the result of the first action? – Roman C Jun 13 '13 at 13:37
  • please help me. i am a newbie to struts2. @Dave Newton – user Jun 13 '13 at 13:38
  • Tell me how? give a simple example. @Roman – user Jun 13 '13 at 13:43
  • @user What is the logic? login action return jsp then you inputed three values then submit to inbox action. the values you submit should be also there as properties of the action. Then this properties or fields are passed as parameters to the action that you submit. – Roman C Jun 13 '13 at 13:53
  • i used this statement. LoginAction Email=new LoginAction(); in InboxAction class. and when i print Email.getEmail() in InboxAction class, it is printing null. what is the problem. help me. – user Jun 13 '13 at 14:03
  • i am creating inbox module. that is, when u click on the inbox link, it should directly retrieve values, that is mails from database for that logged in user.so i need to retrieve the email parameter from the LoginAction class, becuase i should not again create jsp page to enter my mail id to retrieve the mails after i logged in ok. i am not good at enlish. please forgive if any mistake is there in this. – user Jun 13 '13 at 14:08
  • You should write Email.setEmail(LoginAction action){this.email = action;} then when you use Email.getEmail() {return email;} – Roman C Jun 13 '13 at 14:08
  • 2
    Why are you directly instantiating an action inside another action? There are multiple reasons why this is not a good idea. – Dave Newton Jun 13 '13 at 14:30
  • @user: What are you trying to achieve? – Aleksandr M Jun 13 '13 at 19:37
  • @Roman, The logic is i used three properties in LoginAction class named email, username, password and i am checking with the database values. after this i need the email property value to be retrieved in to the InboxAction Class. how can i do this. – user Jun 14 '13 at 07:14

3 Answers3

2

There are different ways the actions could communicate with each other as well as they running in different threads and don't share the action context. The most popular way is to pass parameters with the request or in the URL and XWork converter will convert them to the action properties with the help of OGNL.

But I think the purpose of the LoginAction is to authenticate the user by their credentials (email, username, password) and save this information in the session map. It is a common resource that could be shared between actions. To get the session map available to the action and other actions they should implement the SessionAware. It will help the Struts to inject the session map to the action property. If you want to use the session in many actions over the application then to not implement this interface in every action you could create a base action.

public class BaseAction extends ActionSupport implements SessionAware {

  private Map<String, Object> session;

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

  public Map<String, Object> getSession(){
    return session;
  }
}

then actions will extend the base action to get the session compability.

public class LoginAction extends BaseAction {

  @Override
  public String execute() throws Exception {
    User user = getUserService().findBy(username, email, password);
    getSession().put("user", user);

    return SUCCESS;
  }

}

Now the user in the session and you could get the session from other action or JSP and user object from the session map.

public class InboxAction extends BaseAction {

  @Override
  public String execute() throws Exception {
    User user = (User) getSession().get("user");

    return SUCCESS;
  }

} 
Roman C
  • 49,761
  • 33
  • 66
  • 176
0

Try this : use result type chain in the first action.

and add the name of the second action as a value to the result of the first action

link leads to the official page of the struts2 for the chain result

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Kartik73
  • 503
  • 6
  • 19
0

If you want to post all values to another action use 'chain', other wise use redirect-action and specify the parameters.

Jegatheesh M
  • 68
  • 2
  • 8