selectonemenu没有使用<p:ajax> primefaces </p:ajax>从另一个selectonemenu更新

时间:2013-03-12 18:49:25

标签: java-ee jsf-2 primefaces

我正在尝试通过使用<p:ajax update=":main:deviceosName" listener="#deviceRegistration.updateOSName}" />从其他selectonemenu.i选择一些值来更新selectonemenu 但是监听器没有调用updateOSName方法。 在这里我尝试用id-manufectureName从selectonemenu中选择一些值后,用id -deviceosName更新selectonemenu

另一件事,id-manufectureName的selectonemenu再次更新。

这是我的xhtml页面 -

<h:form id="main" enctype="multipart/form-data">
    <p:growl id="messages" showDetail="true" />

    <p:panel id="os_version_panel" header="Os Version">
        <h:panelGrid columns="3" title="Os Version" style="width:40%;height:20%;padding:10%;padding-left:30%">

            <h:outputLabel for="manufectureName" value="Manufecture Name:*" style="float:right;font-size:15px;width:200px" />
            <p:spacer width="20" height="50" />
            <p:selectOneMenu id="manufectureName" value="#{deviceRegistration.selectedManufacture}" effect="fade" converter="deviceManufactureConverter"
                style="width:200px">
                <f:selectItem itemLabel="Select One" itemValue="" />
                <f:selectItems value="#{deviceRegistration.manufactureNameList}" var="manufacturer" itemLabel="#{manufacturer.manufacturersName}"
                    itemValue="#{manufacturer}" />
                <p:ajax event="valueChange" update=":main:deviceosName" listener="#{deviceRegistration.updateOSName}" />
            </p:selectOneMenu>

            <h:outputLabel for="deviceosName" value="Device OS Name:*" style="float:right;font-size:15px;width:200px" />
            <p:spacer width="20" height="50" />
            <p:selectOneMenu id="deviceosName" value="#{deviceRegistration.selectedOsName}" effect="fade" converter="deviceOsNameConverter"
                style="width:200px">
                <f:selectItem itemLabel="Select One" itemValue="" />
                <f:selectItems value="#{deviceRegistration.osNamesList}" var="name" itemLabel="#{name.osName}" itemValue="#{name}" />
            </p:selectOneMenu>

            <h:outputLabel for="versionName" value="Device OS Version Name:*" style="float:right;font-size:15px;width:200px" />
            <p:spacer width="20" height="50" />
            <p:inputText id="versionName" value="#{deviceRegistration.selectedVersion.osVersionName}" required="true" label="versionName" size="30">
                <f:validateLength minimum="2" />
            </p:inputText>
            <h:outputLabel for="version" value="Device Os Version:*" style="float:right;font-size:15px;width:200px" />
            <p:spacer width="20" height="50" />
            <p:inputText id="version" value="#{deviceRegistration.selectedVersion.osVersion}" required="true" label="version" size="30">
                <f:validateLength minimum="2" />
            </p:inputText>
            <f:facet name="footer">
                <p:spacer width="20" height="80" />

                <p:commandButton id="addOsVersion" value="Add OS Version" actionListener="#{deviceRegistration.addOsVersion}"
                    action="addversion.xhtml?faces-redirect=true" ajax="false" style="float:right;" />
            </f:facet>
        </h:panelGrid>
    </p:panel>


</h:form>

3 个答案:

答案 0 :(得分:1)

  

我正在尝试通过从其他selectonemenu中选择一些值来更新selectonemenu。但是听众没有调用updateOSName方法。

如果发生转换或验证错误,可能会发生这种情况。然后将完全跳过INVOKE_APPLICATION阶段。您没有使用<p:growl autoUpdate="true">,也可能没有注意服务器日志中的警告,因此这些转换/验证错误已经引起您的注意。

确保您注意转换/验证错误。实际上你有一个很大的机会

  

main:manufectureName:验证错误:值无效

如果是这样,那么请针对所有可能的原因和适当的解决方案找到以下答案:Validation Error: Value is not valid

另一个可能的原因是您没有正确注册PrimeFaces FileUploadFilter。你有一个<h:form enctype="multipart/form-data">,默认情况下JSF不支持。但是,这不会导致这个问题,而是整个表单根本不可提交/可处理,而且基于目前提供的信息似乎并非如此。

答案 1 :(得分:0)

将菜单更改为:

<p:selectOneMenu id="manufectureName" value="#{deviceRegistration.selectedManufacture}" effect="fade" converter="deviceManufactureConverter"
    style="width:200px">
    <f:selectItem itemLabel="Select One" itemValue="" />
    <f:selectItems value="#{deviceRegistration.manufactureNameList}" var="manufacturer" itemLabel="#{manufacturer.manufacturersName}"
        itemValue="#{manufacturer}" />
    <p:ajax update="deviceosName" listener="#{deviceRegistration.updateOSName}" />
</p:selectOneMenu>

p:ajax的默认事件为change。你的监听方法被调用了吗?此外,您不需要:main:deviceosName,因为deviceosNamemanufectureName的内容相同。

答案 2 :(得分:0)

示例应用程序,工作正常。 托管bean:

package app.so.dev.web.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.AjaxBehaviorEvent;

import app.so.dev.web.model.Manufacturer;
import app.so.dev.web.model.Product;

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

    private static final long serialVersionUID = 3128349336575959334L;

    private static final String[] manufacturerNames;
    private static final String[] productNames;
    private List<Manufacturer> manufacturers;
    private List<Product> products;
    private List<Product> productsForManufacturer;
    private String selectedManufacturer;
    private String selectedProduct;

    static {
        manufacturerNames = new String[3];
        manufacturerNames[0] = "Intel";
        manufacturerNames[1] = "Asus";
        manufacturerNames[2] = "AMD";

        productNames = new String[5];
        productNames[0] = "Motherboard";
        productNames[1] = "Processor";
        productNames[2] = "Graphics card";
        productNames[3] = "NIC";
        productNames[4] = "RAM";
    }

    @PostConstruct
    public void init() {
        manufacturers = new ArrayList<Manufacturer>();

        for(String manufactureName : manufacturerNames) {
            manufacturers.add(new Manufacturer(manufactureName));
        }

        products = new ArrayList<Product>();

        for (String productName : productNames) {
            products.add(new Product(productName, getRandomManufacturerName()));
        }

        productsForManufacturer = new ArrayList<Product>();
    }

    public void updateProduct(AjaxBehaviorEvent event) {
        if(selectedManufacturer == null || selectedManufacturer.trim().length() == 0) {
            return;
        }       

        productsForManufacturer.clear();

        for(Product product : products) {
            if(product.getManufactureName().equals(selectedManufacturer)) {
                productsForManufacturer.add(product);
            }
        }           
    }

    private String getRandomManufacturerName() {
        return manufacturerNames[(int) (Math.random() * 3)];
    }

    public List<Manufacturer> getManufacturers() {
        return manufacturers;
    }

    public void setManufacturers(List<Manufacturer> manufacturers) {
        this.manufacturers = manufacturers;
    }

    public List<Product> getProductsForManufacturer() {
        return productsForManufacturer;
    }

    public void setProductsForManufacturer(List<Product> productsForManufacturer) {
        this.productsForManufacturer = productsForManufacturer;
    }

    public String getSelectedManufacturer() {
        return selectedManufacturer;
    }

    public void setSelectedManufacturer(String selectedManufacturer) {
        this.selectedManufacturer = selectedManufacturer;
    }

    public String getSelectedProduct() {
        return selectedProduct;
    }

    public void setSelectedProduct(String selectedProduct) {
        this.selectedProduct = selectedProduct;
    }       
}

和xhtml:

<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui" template="/WEB-INF/templates/globalTemplate.xhtml">

    <ui:define name="title">15369641</ui:define>
    <ui:define name="content">
        <p:growl id="growl" showDetail="true" />

        <h:form id="form">
            <p:selectOneMenu id="manufacturer" value="#{so15369641.selectedManufacturer}">
                <f:selectItem itemLabel="Select One" itemValue="" />
                <f:selectItems value="#{so15369641.manufacturers}" var="manufacturer" itemLabel="#{manufacturer.name}" itemValue="#{manufacturer.name}" />

                <p:ajax listener="#{so15369641.updateProduct}" update="product"/>
            </p:selectOneMenu>

            <p:selectOneMenu id="product" value="#{so15369641.selectedProduct}">
                <f:selectItem itemLabel="Select One" itemValue=""/>
                <f:selectItems value="#{so15369641.productsForManufacturer}" var="product" itemLabel="#{product.name}" itemValue="#{product.name}"/>
            </p:selectOneMenu>
        </h:form>
    </ui:define>

</ui:composition>

希望它会有所帮助。

环境:

  • JBoss AS7
  • JBoss org的JSF Mojarra 2.1.7
  • Primefaces 3.4.2