views:

13

answers:

1

I am rendering a form to present an annotated domain model class, i.e.:

@Entity
class User {
    @NotNull
    private String name;
    // other code
}

I would like to read the annotated meta-data to decide how to render each property.

I would use it to add css classes to my input tags and finally use jQuery to attach validation methods on such fields.

Does freemarker offer any way to access the annotated meta-data?

+1  A: 

There may indeed be a way to do it directly, but I've gradually come to the conclusion it's simpler to do extra work in my action code to provide easier "things" for the template to consume, rather than trying to co-erce Freemark to jump through hoops.

For example, I'll create a list of booleans, one per item I'm displaying, indicating, say, whether the item has already been purchased. Freemarker is quite good at traversing parallel lists.

Rodney Gitzel
I think there should be an "annotation-aware" TemplateModel that exposes built-ins like _user.name?hasAnnotation('NotNull')_ . That would keep the controller freemakrer-agnostic (it just return the domain-object) and will keep the freemarker style for exposing the data in the template, wouldn't it? But I don't know now how to implement such feature.
Xan
That would be nice, certainly, but... you have to weigh the cost of figuring out how to implement it vs. your controller not being agnostic. IMO the controller *should* know about the view, so I don't see much value in the latter. Then again, how often would you need this? If a lot, then it may be worth the cost to implementing your annotation idea.
Rodney Gitzel