Spring Form Command Assignment

The spring form controller (e.g. SimpleFormController or BaseCommandController ) uses commands to transfer data between the HTML form and the controller. My question is, is it common practice to use the support model as a team itself? Or, as a rule, a separate command is created with the appropriate attributes for those that are in the swap model.

My problem is that to use the support model as a command, property editors are needed to convert non-string attributes. Imagine a data model with many non-line strongly typed custom field types. When submitting a form, the property editor performs the conversion before invoking the validator. If type conversion is not possible (user input error), then the validator will never be able to provide a detailed error message. Everything that appears in HTML form is a generic error message. See my question https://stackoverflow.com/a/3188141/

An alternative is to create a separate command that duplicates each field in the backup model, but as a string. Thus, the validator can check the string representation of each field. The onSubmit controller is onSubmit responsible for converting the text command to the backup model. From my Spring research, this seems to be intended to be used. My desire to go this route is a cumbersome way that you need to create a separate command for each data model. Then added extra work that should march between the team and the data model. It is much more convenient to have a form directly editing the support model, and use the property editor to convert. Then the problem is validation.

So, I'm curious how others approach the editing problem based on model forms that contain custom non-line fields.

+4
source share
2 answers

I would recommend you to study Spring API bindings and checks . Bind form elements to objects that need a service level, and pass them to the controller.

My preference is to bind directly to business objects and not create a DTO just for the sake of the web tier. I do not like parallel hierarchies.

+3
source

IMHO it comes down to how you want to create your domain classes. I prefer the design of `em qiute strict, not even allowing you to set inappropriate values, etc. This doesn't go well with the way Spring handles binding and validation.

As I want to avoid weakening my domain model, I tend to use DTOs as command objects, since usually a view gets a slightly different view of domain objects. A classic example is a user domain class that contains a password. At the presentation level, you usually want the actual user to enter the password twice and compare these values ​​in the verification step. Only if they match correctly will the data be bound to the domain class.

It may seem like a bit of overhead, but it allows you to cleanly separate the domain / application level from the presentation.

+2
source

All Articles