我需要将primeface自动完成对象发送到支持bean,同时用户点击"添加"按钮。在这种情况下,primeface自动完成功能正常工作。但是当将所选对象发送到辅助bean时,它总是给我一个空指针
这是我的页面
<h:form id="orderForm" >
<p:outputLabel value="Item : " for="acItem" />
<p:autoComplete id="acItem" value="#{orderController.item}" completeMethod="#{orderController.completeItem}"
var="item" itemLabel="#{item.itemDescription}" itemValue="#{item}" converter="itemConverter" forceSelection="true" />
<p:outputLabel value="Qty : " for="itemQty" />
<p:inputText id="itemQty" value="#{orderController.quantity}"/>
<p:commandButton id="add" value="AddItem" update="dataTable" actionListener="#{orderController.addItemsToDataTable(item)}"/>
<p:dataTable id="dataTable" value="#{orderController.orderdItems}" var="item" paginator="true" rows="10">
<p:column>
<f:facet name="header">
<h:outputText value="Item ID"/>
</f:facet>
<h:outputText value="#{item.itemId}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Item Code"/>
</f:facet>
<h:outputText value="#{item.itemCode}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Customer Name"/>
</f:facet>
<h:outputText value="#{item.itemDescription}"/>
</p:column>
</p:dataTable>
<h:commandButton id="save" value="Save"
action="#{orderController.createOrder}" />
<h:messages />
</h:form>
这是我的烤豆
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import org.springframework.stereotype.Component;
import com.kpowd.model.Item;
import com.kpowd.model.Order;
import com.kpowd.model.OrderItem;
import com.kpowd.service.ItemService;
import com.kpowd.service.OrderService;
import com.kpowd.service.UserService;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
@Component
@ManagedBean
@ViewScoped
public class OrderController implements Serializable{
@ManagedProperty(value = "#{orderService}")
public OrderService orderService;
@ManagedProperty(value = "#{itemService}")
public ItemService itemService;
public Item item;
public List<Item> allItems;
// public List<Item> acFilterdItem;
public int quantity;
public List<Item> orderdItems;
@PostConstruct
public void Init(){
allItems = itemService.getAllItems();
System.out.println("item array size : "+allItems.size());
orderdItems = new ArrayList<Item>();
}
public List<Item> completeItem(String query) {
List<Item> acFilterdItem = new ArrayList<Item>();
for (int i = 0; i < allItems.size(); i++) {
if(allItems.get(i).getItemCode().toLowerCase().startsWith(query)){
acFilterdItem.add(allItems.get(i));
}
}
return acFilterdItem;
}
public void addItemsToDataTable(Item item){
// this is the object that gets null each time
System.out.println("inside addItemsToDataTable");
System.out.println("Print : "+item.toString());
// System.out.println("Item : "+item.getItemCode()+ " : " +item.getItemDescription());
orderdItems.add(item);
// return "";
}
}
请帮助我.....
答案 0 :(得分:1)
我认为这是因为commandButton属性:
actionListener="#{orderController.addItemsToDataTable(item)}"
不知道您尝试传递的item
参数。虽然类属性item
应该具有getter / setter,所以只需将该方法签名切换到此方法:
actionListener="#{orderController.addItemsToDataTable(orderController.item)}"
甚至,你可以省略它,并保持它:
actionListener="#{orderController.addItemsToDataTable}"
加上managedBean:public void addItemsToDataTable( ){ ... }
。