<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: Dropdown autocomplete and ManagedBean</title><link>https://answers.axonivy.com/questions/2315/dropdown-autocomplete-and-managedbean</link><description>&lt;p&gt;Hello everybody :)
I am implementing a dropdown autocomplete. This is my code so far: &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;@ManagedBean
public class MealBean {

    private String mealType;

    List&amp;lt;MealType&amp;gt; allMeals = DataHelper.getMealTypes();

    public List&amp;lt;String&amp;gt; complete(String query) {
        List&amp;lt;String&amp;gt; filteredMeals = new ArrayList&amp;lt;String&amp;gt;();

        for (int i = 0; i &amp;lt; allMeals.size(); i++) {
            MealType mType = allMeals.get(i);

            if (mType.getTypeOfMeal() != null
                    &amp;amp;&amp;amp; mType.getTypeOfMeal().toLowerCase().startsWith(query)) {
                filteredMeals.add(mType.getTypeOfMeal());
            }
        }
        return filteredMeals;
    }

    public String getMealType() {
        return mealType;
    }

    public void setMealType(String mealType) {
        this.mealType = mealType;
    }

}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The DataHelper actually is used for reading values from the database. The &lt;code&gt;complete&lt;/code&gt; method is invoked each time user enters something. So the thing i am wondering is that: when user enters some character is the &lt;code&gt;DataHelper.getMealType()&lt;/code&gt; method invoked,or just once when the bean is created,  if so how I can optimize the code because it will be not a good practice to ask the database for result each type user enters something.&lt;/p&gt;
&lt;p&gt;Thanks :)&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/2315/dropdown-autocomplete-and-managedbean" rel="self"></atom:link><language>en</language><lastBuildDate>Thu, 22 Dec 2016 11:22:12 -0500</lastBuildDate><item><title>Comment by tano9321 on Flavio Sadeghi's answer</title><link>https://answers.axonivy.com/questions/2315/dropdown-autocomplete-and-managedbean#2320</link><description>&lt;p&gt;thank you, that was the information I need :)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">tano9321</dc:creator><pubDate>Thu, 22 Dec 2016 11:22:12 -0500</pubDate><guid>https://answers.axonivy.com/questions/2315/dropdown-autocomplete-and-managedbean#2320</guid></item><item><title>Answer by Flavio Sadeghi</title><link>https://answers.axonivy.com/questions/2315/dropdown-autocomplete-and-managedbean/2317</link><description>&lt;p&gt;A managed bean has always a scope and the scope defines how long a bean lives. The default scope is request. Means the bean gets created for each request at most once. So when the same bean is used multiple times in the same request, always the same beans instance is used. Therefore the method &lt;code&gt;DataHelper.getMealType()&lt;/code&gt; is executed on each request once. &lt;/p&gt;
&lt;p&gt;JSF 2.x supports the below scopes. &lt;strong&gt;Your choice will probably be &lt;code&gt;@ViewScoped&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;@SessionScoped&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@RequestScoped&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@ApplicationScoped&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@ViewScoped&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Session Scope&lt;/strong&gt;: The session scope persists from the time that a session is established until session termination. A session terminates if the web application invokes the invalidate method on the HttpSession object, or if it times out.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;RequestScope&lt;/strong&gt;: The request scope is short-lived. It starts when an HTTP request is submitted and ends after the response is sent back to the client. If you place a managed bean into request scope, a new instance is created with each request. It is worth considering request scope if you are concerned about the cost of session scope storage.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ApplicationScope&lt;/strong&gt;: The application scope persists for the entire duration of the web application. That scope is shared among all requests and all sessions. You place managed beans into the application scope if a single bean should be shared among all instances of a web application. The bean is constructed when it is first requested by any user of the application, and it stays alive until the web application is removed from the application server.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ViewScope&lt;/strong&gt;: View scope was added in JSF 2.0. A bean in view scope persists while the same JSF page is redisplayed. (The JSF specification uses the term view for a JSF page.) As soon as the user navigates to a different page, the bean goes out of scope.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Source: Core Java Server Faces 3rd Edition by David Geary &amp;amp; Cay Horstmann [Page no. 51 - 54]):&lt;/em&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Flavio Sadeghi</dc:creator><pubDate>Thu, 22 Dec 2016 09:56:41 -0500</pubDate><guid>https://answers.axonivy.com/questions/2315/dropdown-autocomplete-and-managedbean/2317</guid></item></channel></rss>