How to use multiple controller beans in one .xhtml file?

UI Components for JSF
Post Reply
Bwang
Posts: 7
Joined: 23 Aug 2011, 08:27

28 Sep 2011, 09:40

Hi All,

I am facing a design issue. My co-worker complains I use one controller bean in .xhtml file to handle all the business logics.
So I want to separate the original controller into two controllers.

One controller(createFeature) is to add new entry. Another controller(manageFeatures) is to show all the entries(including the new one).
My issue is when new entry is created, it can't be show .

Code: Select all

<html xmlns="http://www.w3.org/1999/xhtml"   
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.prime.com.tr/ui">
    <h:head>
    </h:head>
    <h:body>
        <h1 class="title ui-widget-header ui-cornet-all">Feature</h1>
            <div class="entry">
                <h:form id="form">
                    <p:panel header="Create a new feature">
                        <h:panelGrid id="create" columns="2" columnClasses="lable,value">
                            <h:outputLabel value="Feature : *" for="txt_feature"/>
                            <p:inputText id="txt_feature" value="#{createFeature.feature.feature}" required="true"/>
                            <h:outputLabel value="Score : *" for="txt_score"/>
                            <p:inputText id="txt_score" value="#{createFeature.feature.score}" required="true"/>
                        <h:panelGroup>
                            <p:commandButton value="Reset" type="reset" async="false"/>
                            <p:commandButton value="Submit" actionListener="#{createFeature.save}" update="create, manage"/>
                        </h:panelGroup>
                    </h:panelGrid>
                   </p:panel>

                   <p:dataTable id="manage" var="feature" value="#{manageFeatures.features}">
                       <p:column sortBy="#{feature.feature}" filterBy="#{feature.feature}">
                           <f:facet name="header">
                               <h:outputText value="Feature"/>
                           </f:facet>
                           <h:outputText value="#{feature.feature}"/>
                       </p:column>
                       <p:column>
                           <f:facet name="header">
                               <h:outputText value="Score"/>
                           </f:facet>
                           <h:outputText value="#{feature.score}"/>
                       </p:column>
                   </p:dataTable>
               </h:form>
           </div>
</h:body>
</html>
In the construct function of manageFeatures, feature list will be loaded from database.

Can you help me to fix this issue?

Thank you,
Colin.B.Wang
Mojarra 2.1.2, PrimeFaces 3.0.M2, Tomcat 7.0.16

arturo
Posts: 90
Joined: 23 Aug 2011, 09:57
Location: Mexico

28 Sep 2011, 10:16

Of course you can use multiple managed beans in a single page, but keep in mind the scopes of the managed beans you are using. createFeature could be request scoped, in the case of the manageFeatures managed bean you could use request scoped and fetch the data from database in the mehtod getFeatures, not in the constructor, because the request scoped managed beans are pooled, so every time you call a method not necesarily implies that it will be a new instance of the bean.
PrimeFaces 4.0 | Extensions 1.1.0 | GlassFish 4.0 | Mojarra 2.2.4 | NetBeans 7.3.1

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

28 Sep 2011, 10:27

I would recommend you to read this article too http://blog.icefaces.org/blojsom/blog/d ... ged-beans/
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

Bwang
Posts: 7
Joined: 23 Aug 2011, 08:27

28 Sep 2011, 11:37

Thank you, arturo. Problem has been fixed following your tip.

Code: Select all

@ManagedBean(name="createFeature")
@RequestScoped
public class CreateFeature ...

Code: Select all

public class ManageFeatures {...
    public List<Feature> getFeatures() {
        features = featureDAO.findAll();
        return features;
    }
...}
arturo wrote:Of course you can use multiple managed beans in a single page, but keep in mind the scopes of the managed beans you are using. createFeature could be request scoped, in the case of the manageFeatures managed bean you could use request scoped and fetch the data from database in the mehtod getFeatures, not in the constructor, because the request scoped managed beans are pooled, so every time you call a method not necesarily implies that it will be a new instance of the bean.
Mojarra 2.1.2, PrimeFaces 3.0.M2, Tomcat 7.0.16

Bwang
Posts: 7
Joined: 23 Aug 2011, 08:27

28 Sep 2011, 11:38

Thank you, Oleg.
I am reading this article.
Improve my understanding.
Oleg wrote:I would recommend you to read this article too http://blog.icefaces.org/blojsom/blog/d ... ged-beans/
Mojarra 2.1.2, PrimeFaces 3.0.M2, Tomcat 7.0.16

piogwi
Posts: 19
Joined: 29 Aug 2011, 08:31

28 Sep 2011, 19:47

I would recommend to lazy-load data in beans

Code: Select all


public class ManageFeatures {
   
   private List<Feature> features;
  
    public List<Feature> getFeatures() {
        if (features == null) {
             features = featureDAO.findAll();
        }
        return features;
    }

Getter can be fired for example 50 times during response rendering (why not, it's a getter). Data should be loaded once. Constructor is not a good place. You may want to load features in other page, where you don't need other data from ManageFeatures bean.

I tend to create something like ListsProvider bean. With all those dictionary-like lists to use in comb boxes. For example I use List<UserGroup> on 10 different pages to select one group in combobox. Every time I get it from tha same request scoped bean with lazy loaded list and all converters etc. needed.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 45 guests