Search code examples
javajsfjsf-2javabeansmanaged-bean

JSF Java Beans - initialize and proceed


I am new to JavaBeans and I need a little help to keep my first little JSF-project going.

I am writing a little web application where a user can search with certain criteria for buildings. So the user enters in the search form 'location', 'property type', 'asking price', 'number of rooms' and 'living space'.

My managed bean accept the requiry with setter/getter and now the data is to be transmitted to a SQL class, where they are processed and matching search results are returned. It sounds simple, but I can not find a solution.

My managed bean looks like this now:

package beans

//import statements
...

@ManagedBean
@RequestScoped
public class PropertySearchBean {
   private String _place
   private String _propertyType
   private double _askingPrice
   private int    _rooms
   private double _livingSpace

   public ArrayList<SearchResults> results = new ArrayList<SearchResults>();

   // empty constructor
   ...

   // getter and setter for these 5 user inputs
   ...

   public void initializeSearchResults() {
      // do the SQL query, recieve search results
      // add it as a new object of 'SearchResults'

      SQLPropertySearch search = new SQLPropertySearch(_place, _propertyType,
                                 _askingPrice, _rooms, _livingSpace);
      ArrayList<Integer> idResults = search.getPropertyIDlist();
      SQLProperty property;

      if(!idResults.isEmpty()) {
         for(int i=0; i<idResults.size(); i++) {
            property = new SQLProperty(idResults.get(i));

            results.add(new SearchResults(
               property.getPropertyID(),
               property.getPropertyName(),
               // and so on..
            ));
         }
      }
   }

   public static class SearchResults {
      int propertyID;
      String propertyName;
      // and so on..

      public SearchResults(int propertyID, String propertyName) {
         this.propertyID = propertyID;
         this.propertyName = propertyName;
         // and so on..
      }

      // getter and setter
      public int getPropertyID() {
         return propertyID;
      }
      public void setPropertyID(int propertyID) {
         this.propertyID = propertyID;
      }
      // and so on..
   }

   public ArrayList<SearchResults> getResults() {
      return results;
   }
}

In my XHTML-file I go through each entry of my ArrayList results.

It looks like this:

<ui:repeat var="res" value="#{PropertySearchBean.results}">
   <p>#{res.propertyID}</p>
   <p>#{res.propertyName}</p>
</ui:repeat>

I don't have an idea how to initialize the ArrayList, because first thing to do is the search itself, with the user input.

I am thankful for any kind of help!


Solution

  • You've removed the getters and setters from your example to improve readability. I'll provide one implementation here to ensure a common understanding (especially regarding the leading underscores).

    public String getPlace() {
        return _place;
    }
    
    public void setPlace(String place) {
        this._place = place;
    }
    

    The property 'place' will be accessible within your view by using the value binding #{propertySearchBean.place}(see below).

    Your code is meant to perform a search. Therefore you'll have to transfer user input from your XHTML file (view) to your managed bean. To do so you need to add a form to your view. Each search query parameter is bound to your bean using a specific value binding. Additionally the form contains a <h:commandButton> tag which finally triggers initialization of the result list.

    <h:form>
        <h:outputLabel for="place" value="Place:" />
        <h:inputText id="place" value="#{propertySearchBean.place}" />
    
        <!-- Additional fields -->
    
        <h:commandButton action="#{propertySearchBean.initializeSearchResults}" 
            value="Search"/>
    </h:form>
    

    Note: You've used the following code in your example

    <ui:repeat var="res" value="#{PropertySearchBean.results}">
    

    Make sure that the first letter of your bean name is lower-case (propertySearchBean instead of PropertySearchBean). So this needs to be updated to

    <ui:repeat var="res" value="#{propertySearchBean.results}">