0

I'm new to struts and I'm migrating a project form struts-1 to struts-2 where multiple methods are being called in same action class from the same jsp. The UI has lot of elements which were dynamically loaded based on the prev values. They're going to the action(method) using onclick and

form.action=/App/action?method=methName
form.submit()

in struts-1 which I've changed to

form.action=/App/actionNameChangeField
form.submit()

on field change to load the field elements below.

I've written separate actions to the methods like this

<action name="actionNameChangeField" class="com.org.RequestAction" method="changeField">
    <result name="createRequest" type="tiles">page.createRequest</result>           
</action>

But when the request comes, I think a new RequestAction object is being created because all the fields are reset/reinitialized and data from previous request is lost. This doesn't seem to be a issue when it goes to a different action class.

Is there any way to persist the field data when the action is invoked on the same action class?

chaitunakka
  • 11
  • 1
  • 6
  • Keep the information in session. In S2 actions are created per-request as stated in the docs; it's irrelevant if it's the same or a different action class. – Dave Newton Jun 24 '19 at 12:32
  • @DaveNewton there are more than 20 variables used in the jsp from the action. Is there no other better way? – chaitunakka Jun 24 '19 at 13:40
  • Not really--that's (one reason) why wrapping up stuff like this into simple DTOs is usually a good idea; this makes refactoring/etc. easier later. There are various games one could play with interceptors that look at an action's properties and automagically put them into session, but that's a slippery slope that could lead to problems later. – Dave Newton Jun 24 '19 at 14:20
  • Did you try this https://stackoverflow.com/a/23106806/573032? – Roman C Jul 14 '19 at 22:07
  • Like @DaveNewton said we can wrap all the fields in a DTO and maintain in request or session scope. Struts calls a new instance of the action class every time even if the call is from the same page. – chaitunakka Aug 25 '19 at 03:57

0 Answers0