1

Let be my custom type (note that there is NO setters because this is an Immutable Value Object):

class CustomType extends ValueObject {

  private String value;

  @NotNull
  public String getValue();

  CustomType(String value) { this.value = value;}
  CustomType(String prefix, String suffix) { this.value = prefix + suffix;}

  public String getPrefix() { return value.substring(0, 4);}
  public String getSuffix() { return value.substring(4);}
}

and my controller:

@Controller
class MyController {
  public ModelAndView processSubmittedForm(@Valid CustomType myObject, BindingResult result) {..}
}

and my jsp view form:

<form>
    <input id="prefixField" name="prefix" value="756." type="hidden">
    <input id="suffixField" name="suffix" type="text">
...
<form/>

Considering that this view will send two POST parameters prefix and suffix, what do I need to do in order to have these two parameters be bound with the single object myObject, assuming that it will be validated by Spring with a not-null value ?

Can I achieve this by customizing WebDataBinder, InitBinder, by registering a Formatter or by Custom Editor or whatever ?

Thank you very much for your help.

EDIT: Here are the related articles I have googled without finding one solution that matches my own issue :

Community
  • 1
  • 1
fabien7474
  • 16,300
  • 22
  • 96
  • 124
  • This should just work as it is - I think you just to need to provide getter and setter for your `customType` – Biju Kunjummen Apr 03 '13 at 15:29
  • @Biju Thx. But that is the point: I don't have setters for prefix and suffix since this is an Immutable Value Object. The only thing I have is a constructor for creating an instance of CustomType – fabien7474 Apr 03 '13 at 15:45

2 Answers2

1

@fabien7474, you can use the PropertiesEditor, because if we look a little to the HTTP protocol, all the parameters in the request is String, and when you need to perform some type conversion or validation the spring gives you a method of initializing binder.

Example:

@Controller
class MyController {
    public ModelAndView processSubmittedForm(@Valid MyObject myObject, BindingResult result) {..}

    @InitBinder
    public void initBinder (WebDataBinder binder) {
         binder.registerCustomEditor(MyObject.class, new CustomObjectEditor());
    }

}


class CustomObjectEditor {
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
            MyObject ob = new MyObject();
            CustomType ct = new CustomType();
            ct.setValue(text);
            ob.setCustomType(ct);

            super.setValue(ob);

    }
}

With this example you can see a type of conversation. Hope this helps you.

Tiarê Balbi
  • 1,480
  • 1
  • 23
  • 32
  • Hi Tiarê: This is not gonna work because setAsText accept one single string. The parameters sent by HTTP come in the form of : customType.prefix=756.&customType.suffix=29389&name=foo – fabien7474 Apr 03 '13 at 15:03
  • When you need get params in the URL like ?param1=info&param2=info3 you will need to use @RequestParam() remembering you can define required=false, and you can use the same example above, because he considers the type of object to be converted, and when the type of object is the same registred in the binder, spring will convert the information for you. – Tiarê Balbi Apr 03 '13 at 17:09
0
<form>
<input id="prefixField" name="prefix" value="756." type="hidden">
<input id="suffixField" name="prefix" type="text">

...

Declare prefix field in your object(i.e commandName).
  • Thank you for answering a question. Please add an explanation to make the answer more useful for others. – namezero Jul 16 '15 at 11:33