<p:ajax>事件不会在<p:selectonemenu>的</p:selectonemenu> </p:ajax>的ValueChangeEvent中触发

时间:2014-03-16 19:45:28

标签: jsf-2 primefaces selectonemenu

问题出在我模板的内容部分。当我在标签中设置它们时,内容页面的ValueChangeEvent不再会触发。我无法获得SelectOneMenu中所选项目的值,即使我使用omnifaces转换器因为无法达到CityChaged事件。当我尝试不使用模板时,这个问题不会发生,并且还可以获得持久值。请给我一些建议,我通过使用模板得到了类似的错误。谢谢。

这是我的模板页面。

<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">

<f:view contentType="text/html" id="fview">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <f:metadata>
    <ui:insert name="metadata" />
    </f:metadata>

    <h:head>
          <ui:insert name="head"/>
          <h:outputStylesheet name="css/default.css"/>
    </h:head>

    <h:body>
    <p:ajaxStatus onstart="statusDialog.show();" onsuccess="statusDialog.hide();"  />

        <p:layout fullPage="true" id = "layout1">  
        <p:ajax event="toggle" listener="#{layoutBean.handleToggle}"/>  
        <p:ajax event="close" listener="#{layoutBean.handleClose}" />  

        <p:layoutUnit id= "contentLayout1" position="north" size="100" resizable="false">  
            <ui:include src="/WEB-INF/templates/header.xhtml"/>
        </p:layoutUnit>  

        <p:layoutUnit id= "contentLayout3" position="west" size="300" resizable="true" 
            header="Main Menu" style="height:580px;overflow:hidden;">  
            <ui:include src="/WEB-INF/templates/menu.xhtml"/>
        </p:layoutUnit>  

         <p:layoutUnit id= "contentLayout4" position="center">  
            <ui:insert name="content"/>
         </p:layoutUnit> 

        <p:layoutUnit  id= "contentLayout2" position="south" resizable="true">  
            <ui:include src="/WEB-INF/templates/footer.xhtml"/>
        </p:layoutUnit>  

    </p:layout>

    </h:body>
</f:view>
</html>

这是我的内容页面。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
      template="/WEB-INF/templates/BasicTemplate.xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">


<ui:define name="content">
    <h:form>
        <p:panel header = "Delivery Charges Entry" style="width:930px;">
            <h:panelGrid columns = "4" width="500" border="0">

                <h:outputText value="City ID"/>
                <p:selectOneMenu id= "cboCity" converter="omnifaces.SelectItemsIndexConverter"
                    value = "#{ManageDeliveryCharges.selectedCity}" required = "true"
                    panelStyle="width:150px" effect = "fade" style = "width:187px" var = "pg" >

                    <p:ajax event = "valueChange" listener ="#{ManageDeliveryCharges.CityChanged}"/>
                    <f:selectItem itemValue="#{null}" itemLabel="Select-One" noSelectionOption="true" />
                    <f:selectItems value = "#{ManageDeliveryCharges.citySelectedItems}" var = "city"/>

                    <p:column>
                        #{pg.cityID}
                    </p:column>   

                    <p:column>
                        #{pg.cityName}
                    </p:column>

                </p:selectOneMenu> 
                <p:message for="cboCity"/>

            </h:panelGrid>  

        </p:panel>
    <h:form>

</ui:define>
</ui:composition>       

这是我的支持豆。

@ManagedBean(name = "ManageDeliveryCharges")
@RequestScoped
public class ManageDeliveryCharges{

    private City city;
    private List<SelectItem> citySelectedItems = null;
    private City selectedCity;
    private List<City> cityList;

    @ManagedProperty("#{TownshipService}")
    private ITownshipService townshipService;


    @PostConstruct
    public void Page_Load(){
        prepareForAdd();

        cityList = cityService.findAllCity();
        citySelectedItems =new ArrayList<SelectItem>();

        for(City city:cityList){
            SelectItem item =new SelectItem(city,city.getCityName());
            citySelectedItems.add(item);
        }
    }


    public void CityChanged(ValueChangeEvent ex){
        System.out.println("City ID is " + selectedCity.getCityID() + "City Name is " + selectedCity.getCityName());
        deliveryCharges.setCity((City)((UIOutput) ex.getSource()).getValue());


    }   

    public List<SelectItem> getCitySelectedItems() {
        return citySelectedItems;
    }

    public void setCitySelectedItems(List<SelectItem> citySelectedItems) {
        this.citySelectedItems = citySelectedItems;
    }

    public List<City> getCityList() {
        return cityList;
    }

    public void setCityList(List<City> cityList) {
        this.cityList = cityList;
    }

    public void setCityService(ICityService cityService) {
        this.cityService = cityService;
    }

    public IDeliveryChargesService getDeliveryChargesService() {
        return deliveryChargesService;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }


}

UPDATE1。 CityChanged方法实际上是这样的。

public void CityChanged(AjaxBehaviorEvent ex){
        System.out.println("City ID is " + selectedCity.getCityID() +     "City Name is " + selectedCity.getCityName());
        deliveryCharges.setCity((City)((UIOutput)    ex.getSource()).getValue());

2 个答案:

答案 0 :(得分:1)

你的问题并不完全清楚,但有些事情看起来并不正确:

  1. 您在监听器中使用<p:ajax/>ValueChangeEvent是不正确的。提供ValueChangeEvent以用作组件上正确定义的valueChangeListener的一部分。所以你真正应该拥有的是:

    p:selectOneMenu id="cboCity" valueChangeListener="#{ManageDeliveryCharges.CityChanged}" converter="omnifaces.SelectItemsIndexConverter"
                value="#{ManageDeliveryCharges.selectedCity}" required="true"
                panelStyle="width:150px" effect ="fade" style="width:187px" var="pg"/> 
    

    使用上面定义的valueChangeListener,您可以使用辅助bean中定义的侦听器。这导致......

  2. 仅在提交整个表单时才会触发valueChangeListener,即不是ajax请求。同样,<p:ajax/>在你拥有它的地方不合适。你应该改为:

    <p:selectOneMenu id= "cboCity" onchange="submit();"/>
    

    onchange此处将触发整个表单的提交

  3. <p:ajax/>没有valueChange个活动,只有change

答案 1 :(得分:1)

您必须使用p:commandButton组件提供的事件。有关详细信息 primefaces-p Tag selectOneMenu

p:commandButton只有一个事件,即onchange。替换为以下行。

<p:ajax event = "onchange" listener ="#{ManageDeliveryCharges.CityChanged}"/>

例如,p:inputText支持onblur, onchange, onclick, ondblclick, onfocus, onkeydown, onkeypress, onkeyup, onmousedown, onmousemove, onmouseout, onmouseover, onmouseup, onselect等事件在p:ajax属性event组件中使用这些事件。

事件名称以开始开始,然后点击ctrl + space显示支持的事件。