运行查询后,视图中不显示任何结果

时间:2013-11-20 16:05:14

标签: jsf primefaces derby

我有一个搜索查询,应搜索数据库并返回与该用户名匹配的所有详细信息或包含搜索字符串的任何部分

 <h:form id="searchForm">
                    <p:outputLabel value="Search: "/>
                    <h:inputText id="search" value="#{userdetailsController.search}"  />   
                    &nbsp;
                    <p:commandButton value="Search" action="#{userdetailsController.submit}" ajax="true" update="UserTableSearch"/>
                    <br></br>
                    <br></br>
                    <p:dataTable id="UserTableSearch"
                                 widgetVar="UserTableSearch" 
                                 paginator="true" rows="10"
                                 value="#{userdetailsController.item}"
                                 var="item"
                                 emptyMessage="No details was found with given criteria">

                        <p:column id="id"  
                                  headerText="i.d." 
                                  sortBy="id">
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListUserdetailsTitle_id}"/>
                            </f:facet>
                            <h:outputText value="#{userdetails.username}"/>
                        </p:column>

                        <p:column id="username"   
                                  headerText="username"
                                  sortBy="username">
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListUserdetailsTitle_username}"/>
                            </f:facet>
                            <h:outputText value="#{userdetails.username}"/>
                        </p:column>
                    </p:dataTable>
                </h:form>

即视图,用户可以在其中搜索用户名,然后应该自动使用搜索结果更新数据表,但是当前显示的是空消息

emptyMessage="No details was found with given criteria"

如何检查传递给视图的数据是否包含某些值,

这是它调用的userdetails控制器

@Named("userdetailsController")
@SessionScoped
public class UserdetailsController implements Serializable {

    private Userdetails current;
    private DataModel items = null;
    @EJB

    private Richard.beans.UserdetailsFacade ejbFacade;
    private PaginationHelper pagination;
    private int selectedItemIndex;
    private String search;

    private List<Userdetails> item; // No need for DataModel here.

    public List<Userdetails> getItem() {
        return item;
    }

    public String submit() {
        item = ejbFacade.searchByString(search);
        return ""; //change this !, testing only
    }

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }

    public UserdetailsController() {
    }

    public Userdetails getSelected() {
        if (current == null) {
            current = new Userdetails();
            selectedItemIndex = -1;
        }
        return current;
    }

    private UserdetailsFacade getFacade() {
        return ejbFacade;
    }

    public PaginationHelper getPagination() {
        if (pagination == null) {
            pagination = new PaginationHelper(10) {

                @Override
                public int getItemsCount() {
                    return getFacade().count();
                }

                @Override
                public DataModel createPageDataModel() {
                    return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
                }
            };
        }
        return pagination;
    }

    public String prepareList() {
        recreateModel();
        return "List";
    }

    public String prepareView() {
        current = (Userdetails) getItems().getRowData();
        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        return "View";
    }

    public String prepareCreate() {
        current = new Userdetails();
        selectedItemIndex = -1;
        return "Create";
    }

    public String create() {
        try {
            getFacade().create(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("UserdetailsCreated"));
            return prepareCreate();
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String prepareEdit() {
        current = (Userdetails) getItems().getRowData();
        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        return "Edit";
    }

    public String update() {
        try {
            getFacade().edit(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("UserdetailsUpdated"));
            return "View";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String destroy() {
        current = (Userdetails) getItems().getRowData();
        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        performDestroy();
        recreatePagination();
        recreateModel();
        return "List";
    }

    public String destroyAndView() {
        performDestroy();
        recreateModel();
        updateCurrentItem();
        if (selectedItemIndex >= 0) {
            return "View";
        } else {
            // all items were removed - go back to list
            recreateModel();
            return "List";
        }
    }

    private void performDestroy() {
        try {
            getFacade().remove(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("UserdetailsDeleted"));
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    private void updateCurrentItem() {
        int count = getFacade().count();
        if (selectedItemIndex >= count) {
            // selected index cannot be bigger than number of items:
            selectedItemIndex = count - 1;
            // go to previous page if last page disappeared:
            if (pagination.getPageFirstItem() >= count) {
                pagination.previousPage();
            }
        }
        if (selectedItemIndex >= 0) {
            current = getFacade().findRange(new int[]{selectedItemIndex, selectedItemIndex + 1}).get(0);
        }
    }

    //datatable for all results
    public DataModel getItems() {
        if (items == null) {
            items = getPagination().createPageDataModel();
        }
        return items;
    }

    private void recreateModel() {
        items = null;
    }

    private void recreatePagination() {
        pagination = null;
    }

    public String next() {
        getPagination().nextPage();
        recreateModel();
        return "List";
    }

    public String previous() {
        getPagination().previousPage();
        recreateModel();
        return "List";
    }

    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
    }

    public Userdetails getUserdetails(java.lang.Integer id) {
        return ejbFacade.find(id);
    }

    @FacesConverter(forClass = Userdetails.class)
    public static class UserdetailsControllerConverter implements Converter {

        @Override
        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            UserdetailsController controller = (UserdetailsController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "userdetailsController");
            return controller.getUserdetails(getKey(value));
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuilder sb = new StringBuilder();
            sb.append(value);
            return sb.toString();
        }

        @Override
        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Userdetails) {
                Userdetails o = (Userdetails) object;
                return getStringKey(o.getId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Userdetails.class.getName());
            }
        }

    }

}

由于

1 个答案:

答案 0 :(得分:1)

调用您的getItem()方法来填充数据。在该方法中,您可以遍历列表或只打印出项目数。

同样在submit()中检查search中是否有任何值,或者是否正在调用它。我怀疑search为空。

您的inputtext不是PrimeFaces组件。我会把它改成PrimeFaces。存在渲染时间差异,这可能导致搜索字符串不存在。