DIY Form Processing, PRG, Inspection

I would like to implement form processing for web applications. I would like to implement PRG (post-redirect-get), as I believe this is the way to work with the form (plays very well with the reload and back button). However, I can see that this makes it harder to check.

Basically, when you submit a message, you have the state of the form (as post parameters). However, if you validate and redirect back to the form, you lose your form state (your redirect is GET, you can encode all form parameters in GET, but that is not always possible due to url length restrictions. Ugly as hell) ...

I am thinking about the following:

  • The user is in the form ( /addUserDetails.html

  • POST to action url ( /addUser

  • /addUser

    performs the appropriate action. If it fails, it saves the state of the form, assigns it an ID, and redirects it to the form view with the ID as a get ( /addUserDetails.html?state=2313ab2

    ) parameter
  • The view checks the status and displays the appropriate information.

Depending on how you save the state of the form, you can also allow the user to continue working on the form even if their browser explodes or something goes wrong.

Thoughts? Also, is there any Java web framework that does this or can be coerced into doing it?


source to share

2 answers

You don't need to store all the state in the redirect url. I did it a little differently than your approach, and it works great for our use.

I don't redirect if the form fails validation. I just re-fill the form with the entered values ​​with the highlighted errors, exactly what you would do without the PRG. Only when POST is successful do I redirect the browser to the GET page. In your case, you only need to provide the username in the url so that you can display a personalized welcome page.

Yes, we will get double feed in bad forms, but this turns out to be more desirable for our usecase. When the user navigates to the form, they get the error again and resume their work. Due to validation, no double commit is allowed, which we try to avoid.



Try Wicket - it does this by default.



All Articles