简体   繁体   English

Struts2 验证 - 验证失败时重新填充子项

[英]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 before the form is displayed. struts2-Action 有一个名为 edit() 的方法,该方法将在显示表单之前运行。 This method takes the provided id-parameter and retrieves the complete entity (including children) from the database.此方法采用提供的 id 参数并从数据库中检索完整的实体(包括子实体)。 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).我可能会通过使用 ajax 来避免这个问题(即将动作分成更小的部分,因为一项服务似乎与另一项服务无关,即显示孩子不需要与更新实体相关联)。

But that is a lot of work and particularly so if you don't use ajax.但这是很多工作,尤其是如果您不使用 ajax。 What is really easy is implementing Validateable (or if you extend ActionSupport it already does that) and then adding/overriding the validate method.真正简单的是实现 Validateable(或者如果您扩展 ActionSupport 它已经这样做了),然后添加/覆盖 validate 方法。 If you use xml for validation don't worry both are run.如果您使用 xml 进行验证,请不要担心两者都会运行。

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).然后创建一个 setup 方法来填充您需要的字段,并将其放置在 validate 方法中的正确位置(可能是第一行)。 Since this will always run before execute, it will probably reduce the size of the execute method.由于这总是在执行之前运行,它可能会减少执行方法的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM