0

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

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

这是我的 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>
4

3 回答 3

1

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

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

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

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

如果这是真的,那么针对所有可能的原因和适当的解决方案,请前往以下答案:验证错误:值无效

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

于 2013-03-13T19:40:28.363 回答
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:ajaxchange。你的监听器方法被调用了吗?你也不需要:main:deviceosName,因为deviceosName与 相同manufectureName

于 2013-03-12T21:30:05.563 回答
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
于 2013-03-13T18:12:48.573 回答