Froob
Froob

Reputation: 15

JSF2 Search method for ArrayList

I am trying to create a method that allows me to search via a JSF page with an inputText field. I intend on getting one or more entries from an arraylist, located in a managed bean, and displaying it on the JSF page once I hit submit. I tried to draw inspiration from this question, but am stuck at creating the search method as I am a novice at Java: JSF2 search box

In my JSF page I intend to have something like this, 'something' is that method that I'm missing:

<h:form>
    <h:inputText id="search" value="#{order.something}">
        <f:ajax execute="search" render="output" event="blur" />
    </h:inputText>
    <h2>
        <h:outputText id="output" value="#{order.something}" />
    </h2>
</h:form>

In my java file I have the following arraylist 'orderList', I just use strings:

private static final ArrayList<Order> orderList = 
    new ArrayList<Order>(Arrays.asList(
    new Order("First", "Table", "description here"),
    new Order("Second", "Chair", "2nd description here"),
    new Order("Third", "Fridge", "3rd description here"),
    new Order("Fourth", "Carpet", "4th description here"),
    new Order("Fifth", "Stuff", "5th description here")
));

Hope this is sufficient information. I guess I have to make some completely new methods from scratch, but I don't have much at the minute. How would I go about tying these together? Any pointers would be greatly appreciated :)

~edit~ Here is my Order object for reference, I assume I use one of the getters here?

public static class Order{
    String thingNo;
    String thingName;
    String thingInfo;

    public Order(String thingNo, String thingName, String thingInfo) {
        this.thingNo = thingNo;
        this.thingName = thingName;
        this.thingInfo = thingInfo;
    }
    public String getThingNo() {return thingNo;}
    public void setThingNo(String thingNo) {this.thingNo = thingNo;}
    public String getThingName() {return thingName;}
    public void setThingName(String thingName) {this.thingName = thingName;}
    public String getThingInfo() {return thingInfo;}
    public void setThingInfo(String thingInfo) {this.thingInfo = thingInfo;}
    }

Upvotes: 1

Views: 743

Answers (1)

Daniel B. Chapman
Daniel B. Chapman

Reputation: 4687

First, you're going to need to do some work with java equality:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

Searching the ArrayList (perhaps not the best structure for this, but it does work) would require writing a function to handle it, in the first example it uses the commandButton's action (executing a method). What you have won't do anything as it doesn't have anything to perform (no methods are called).

If you're just learning JSF and you're not familiar with Java I'd recommend keeping it simple until you learn the life cycle. It can be highly frustrating if you don't have a solid Java background.

However, to answer your question you would need to do something with a ValueChangeEvent as you don't have the command (as per the second half of his answer).

Your "search method" would be pure Java acting on the data-structure. A simple implementation could be:

  public void searchByFirstValue(String search)
  {
    if(search == null || search.length() == 0)
      return;

    for(Order order : orderList)
    {
      if(order.getFirstValue().contains(search)) //java.lang.String methods via a regular expression
        setSelectedOrder(order);

      return;
    }
  }

This assumes that your Order object has a method getFirstValue() that returns a String. I'm also assuming that value is never null based on your constructor (There's a lot of potential pitfalls). Assuming you've registered the 'OrderBean' in the web.xml or you've used the ManagedBean annotations your JSF might look like:

    <h:form>
    <h:inputText id="search" valueChangeListener="#{orderBean.onChange}"> <!--Use the previous example -->
        <f:ajax execute="search" render="output" event="blur" />
    </h:inputText>
    <h2>
        <h:outputText id="output" value="#{orderBean.order}" /> <!-- this is the object from setSelectedOrder(order); -->
    </h2>
    </h:form>

Hopefully that points you in the right direction. Again, I'd stick to actions/actionListeners until you get the basics of the framework down. From there it is pretty easy to move to very complicated actions without a lot of work. The reason I say this is that they are easier to debug and very simple to understand.

Upvotes: 2

Related Questions