我的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;
}
但点击按钮后,它会给我一个错误,说:
服务器错误:classjavax.faces.el.MethodNotFoundException/app/ball/index.xhtml@40101 action="#{ballController.items}":方法未找到:ballController@a7150b6.items()
我的目标是在单击命令按钮时重新加载dataTable,而不刷新整个页面。
*已编辑
我的全豆代码
@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());
}
}
}
}
要重新加载表,只需要使用<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")*/);
}
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表达式更改为CCD_ 3。
编辑
您可以将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);
}
}