【问题标题】:primefaces autocomplete not showing in textBoxprimefaces自动完成未显示在文本框中
【发布时间】:2014-01-22 05:31:51
【问题描述】:

我尝试在 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;
    }


}

类别转换器


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

}

【问题讨论】:

    标签: jsf primefaces


    【解决方案1】:

    这对我有用

    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>
    

    【讨论】:

    • 我已完成所有更改,但没有帮助。这是同一个故事。我还意识到arrayList“suggestions”中没有数据。
    • 我已经使用 java 7 在 tomee 1.6.0+ 上运行此代码。在我的示例中,如果您键入“T”,它会按预期提示“二”和“三”。您使用的是什么容器和 JSF 版本?您是否尝试过将此代码复制并粘贴到您的应用中?
    猜你喜欢
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 2017-05-19
    • 1970-01-01
    相关资源
    最近更新 更多