0

我的 jsf 页面

<h:form>
    <h:dataTable id="btable" value="#{ballController.items}" var="item">
         <h:column>
             <h:outputText value="#{item.id}"/>
         </h:column>
         <h:column>
             <h:outputText value="#{item.bname}"/>
         </h:column>
    </h:dataTable>

    <h:commandButton action="#{ballCOntroller.items}" value="test">
          <f:ajax execute="@form" render="btable" />
    </h:commandButton> 
</h:form>

在我的控制器中,此方法由 ballController.items 调用

public DataModel getItems() {
    if (items == null) {
        items = getPagination().createPageDataModel();
    }
    return items;
}

但是在单击按钮时,它会给我一个错误消息:

serverError:classjavax.faces.el.MethodNotFoundException /app/ball/index.xhtml @40,101 action="#{ballController.items}":找不到方法:ballController@a7150b6.items()

我的目标是在不刷新整个页面的情况下单击命令按钮重新加载数据表。

*已编辑

我的整个 bean 代码

@ManagedBean(name = "ballController")
@ViewScoped
public class ballController implements Serializable {

    private ball current;
    private DataModel items = null;
    @EJB
    private ballFacade ejbFacade;
    private PaginationHelper pagination;
    private int selectedItemIndex;

    public ballController() {
    }

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

    private ballFacade 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 = (ball) getItems().getRowData();
        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        return "View";
    }

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

    public String create() {
        try {

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

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

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

    public String destroy() {
        current = (ball) 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("ballDeleted"));
        } 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);
        }
    }

    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);
    }

    @FacesConverter(forClass = ball.class)
    public static class ballControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            ballController controller = (ballController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "ballController");
            return controller.ejbFacade.find(getKey(value));
        }

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

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

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

2 回答 2

2

要重新加载表,您只需要使用<f:ajax render="btable" />

<h:commandButton value="test">
      <f:ajax render="btable" />
</h:commandButton> 

但是要执行一些操作,例如向表中添加一些数据,您需要将按钮指向一个正确的方法

<h:commandButton value="test" action="#{ballController.addItem}"">
      <f:ajax render="btable" />
</h:commandButton> 


public void addItem() {
    if (items == null) {
        items = getPagination().createPageDataModel();
    }
    items.getWrappedData().add(/*something like new MyObject("one","two")*/);
}
于 2013-02-21T08:33:26.160 回答
1
serverError: classjavax.faces.el.MethodNotFoundException /app/ball/index.xhtml @40,101 action="#{ballController.items}": Method not found: ballController@a7150b6.items()

发生是因为您试图以getItems不正确的方式访问。

将您的 bean 代码更新为:

public DataModel items() {
    if (items == null) {
        items = getPagination().createPageDataModel();
    }
    return items;
}

否则将您的 facelet 动作 EL 表达式更改为action="#{ballCOntroller.getItems}".

编辑

您可以更新您List的:

public class DataTableBean implements Serializable {

    List<String> dataModelList = new ArrayList<DataModel>();

    public List<DataModel> getDataModelList() {
        return dataTableList;
    }

    public void setDataModelList(List<String> dataModelList) {
         this.dataTableList = dataTableList;
    }

    public void addItems(){

        List<DataModel> tempList = getPagination().createPageDataModel(); // Returns a list of type `List<DataModel>`.

        dataModelList.addAll(tempList);

    }
}
于 2013-02-21T08:29:38.020 回答