문제

I am working on a struts2 project that has interdependent forms. I found struts2-conversation, stepped through their simple-example and understood the conversation mechanism this far (please correct me if I got something wrong):

  • The Controller is mapped in the struts.xml
  • It holds the serializable ConversationContext and the Storing-Service
  • The ConversationContext holds POJOs mapped on forms by naming convention

Now my question is where to put the validation?

In this structure the controller is only one extending ConversationSupport and thereby ActionSupport supplying the validate, prepare and addField- & ActionError methods. But validating within the controller would mean to validate the whole context, which does not really serve the issue.

I tried validation through annotation within the POJOs, within the context as described above which gives me some NullPointerException as if the context wasn't flushed and I think the xml-validation approach of struts2 is just too stiff. (btw how to let the generated javascripts be minified before being served? And why is there so many options?)

Mark's conversation-interceptor approach had similar problems coming up which's workarounds I didn't really get. Maybe you can help me there.

도움이 되었습니까?

해결책

If you would like to use annotations on your model classes, it works fine with the plugin (as do the other validation approaches).

To validate your model, add @VisitorFieldValidator to the getModel() method in your controller. In the example app, you would then also add @VisitorFieldValidator to the getContact() and getPreferences() methods. Then you can use the validation annotations on the fields you wish to validate.

The service in the example is just there as a simple example of using an injected service in a Struts2 controller and how that can integrate easily with the conversation framework, but it is not directly related or needed (and I would recommend using either Spring, Guice, or CDI for dependency injection in the real world).

The ConversationContext class is intended mostly for internal use by the framework. You should be able to avoid interacting with it by using the annotations and conventions. Unless you simply wish to be adventurous.

To use XML validation in the example app, you would have to change the package name to remove the "struts2" word in order for the Struts2 resource loading tool to load the XML.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top