0 Replies Latest reply on Aug 16, 2010 6:51 AM by nschweig

    JSF Best practice especially design managed beans

    nschweig

      Hi,

       

      I have got questions regarding best-practices in JSF especially managed beans.

       

      1.) Where an I find best practices for JSF? Do "official" best-pracices exist?
      I googled a lot and only found this one:
      http://venkatachalam.wikidot.com/javaee
      But this is only a collection from one single developer. I agree in some parts of his thoughts but no in all.

       

      2.) Regarding managed beans:
      Is it always the best way to have one bean for every page?  I think it could be different from project to project. If you have the  requirement that the software should be scalable and extendable, of  course, creating one bean per page should be the best way.But in the  other way it could be that you create a lot of redundance by creating  one bean per page. (For example: If you have some very similar pages)
      In this case, an approach which I read in the following article could be a solution:

       

      http://blog.icefaces.org/blojsom/blog/default/2009/04/23/Making-distinctions-between-different-kinds-of-JSF-managed-beans/

       

      The article supposes to divide the managed beans in 5 kinds of beans.
      - model managed-beans     (model = data)
      - backing managed-beans (represents the page)
      - controller managed-beans (executes business logic and returns a navigation outcome)
      - support managed-beans (supports" one or more views --> typical use case is supplying an ArrayList<SelectItem> )
      - utility managed-beans (utility function like Fileupload for example)

       

      How do you design your managed-beans?

       

      3) Another thing is: What is the best way to get values into a bean. Putting or getting?

       

      Miniwebshop for example:
      The user chooses a product from a list. He clicks a button and on a second page the details of the product are shown.
      Webshopbean.java
      //Webshopbean gets access to the ProductBean (second bean for second page)
      //and puts the chosenProduct into the ProductBean:

       

      public class WebshopBean {

       

      private Product chosenProduct = new Product();

      public String showProduct(){

            ProductBean productBean = (ProductBean)getBean("productBean");
            productBean.setProductToShow(chosenProduct);
            return "show_product";
      }

       

      Or the other possibility: getting the value from WebshopBean.java

       

      public class ProductBean {

       

            private Product productToShow = new Product();

            public Product getProductToShow() {

            WebshopBean webshopBean = (WebshopBean)getBean("webshopBean");
            productToShow = webshopBean.getChosenProduct();
            return productToShow;
           }
      ...

       

      (Both beans use the following code to get access to the other bean:
      public Object getBean(String beanName){
            Object bean = null;
            FacesContext context = FacesContext.getCurrentInstance();
            bean = (Object) context.getApplication().getELResolver().getValue(context.getELContext(), null, beanName);           
            return bean;
      }

      )

       

      I am looking forward to your answers.
      Thanks a lot!
      Nicole