-
1. Re: Model annotation
gavin.king May 19, 2009 7:12 AM (in response to gonorrhea)The @Model annotation does to things:
- it makes the object @RequestScoped, instead of @Dependent
- it gives the object an EL name
So it's not intended to be used for entities, rather for things that contain application/business logic.
-
2. Re: Model annotation
sboscarine Jul 31, 2010 7:45 PM (in response to gonorrhea)Where can I find more information about @Model? From viewing the examples, I have an idea of where to put the annotation, and an idea of why I should do so, and what will happen. However, the JavaDocs contain literally 1 sentence:
The built-in stereotype intended for use with beans that define the model layer of an MVC web application architecture such as JSF.
This post was all I could find in the first 100 or so results from Google.
It appears Gavin provided great info, however where should I go to find this level of detail in the future? I want to be sure I fully understand this annotation as well as others I come across down the road. Is there a user guide somewhere?
The questions that come to my mind when I view an annotation (or really any chunk of code) for the first time are:
- What does this do?
- When can it be used?
- Why?...How will it's use benefit my application? What will I miss out (or what may go wrong) on if I don't use it?
In looking at the JavaDocs, Google Search, and JSR 299, but I couldn't find the answers to those questions. Did I miss them somewhere?
-
3. Re: Model annotation
nickarls Aug 1, 2010 5:03 PM (in response to gonorrhea)The Welf refguide provides the following hints
Notice the controller bean is request-scoped and named. Since this combination is so common in web applications, there's a built-in annotation for it in CDI that we could have used as a shorthand. When the (stereotype) annotation @Model is declared on a class, it creates a request-scoped and named bean.and
If you need to access a bean directly by EL name in a JSF page, you probably need to give it a scope other than @Dependent. Otherwise, any value that gets set to the bean by a JSF input will be lost immediately. That's why CDI features the @Model stereotype; it lets you give a bean a name, and set its scope to @RequestScoped in one stroke. If you need to access a bean that really has to have the scope @Dependent from a JSF page, inject it into a different bean, and expose it to EL via a getter method.and finally
CDI defines one further standard stereotype, @Model, which is expected to be used frequently in web applications:@Named @RequestScoped @Stereotype @Target({TYPE, METHOD}) @Retention(RUNTIME) public @interface Model {}
Instead of using JSF managed beans, just annotate a bean @Model, and use it directly in your JSF view! -
4. Re: Model annotation
sboscarine Aug 1, 2010 5:15 PM (in response to gonorrhea)The 1.0.1 Reference Guide does explain things a lot better. I was viewing the 1.0 Guide (I Googled the class name and it came up)...my mistake The 1.0.1 does a much better job explaining this.
Thank you!
-