Suggested Feature

72 views
Skip to first unread message

idiotsavant

unread,
Aug 30, 2011, 3:06:34 PM8/30/11
to play-framework
One of the features of Spring that would be great to see implemented
in Play! is annotated-driven formatting (perhaps under
play.data.formatting). This would provide tag-like functionality when,
say, binding model data submitted via an HTML form. Then developers
could create custom formatting annotations to suit their needs.

Gaëtan Renaudeau

unread,
Aug 30, 2011, 3:44:03 PM8/30/11
to play-fr...@googlegroups.com
binding model data submitted via an HTML form.


2011/8/30 idiotsavant <scott...@mckesson.com>

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.




--
---------------------------------------------------
http://twitter.com/greweb - http://blog.greweb.fr - http://gaetanrenaudeau.fr

idiotsavant

unread,
Aug 31, 2011, 7:49:12 AM8/31/11
to play-framework
I looked at that, but it appears to apply only to method parameters. I
was thinking more of field annotations in a model class.

On Aug 30, 3:44 pm, Gaëtan Renaudeau <renaudeau.gae...@gmail.com>
wrote:
> > binding model data submitted via an HTML form.
>
> You mean thathttp://www.playframework.org/documentation/1.2.3/controllers#binding?
>
> 2011/8/30 idiotsavant <scott.he...@mckesson.com>

idiotsavant

unread,
Aug 31, 2011, 9:05:19 AM8/31/11
to play-framework
That is what the documentation implies for custom bindings:

The @As annotation also allows you to define a completely custom
binder. A custom binder is subclass of TypeBinder that you define in
your project...You can use it in any action...

And that is what my tests confirm. What is a little puzzling is that
in the section above the one from which I just quoted, one sees this:

public class User extends Model {
@NoBinding("profile") public boolean isAdmin;
@As("dd, MM yyyy") Date birthDate;
public String name;
}

Which suggests that the @As annotation can be used at the field level.
But when I stepped through a model save operation using a custom @As
binding, the custom binder was never triggered.

If I am missing something, please let me know. If a regular (as
opposed to custom) @As annotation can indeed be applied to a model
field, when is the formatting applied - during a model save?
> > ---------------------------------------------------http://twitter.com/greweb-http://blog.greweb.fr-http://gaetanrenaudea...

Nicolas Leroux

unread,
Aug 31, 2011, 9:52:31 AM8/31/11
to play-fr...@googlegroups.com

On Aug 31, 2011, at 3:05 PM, idiotsavant wrote:

> That is what the documentation implies for custom bindings:
>
> The @As annotation also allows you to define a completely custom
> binder. A custom binder is subclass of TypeBinder that you define in
> your project...You can use it in any action...
>
> And that is what my tests confirm. What is a little puzzling is that
> in the section above the one from which I just quoted, one sees this:
>
> public class User extends Model {
> @NoBinding("profile") public boolean isAdmin;
> @As("dd, MM yyyy") Date birthDate;
> public String name;
> }
>
> Which suggests that the @As annotation can be used at the field level.
> But when I stepped through a model save operation using a custom @As
> binding, the custom binder was never triggered.
>
> If I am missing something, please let me know. If a regular (as
> opposed to custom) @As annotation can indeed be applied to a model
> field, when is the formatting applied - during a model save?
>

It is applied during the binding phase ie when binding http parameter to Java object.

For example

?user.birthdate=22,%2009%201980&user.name=bob

with a controller class

public static void profile(User user) {
// User is now an object with name=bob and birthDate=Date(22nd september 1980)
}

Does that answer your question?

Nicolas

idiotsavant

unread,
Aug 31, 2011, 10:32:20 AM8/31/11
to play-framework
Yes, thanks. I guess the more I think about it, what I really would
like to see is for the Play! framework to generate a custom setter
when a custom annotation is applied to, say, a model field. Why?
Because I am helping to develop a Web application that accepts both
HTML and JSON media HTTP requests. The later is converted to a class
using Gson, hence form parameters never enter the picture - as
follows:

HTML: model.property = form string parameter (after being converted
and/ or formatted)
JSON: model.property = json_string_to_class.property (after being
formatted)

So, instead of using custom utility methods to format the incoming
data before it is set to model fields, it would be really cool to
simply add an annotation to applicable fields that generate non-
default setters to implement whatever bit of formatting needs to be
done (coded perhaps in special class Play! pulls its implementation
from) when values are set in the bean.

Does anyone think this would be a bad idea?

idiotsavant

unread,
Sep 1, 2011, 12:27:19 PM9/1/11
to play-framework
Alright, I created a module to replace the setters for custom
annotated model fields using javassist and Play's Enhancer. It seems
to do just what i wanted. A question I have for Play developers is
this: is it safe to assume that automatically generated setters will
always be plain vanilla setters (that is, a public void method that
only sets the class property to the value of the sole argument -
this.property = argument)? Or should an interceptor or wrapper be used
to preserve the original auto-generated setter (instead of simply
replacing its method body)?

Thanks.

Julien Richard-Foy

unread,
Sep 1, 2011, 3:16:29 PM9/1/11
to play-fr...@googlegroups.com
Developpers are free to define their own setters. The default setter
is generated only if there is no one already defined.
Reply all
Reply to author
Forward
0 new messages