2

I am migrating a project from struts 1.3 to struts 2.0. Now I have read through various resources available on web but could not find a struts 1.3 reset() form method alternative in struts 2.0.

My action class extends ActionSupport and implements Modeldriven. I have implemented the validate method in action class and that works perfect. However, when the form has no errors and the submission is done properly, the page reloads with initial values.

I expected that I will receive a blank form. I looked for a reset() form method but could find none. Currently, I am explicitly setting all form values to blank. I don't see this as a good programming practice. Please suggest how to implement form reset() in struts 2.0.

AL.
  • 36,815
  • 10
  • 142
  • 281
Sushant Gupta
  • 1,487
  • 2
  • 16
  • 23

2 Answers2

1

EDIT: ok, you are working with Tiles (that I've never used), then I'll try another solution:

if you have an empty JSP tile, and after the user compiled its fields and submitted the form, you want to ALWAYS clear those fields... what about declaring only SETTERS in the Action, and not the GETTERS ?

You could only set from JSP to Action, but not read them from the JSP... then your fields will be always empty in the page, and always rewritten by setters in the Action.


If i got it, you are on JSP, you call the method saveStuff() of the Action (or of another action), then you come back to the page...

in this case, you can use a RedirectAction return type, that will strip all params from the request, and redirecting your request to the execute() method of your Action.

So, instead of

<action name="myAction" class="com.blabla.myAction">
        <result name="success">/myPage.jsp</result>
</action>

just do

<action name="myAction" class="com.blabla.myAction">
      <result name="success">/myPage.jsp</result>
      <result name="stuffSaved" type="redirectAction">
          <param name="actionName">myAction</param>
      </result>
</action>

This way you re-enter in the initial state (assuming you are not putting anything in session that is read by the JSP).

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • ok, thats good, but i cannot do type="redirectAction", i am working with tiles, i put type="tiles" – Sushant Gupta Nov 06 '12 at 14:59
  • Yes you can do a redirectAction, that is a struts2 result type! Your redirect would point to another action which would then render using tiles. BTW I have not considered the value of this approach just clearing up that it is something you can easily do while using apache tiles. – Quaternion Nov 07 '12 at 00:40
0

Because your Action and Form are in the same class in Struts2, you have to implement your own reset() method to clear any data that you would like.

doublesharp
  • 26,888
  • 6
  • 52
  • 73
  • Yes, that is what I am doing currently. When I make a successful commit to database, i call my custom reset() method and explicitly set every form value to empty. Do i infer this as the only available solution in my case..?? – Sushant Gupta Nov 06 '12 at 07:54
  • 2
    @SushantGupta Something is wrong: actions should be created per-request; you should never need to reset action properties. If you're using the Spring plugin, make sure your actions are "prototype" scope. – Dave Newton Nov 06 '12 at 11:18