5 Replies Latest reply on Aug 3, 2010 6:57 PM by dan.j.allen

    Model annotation

    gonorrhea

      Regarding 2.7.4. Built-in stereotypes of 299, I'm confused as to what the need is for a @javax.enterprise.inject.Model stereotype.



      The built-in stereotype @javax.enterprise.inject.Model is intended for use with beans that define the model layer of an
      MVC web application architecture such as JSF

      so does this mean you'd use this if you're not using JPA entity class as a model?  B/c if you are using JPA, wouldn't you just use @Entity instead?

        • 1. Re: Model annotation
          gavin.king

          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

            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:



            1. What does this do?

            2. When can it be used?

            3. 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

              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

                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!

                • 5. Re: Model annotation
                  dan.j.allen

                  I created an FAQ using information discussed in this thread.


                  wiki://135152