2012-08-10 70 views
1

我想创建一个方法,允许我通过一个带有inputText字段的JSF页面进行搜索。我打算从位于托管bean中的数组列表中获取一个或多个条目,并在我点击提交后将其显示在JSF页面上。 我试图从这个问题中汲取灵感,但在创建搜索方法我坚持,因为我在Java中的新手: JSF2 search boxArrayList的JSF2搜索方法

在我的JSF页面,我打算有这样的事情,“东西”是方法我失踪:

<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> 

在我的java文件,我有以下的ArrayList 'orderList',我只是使用字符串:

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") 
)); 

希望这是足够的信息。 我想我必须从头开始制作一些全新的方法,但我一分钟也没有太多。 我会怎样把它们绑在一起?任何指针将不胜感激:)

〜编辑〜 这里是我的订单对象供参考,我假设我在这里使用一个getters?

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;} 
    } 

回答

2

首先,你将需要做一些工作与Java平等:

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

搜索ArrayList的(也许不是我们的最佳结构,但它的工作)会需要编写一个函数来处理它,在第一个例子中它使用了commandButton的动作(执行一个方法)。你所拥有的将不会做任何事情,因为它没有任何东西可以执行(没有方法被调用)。

如果你只是在学习JSF,并且你不熟悉Java,我建议保持它简单直到你学习生命周期。如果你没有一个可靠的Java背景,那将会非常令人沮丧。

但是,要回答你的问题,你需要用ValueChangeEvent做些事情,因为你没有命令(按照他的答案的后半部分)。

您的“搜索方法”将是纯Java操作数据结构。一个简单的实现可能是:

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; 
    } 
    } 

这是假设你的订单对象有一个方法getFirstValue()返回一个字符串。我也假设基于你的构造函数,值永远不为空(有很多潜在的陷阱)。假设你已经登记在web.xml中的“OrderBean”或者你已经使用了ManagedBean注释你的JSF可能看起来像:

<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> 

希望这点你在正确的方向。再次,我会坚持使用actions/actionListeners,直到您获得框架的基础知识。从那里很容易移动到非常复杂的行动,没有太多的工作。我这样说的原因是它们更易于调试并且很容易理解。

+0

谢谢,我相信这会帮助我很多。那么我只有三个字符串的getter和setter:thingNo; thingName;和thingInfo ;. – Froob 2012-08-10 15:02:38

+0

不明白setSelectedOrder,即使我导入java.lang,netbeans也不能识别长度。字符串 – Froob 2012-08-10 18:03:12

+0

对不起,这是因为我现在在跳跃语言,它的方法调用search.length(),而不是属性(长度)。 Netbeans和eclipse都有很好的功能来提供修复。如果我记得(我可能没有),Netbeans是ALT-ENTER,并且eclipse是用于修复的ctrl-space/ctrl-1。上面的代码更多的是一个草图而不是工作代码。 – 2012-08-10 18:32:59