我正在学习JavaEE,我正在使用jsf和managedbean进行ajax调用。我试图在下拉列表中更改值时立即显示文本。我看到很多人在stackoverflow上遇到同样的问题,我尝试按照标记为已接受的答案,但我仍然无法使其工作。有人可以告诉我我的错误在哪里吗? 这是jsf代码:
<h:form>
<h:selectOneMenu id="productlist" value="#{productRepoMB.selectedProduct}">
<f:selectItems value="#{productRepoMB.productList}" var="product" itemValue="#{product.productID}" />
<f:ajax event="valueChange"
render="result"
listener="#{productRepoMB.selectMenuListener}" />
</h:selectOneMenu>
text changed: <h:outputText id="result" value="#{productRepoMB.text}" />
</h:form>
这是托管bean代码,产品列表是从数据库中填充的:
@Named
@RequestScoped
public class ProductRepoMB implements Serializable {
@EJB
private ProductsRepo productRepo;
private Products selectedProduct;
private List<Products> productList;
private String text="init text"; //use for testing ajax call
public ProductRepoMB() {
}
public Products getSelectedProduct() {
return selectedProduct;
}
public List<Products> getProductList() {
productList = productRepo.findAll();
return productList;
}
//The following code are used to testing ajax only!
public void selectMenuListener(AjaxBehaviorEvent e) {
setText("changed!");
}
public String getText() {
return text;
}
public void setText(String text){
this.text = text;
}
}