繁体   English   中英

primefaces自动完成功能未显示在textBox中

[英]primefaces autocomplete not showing in textBox

我曾尝试在主字体中实现自动完成功能,但建议未在我的文本框中显示。 有人可以告诉我我所缺少的吗? 这些是代码

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

}

这对我有用

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM