2

Hi I have something like this in struts.xml

   <action name="LoginAction" class="controller.LoginAction">
                <result name="error">/Error.jsp</result>
                <result name="success">/Wizard.jsp</result>
    </action>

Edited:

Upon successful execution of execute in the action the next page has data which it accesses using the request scope. How can i store the same data in session scope instead?

I read that HttpServletRequest object is passed as a parameter to the execute() method in a Struts action, and I can always retrieve the HttpSession object by using the request.getSession() and attaching data to it.

So if I am using something like this in controller.LoginAction

public String execute(HttpServletRequest req) {
    ...
}

Do I have to change struts.xml? execute does not get called when HttpServletRequest is added as a parameter.

   <action name="LoginAction" class="controller.LoginAction">
                <result name="error">/Error.jsp</result>
                <result name="success">/Wizard.jsp</result>
    </action>

Also is using session this way the best method ? I am trying to familiarize my self with the struts method.

Sarin Jacob Sunny
  • 2,138
  • 3
  • 29
  • 61
MistyD
  • 16,373
  • 40
  • 138
  • 240

1 Answers1

3

You can implement SessionAware interface:

public class LoginAction extends ActionSupport implements SessionAware {
    private Map session;

    public String execute() {
        session.put("key", "value");
        // Plus any additional action code
    }

    // Plus setter (and optionally getter) for session map.
}

Alternate way using ActionContext is explained here.

SessionAware is preferred over ActionContext. Here are some discussions on that:

Community
  • 1
  • 1
Anupam
  • 7,966
  • 3
  • 41
  • 63