3

I have an object that I'd like to map form values to. For example, lets just use this class:

public class Example
{
    public String x;
    public Integer y;
    public Boolean z;
}

For the purpose of the example, lets say these values are used in 2 different forms where only 2 of the members at one time are being populated (ie Form 1 uses x and y, but not z). Using the standard DataBinder, this will fail if either the Integer or the Boolean aren't populated by the form (null point exception). One option would be to populate the HTML form with placeholders, but if a larger number of members need to remain null then this becomes a bit of a headache. The other option I believe would be to create a custom DataBinder correct?

Following the information I found in the documentation, it looks fairly straightforward (http://www.playframework.com/documentation/2.1.x/JavaForms). However, I'm not sure where to register the formatter. Does it go in the controller? I saw some mention of putting it in Global (How to bind complex types in play-framework 2.0). Basically I'm looking for a better example of how to set up a formatter (that maps to something other than Strings) and use it. Can anyone point me in the right direction?

4

0 回答 0