欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

通过JSF 2实现可重用的Ajax化组件(1)(6)

来源: javaer 分享于  点击 10696 次 点评:134

清单6显示了监听程序的最终实现:

清单6.监听程序

  1. package com.corejsf;  
  2.  
  3. import java.io.Serializable;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.  
  8. import javax.enterprise.context.SessionScoped;  
  9. import javax.faces.component.UIInput;  
  10. import javax.faces.component.UISelectItems;  
  11. import javax.faces.component.UISelectOne;  
  12. import javax.faces.context.FacesContext;  
  13. import javax.faces.event.ValueChangeEvent;  
  14. import javax.inject.Named;  
  15.  
  16. @Named  
  17. @SessionScoped  
  18. public class AutocompleteListener implements Serializable {  
  19.    private static String COMPLETION_ITEMS_ATTR = "corejsf.completionItems";  
  20.     
  21.    public void valueChanged(ValueChangeEvent e) {  
  22.       UIInput input = (UIInput)e.getSource();  
  23.       UISelectOne listbox = (UISelectOne)input.findComponent("listbox");  
  24.  
  25.       if (listbox != null) {  
  26.          UISelectItems items = (UISelectItems)listbox.getChildren().get(0);  
  27.          Map<String, Object> attrs = listbox.getAttributes();  
  28.          List<String> newItems = getNewItems((String)input.getValue(),  
  29.             getCompletionItems(listbox, items, attrs));  
  30.  
  31.          items.setValue(newItems.toArray());  
  32.          setListboxStyle(newItems.size(), attrs);  
  33.       }  
  34.    }  
  35.     
  36.    public void completionItemSelected(ValueChangeEvent e) {  
  37.      UISelectOne listbox = (UISelectOne)e.getSource();  
  38.      UIInput input = (UIInput)listbox.findComponent("input");  
  39.       
  40.      if(input != null) {  
  41.         input.setValue(listbox.getValue());  
  42.      }  
  43.      Map<String, Object> attrs = listbox.getAttributes();  
  44.      attrs.put("style", "display: none");  
  45.    }  
  46.      
  47.    private List<String> getNewItems(String inputValue, String[] completionItems) {  
  48.       List<String> newnewItems = new ArrayList<String>();  
  49.       
  50.       for (String item : completionItems) {  
  51.          String s = item.substring(0, inputValue.length());  
  52.          if (s.equalsIgnoreCase(inputValue))  
  53.            newItems.add(item);  
  54.       }  
  55.       
  56.       return newItems;  
  57.    }  
  58.     
  59.    private void setListboxStyle(int rows, Map<String, Object> attrs) {  
  60.       if (rows > 0) {  
  61.          Map<String, String> reqParams = FacesContext.getCurrentInstance()  
  62.             .getExternalContext().getRequestParameterMap();  
  63.         
  64.          attrs.put("style", "display: inline; position: absolute; left: "  
  65.              + reqParams.get("x") + "px;" + " top: " + reqParams.get("y") + "px");  
  66.  
  67.          attrs.put("size", rows == 1 ? 2 : rows);  
  68.       }  
  69.       else  
  70.          attrs.put("style", "display: none;");  
  71.    }  
  72.  
  73.    private String[] getCompletionItems(UISelectOne listbox,  
  74.       UISelectItems items, Map<String, Object> attrs) {  
  75.          Strings] completionItems = (String[])attrs.get(COMPLETION_ITEMS_ATTR);  
  76.       
  77.          if (completionItems == null) {  
  78.             completionItems = (String[])items.getValue();  
  79.             attrs.put(COMPLETION_ITEMS_ATTR, completionItems);  
  80.          }  
  81.       return completionItems;  
  82.    }  
  83. }  
  84.  

JSF在Ajax调用期间调用监听程序的valueChanged()方法来响应文本输入中的keyup事件。该方法会创建一组新的完成项目,然后将列表框的项目设置为这个新的项目集。该方法还会设置列表框的样式属性,以确定Ajax调用返回时是否显示列表框。

清单6中的setListboxStyle()方法将使用x和y请求我在发起清单5中的Ajax调用时指定的参数值。

JSF会在Ajax调用期间调用监听程序的其他公共方法completionItemSelected(),以响应列表框中的选择事件。该方法会将列表框的值复制到文本输入中,并隐藏列表框。

请注意,valueChanged()方法还会将原始完成项目存储在列表框的某个属性中。由于每个autoComplete组件都维护自己的完成项目列表,因此多个autoComplete组件可以在相同页面中和谐共存,而不会影响彼此的完成项目。


相关文章

    暂无相关文章
相关栏目:

相关文章

    用户点评