Struts2 Validation - Repopulate Children when validation fails
Its late and im tired, but this problem is bugging me like crazy.
I have a form. This form has some input fields that maps to a entity and will be persisted when submitted. On the same page (but not a part of the form) I have an overview of this entity's children.
The struts2-Action has a method called edit() that will be run 开发者_Go百科before the form is displayed. This method takes the provided id-parameter and retrieves the complete entity (including children) from the database. So the form is then displayed nicely with children information.
However. When validation (serverside) has an error. The entity-object does no longer have children. All information (except what was in the form-fields itself) seems to have disappered.
What should i do to still see my children even after validation fails?
It's late and I'm tired too but off the top of my head:
I would have avoided the issue probably by using ajax (that is make the action into smaller parts, since one service seems to be unrelated to the other, that is displaying the children need not be tied to updating the entity).
But that is a lot of work and particularly so if you don't use ajax. What is really easy is implementing Validateable (or if you extend ActionSupport it already does that) and then adding/overriding the validate method. If you use xml for validation don't worry both are run.
Then create a setup method to populate the fields you need, and place it at the right location in the validate method (probably the first line). Since this will always run before execute, it will probably reduce the size of the execute method.
精彩评论