2014-01-22 85 views
0

我試圖在primefaces中實現自動完成功能,但建議不會顯示在我的文本框中。有人能告訴我我錯過了什麼嗎?論文是碼primefaces自動完成功能未顯示在文本框中

udateCategory.xhtml


<p:panel header="Type in Category to Edit" >     
    <p:outputLabel value="Category Name"/> 
    <p:autoComplete value="#{categoryBean.selectedCategory}" 
            completeMethod="#{categoryBean.completeCategory}" 
            var="cat" 
            itemLabel="#{cat.categoryName}" 
            itemValue="#{cat}" 
            converter="#{catConverter}" 
            forceSelection="true"/> 


    <p:commandButton value="Update" action="#{category.saveCategory}"/> 
</p:panel> 

CategoryBean


public class CategoryBean implements Serializable{ 

    private Category selectedCategory; 

    /** 
    * Creates a new instance of CategoryBean 
    */ 
    public CategoryBean() { 
    } 

    public List<Category> completeCategory (String query){ 

     CategoryManager manager = new CategoryManager();//an instance of the manager 
     List<Category> suggestions = new ArrayList<>();//an instance of list 
     List<Category> allCategory = new ArrayList<>(); //populate the allCategory with data fro db 
     allCategory = manager.getAllCategory(); 

     //checck to see if data exist in allCategory 
     if(!allCategory.isEmpty()){ 
      System.out.println("kobla : allcategory has data"); 
     } 
     else 
     { 
      System.out.println("kobla: no data in alcategory"); 
     } 

     for(Category cat : allCategory){ 
      if(cat.getCategoryName().startsWith(query)){ 
       suggestions.add(cat); 
      } 
     } 

     //check to see if data exists in sugestions 
     if (!suggestions.isEmpty()) { 
      System.out.println("kobla : suggestions has data"); 
     } else { 
      System.out.println("kobla: no data in suggestions"); 
     } 
     return suggestions; 
    } 



    /** 
    * @return the selectedCategory 
    */ 
    public Category getSelectedCategory() { 
     return selectedCategory; 
    } 

    /** 
    * @param selectedCategory the selectedCategory to set 
    */ 
    public void setSelectedCategory(Category selectedCategory) { 
     this.selectedCategory = selectedCategory; 
    } 


} 

CategoryConverter


public class CategoryConverter implements Converter{ 

    @Override 
    public Object getAsObject(FacesContext context, UIComponent component, String value) { 

     if(value.trim().equals("")){ 
      return null; 
     } 
     else{ 
     try{ 
      int id = Integer.parseInt(value); 
      List<Category> myCategory = new ArrayList<>();// 
      myCategory = new CategoryManager().getAllCategory();//load data fro db 
      for(Category cat : myCategory){ 
       if(cat.getCategoryID() == id){ 
        return cat; 
       } 
      } 
     } 
     catch(Exception e){ 

     e.printStackTrace(); 
     } 
    } 
     return null; 
    } 



    @Override 
    public String getAsString(FacesContext context, UIComponent component, Object value) { 

     if(value == null || value ==""){ 
      return null; 
     } 
     else 
     { 
      return String.valueOf(((Category)value).getCategoryName()); 
     } 
     // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

} 

回答

0

這對我的作品

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 

import javax.annotation.PostConstruct; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 

@ManagedBean 
@ViewScoped 
public class CategoryBean implements Serializable { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private Category selectedCategory; 

    @PostConstruct 
    public void init(){ 
     selectedCategory = new Category(); 
    } 

    public void saveCategory(){ 
     System.out.println("saved "+this.selectedCategory); 
    } 

    public List<Category> completeCategory(String query) { 
     CategoryManager manager = new CategoryManager();// an instance of the 
                 // manager 
     List<Category> suggestions = new ArrayList<>();// an instance of list 
     List<Category> allCategory = new ArrayList<>(); // populate the 
                 // allCategory with data 
                 // fro db 
     allCategory = manager.getAllCategory(); 

     // checck to see if data exist in allCategory 
     if (!allCategory.isEmpty()) { 
      System.out.println("kobla : allcategory has data"); 
     } else { 
      System.out.println("kobla: no data in alcategory"); 
     } 

     for (Category cat : allCategory) { 
      if (cat.getCategoryName().startsWith(query)) { 
       suggestions.add(cat); 
      } 
     } 

     // check to see if data exists in sugestions 
     if (!suggestions.isEmpty()) { 
      System.out.println("kobla : suggestions has data"); 
     } else { 
      System.out.println("kobla: no data in suggestions"); 
     } 
     return suggestions; 
    } 

    /** 
    * @return the selectedCategory 
    */ 
    public Category getSelectedCategory() { 
     return selectedCategory; 
    } 

    /** 
    * @param selectedCategory 
    *   the selectedCategory to set 
    */ 
    public void setSelectedCategory(Category selectedCategory) { 
     this.selectedCategory = selectedCategory; 
    } 

} 

import java.io.Serializable; 

public class Category implements Serializable { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private int categoryID; 
    private String categoryName; 
    public int getCategoryID() { 
     return categoryID; 
    } 
    public void setCategoryID(int categoryID) { 
     this.categoryID = categoryID; 
    } 
    public String getCategoryName() { 
     return categoryName; 
    } 
    public void setCategoryName(String categoryName) { 
     this.categoryName = categoryName; 
    } 
    public Category(int categoryID, String categoryName) { 
     super(); 
     this.categoryID = categoryID; 
     this.categoryName = categoryName; 
    } 
    public Category() { 
     super(); 
    } 
    @Override 
    public String toString() { 
     return "Category [categoryID=" + categoryID + ", categoryName=" 
       + categoryName + "]"; 
    } 

} 

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.RequestScoped; 
import javax.faces.component.UIComponent; 
import javax.faces.context.FacesContext; 
import javax.faces.convert.Converter; 

@ManagedBean 
@RequestScoped 
public class CategoryConverter implements Converter, Serializable{ 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 



    @Override 
    public Object getAsObject(FacesContext context, UIComponent component, String value) { 

     if(value.trim().equals("")){ 
      return null; 
     } 
     else{ 
     try{ 
      int id = Integer.parseInt(value); 
      List<Category> myCategory = new ArrayList<>();// 
      myCategory = new CategoryManager().getAllCategory();//load data fro db 
      for(Category cat : myCategory){ 
       if(cat.getCategoryID() == id){ 
        return cat; 
       } 
      } 
     } 
     catch(Exception e){ 

     e.printStackTrace(); 
     } 
    } 
     return null; 
    } 



    @Override 
    public String getAsString(FacesContext context, UIComponent component, Object value) { 

     if(value == null || value ==""){ 
      return null; 
     } 
     else 
     { 
      return String.valueOf(((Category)value).getCategoryName()); 
     } 
     // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 
} 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:p="http://primefaces.org/ui"> 
<h:head></h:head> 
<h:body> 
    <h:form> 
     <p:outputLabel value="Category Name" /> 
     <p:autoComplete 
      value="#{categoryBean.selectedCategory}" 
      completeMethod="#{categoryBean.completeCategory}" 
      var="cat" 
      itemLabel="#{cat.categoryName}" 
      itemValue="#{cat}" 
      converter="#{categoryConverter}" 
      forceSelection="true" /> 
     <p:commandButton value="Update" action="#{categoryBean.saveCategory}" /> 
    </h:form> 
</h:body> 
</html> 
+0

我已經做了所有的t他改變了,但沒有幫助。這是一樣的故事。我也意識到arrayList「建議」裏面沒有數據。 – kobla

+0

我使用java 7在tomee 1.6.0+上運行此代碼。在我的示例中,如果鍵入「T」,則表示按預期方式顯示「Two」和「Three」。你使用什麼容器和JSF版本?您是否嘗試將此代碼複製並粘貼到您的應用程序中? – 2014-01-22 09:03:28

+0

就是這樣,它現在起作用了,謝謝 – kobla

相關問題