更新更新另一个组件的渲染组件

时间:2014-01-21 10:25:06

标签: jsf jsf-2 primefaces

我有一个我无法解决的问题。我有我的xhtml页面:

enter image description here

问题是我有一个表格,我插入名称并选择旅行的位置。它呈现一个外部面板“Volo”,其中selectionMenu被更新,其中一些值是根据所选位置从DB获得的。接下来我想从Volo选择中获取一个值并更新ID =“HE”的另一个面板(就是名为HotelEsc的面板)。 我在“Volo”中选择了菜单,因为我无法获得它的价值。始终给出NullPointException错误。可能问题是我正在尝试理解更新和渲染工作的方式,我错误地了解这些操作。希望对你有所帮助,谢谢你。

xhtml代码:

<!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">


<h:head>
    <title>Add a Default Package</title>
</h:head>
<h:body>
    <h:form id="form">


        <p:panel header="DefaultPackage Form">
            <h:panelGrid columns="3" id="regGrid">

                <h:outputLabel for="Name">Name:</h:outputLabel>
                <p:inputText id="Name"
                    value="#{addDefaultPackageBean.defpackDTO.name}" />
                <p:message for="Name" />

                <h:outputLabel for="location">Locations Available:</h:outputLabel>

                <h:selectOneMenu for="location"
                    value="#{addDefaultPackageBean.defpackDTO.location}">
                    <f:ajax listener="#{addDefaultPackageBean.Search()}" render="Volo"  />
                    <f:selectItems id="location"
                        value="#{addDefaultPackageBean.availableLocations}" />
                </h:selectOneMenu>

            </h:panelGrid>
        </p:panel>





        <p:panel header="Voli Disponibili per la location selezionata"
            id="Volo">

            <h:outputLabel for="Fly">Volo:</h:outputLabel>
            <h:selectOneMenu for="Fly" value="#{addDefaultPackageBean.fly}">
                <f:selectItems id="Fly" value="#{addDefaultPackageBean.elelisfly}"
                    var="ElementDTO" itemValue="#{ElementDTO.name}"
                    itemLabel="#{ElementDTO.name}" />
            </h:selectOneMenu>

        <p:commandButton action="#{addDefaultPackageBean.sel()}" value="HE" render=":form,:form:regularGrid,:form:Volo" update=":form:Volo" process="form:regGrid,@this"></p:commandButton>
        </p:panel>




        <p:panel header="HotelEsc" id="HotelEscursioni">
            <h:panelGrid columns="3" id="regularGrid">

                <h:outputLabel for="Hotel">Hotel:</h:outputLabel>
                <h:selectOneMenu for="Hotel" value="#{addDefaultPackageBean.hotel}">
                    <f:selectItems id="Hotel"
                        value="#{addDefaultPackageBean.elelishotel}" var="ElementDTO"
                        itemValue="#{ElementDTO.name}" itemLabel="#{ElementDTO.name}" />
                </h:selectOneMenu>
                <p:message for="Hotel" />

                <h:outputLabel for="Escursion">Escursioni:</h:outputLabel>
                <f:facet name="header">Clicca su view per vedere i dettagli</f:facet>
                <p:dataTable id="Escursion" var="esc"
                    value="#{addDefaultPackageBean.elelisescursion}"
                    rowKey="#{esc.name}"
                    selection="#{addDefaultPackageBean.selectedEscursions}"
                    selectionMode="multiple">
                    <p:column headerText="Nome">  #{esc.name}  </p:column>

                    <p:column headerText="Costo">  #{esc.cost}  </p:column>

                    <p:column headerText="Data Iniziale">  #{esc.startingDate}  </p:column>

                    <p:column headerText="Data Fine">  #{esc.endingDate}  </p:column>

                    <f:facet name="footer">

                    </f:facet>
                </p:dataTable>

            </h:panelGrid>
        </p:panel>

    </h:form>
</h:body>

</html>

相关bean代码:

package beans;

import java.awt.Event;
import java.io.Serializable;
import java.util.ArrayList;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.view.ViewScoped;

import elementManagement.ElementMgr;
import elementManagementDTO.ElementDTO;
import DefaultPackageManagement.DefaultPackageMgr;
import DefaultPackageManagementDTO.DefaultPackageDTO;


@ManagedBean(name="addDefaultPackageBean") //come viene richiamato 
@ViewScoped
public class AddDefaultPackageBean{

    /**
     * 
     */

    @EJB
    private DefaultPackageMgr defpackMgr;
    private DefaultPackageDTO defpackDTO;
    private ArrayList<ElementDTO> elelisfly;
    private ArrayList<ElementDTO> elelishotel;
    private ArrayList<ElementDTO> elelisescursion;
    private ArrayList<ElementDTO> elelis;
    private ElementDTO[] selectedEscursions;
    private String fly;
    private String hotel;
    private boolean flag=true;
    private boolean flagdopo=true;
    private ArrayList<String> availableLocations;
    private ElementDTO flyElem;

    @EJB
    private ElementMgr elemMgr;



    public ElementDTO[] getSelectedEscursions() {
        return selectedEscursions;
    }
    public void setSelectedEscursions(ElementDTO[] selectedEscursions) {
        this.selectedEscursions = selectedEscursions;
    }
    public AddDefaultPackageBean() {        
        defpackDTO = new DefaultPackageDTO();
    }

    @PostConstruct
    public void init()
    {
        this.elelisfly=new ArrayList<ElementDTO>();
        this.elelishotel=new ArrayList<ElementDTO>();
        this.elelisescursion=new ArrayList<ElementDTO>();
        this.setElelis(elemMgr.getAllElements());
        this.availableLocations=new ArrayList<String>();
        this.flyElem=new ElementDTO();
        for(ElementDTO e:elelis)
        {
                if (this.availableLocations.contains(e.getLocation())==false)
                        {

                         this.availableLocations.add(e.getLocation());  

                        }
        }   
    }


    public String add() {
        this.AssignElemFlyFromSelection();
        this.AssignElemHotelFromSelection();
        this.AssignElemEscursionFromSelection();
        defpackMgr.save(defpackDTO);
        return "/employee/index?faces-redirect=true";
    }

    public void sel()
    {
        System.out.print("ehila" );
        this.setElelis(this.elemMgr.getAllElementsByLocation(this.defpackDTO.getLocation()));
         for(ElementDTO e:elelis)
           {
               System.out.print("elemento della location Haiti "+e.getName());
           }
        this.AssignElemFlyFromSelection();
        System.out.print(this.fly+"Il volo selezionato per la location è "+this.getFlyElem().getName() );
        this.elelisescursion.clear();
        this.elelishotel.clear();

        for(ElementDTO e:elelis)
        {
            if(e.getType().equals("Hotel"))
            {
                 System.out.print("ho un hotel tra gli elementi "+e.getName() );
                if(e.getStartingDate().after(this.flyElem.getStartingDate())&&((e.getEndingDate().before(this.flyElem.getEndingDate()))))
                {
                 System.out.print("ho un hotel tra gli elementi con le date giuste"+e.getName());
                    this.getElelishotel().add(e);

                }
            }
            else
            {

            if(e.getType().equals("Escursion"))
            {
                if(e.getStartingDate().after(this.flyElem.getStartingDate())&&(e.getEndingDate().before(this.flyElem.getEndingDate())))
                {

                    this.getElelishotel().add(e);

                }

            }

            }
        }
    this.setFlag(true);
    this.setFlagdopo(true); 


    }


    public DefaultPackageDTO getDefpackDTO() {
        return defpackDTO;
    }
    public void setDefpackDTO(DefaultPackageDTO defpackDTO) {
        this.defpackDTO = defpackDTO;
    }
    public ArrayList<ElementDTO> getElelisfly() {
        return elelisfly;
    }
    public void setElelisfly(ArrayList<ElementDTO> elelisfly) {
        this.elelisfly = elelisfly;
    }
    public ArrayList<ElementDTO> getElelishotel() {
        return elelishotel;
    }
    public void setElelishotel(ArrayList<ElementDTO> elelishotel) {
        this.elelishotel = elelishotel;
    }
    public ArrayList<ElementDTO> getElelisescursion() {
        return elelisescursion;
    }
    public void setElelisescursion(ArrayList<ElementDTO> elelisescursion) {
        this.elelisescursion = elelisescursion;
    }
    public String getFly() {
        return fly;
    }
    public void setFly(String fly) {
        this.fly = fly;
    }
    public String getHotel() {
        return hotel;
    }
    public void setHotel(String hotel) {
        this.hotel = hotel;
    }

    private void AssignElemFlyFromSelection()
    {
        for (ElementDTO elem:this.elelisfly)
        {
            if(elem.getName().equals(this.fly))
            {
                this.flyElem=elem;
            }
        }
    }

    private void AssignElemHotelFromSelection()
    {
        for (ElementDTO elem:this.elelishotel)
        {
            if(elem.getName().equals(this.hotel))
            {
                this.defpackDTO.getElem().add(elem);
            }
        }
    }

    private void AssignElemEscursionFromSelection()
    {
        for(int i=0;i<selectedEscursions.length;i++)
        {
                this.defpackDTO.getElem().add(selectedEscursions[i]);
        }
    }

   public void Search(){



       String s=defpackDTO.getLocation();
       System.out.print("luogo scelto "+s);
       this.setElelis(this.elemMgr.getAllElementsByLocation(s));
       for(ElementDTO e:elelis)
       {
           System.out.print("aggiungo volo "+e.getName());
           if(e.getType().equals("Flight"))
           {
               this.getElelisfly().add(e);
               System.out.print("aggiungo volo "+e.getName());
           }
       }
       this.setFlag(true);
   }

public ArrayList<ElementDTO> getElelis() {
    return elelis;
}
public void setElelis(ArrayList<ElementDTO> elelis) {
    this.elelis = elelis;
}
public ArrayList<String> getAvailableLocations() {
    return availableLocations;
}
public void setAvailableLocations(ArrayList<String> availableLocations) {
    this.availableLocations = availableLocations;
}
public Boolean getFlag() {
    return flag;
}
public void setFlag(Boolean flag) {
    this.flag = flag;
}
public boolean isFlagdopo() {
    return flagdopo;
}
public void setFlagdopo(boolean flagdopo) {
    this.flagdopo = flagdopo;
}
public ElementDTO getFlyElem() {
    return flyElem;
}
public void setFlyElem(ElementDTO flyElem) {
    this.flyElem = flyElem;
}
}

2 个答案:

答案 0 :(得分:1)

你在某种程度上误解了某些属性的使用。

我会在你的代码上发布我的笔记。

Volo部分内的p:commandButton令人困惑

  1. 它没有render属性,而是update,您应该删除render
  2. 您所拥有的更新是更新当前部分Volo是您需要的吗?
  3. 它只处理第一部分regGrid,这意味着Volo部分内的值不会更新到managedBean内的模型(导致NullPointer),是你的意思想要?不这么认为。
  4. 你的“HE”按钮应链接此(如果我理解你想要的话)

    <p:commandButton action="#{addDefaultPackageBean.sel()}" 
                     value="HE"
                     update="HotelEscursioni" process="@parent">
    </p:commandButton>
    

    希望这有助于......

答案 1 :(得分:0)

经过多次尝试,我明白了渲染/更新的工作原理。我希望这里的xhtml解决方案可以帮助其他人解决同样的问题: 必须对要刷新的组件进行更新(例如,在我的情况下,根据选择中的选定值给出更新的目标);必须使用进程来指定用于实现操作的组件。

<!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">


<h:head>
    <title>Add a Default Package</title>
</h:head>

<h:body>
    <h:form id="form">


                <p:outputLabel for="Name">Name:</p:outputLabel>
                <p:inputText id="Name"
                    value="#{addDefaultPackageBean.defpackDTO.name}" />
                <p:message for="Name" />

                <p:outputLabel for="Location">Locations Available:</p:outputLabel>
                <p:selectOneMenu id="Location" 
                    value="#{addDefaultPackageBean.defpackDTO.location}">
                    <f:selectItems 
                        value="#{addDefaultPackageBean.availableLocations}" />
                </p:selectOneMenu>
            <p:commandButton action="#{addDefaultPackageBean.Search()}" value="Ciao" render=":form:Volo" update=":form:Volo :form"></p:commandButton>







        <p:panel header="Voli Disponibili per la location selezionata"
            id="Volo" rendered="#{addDefaultPackageBean.flag}">

            <h:panelGrid columns="3" id="voloGrid">
            <p:outputLabel for="Volare">Volo:</p:outputLabel>
            <p:selectOneMenu for="Volare" value="#{addDefaultPackageBean.fly}">
                <f:selectItems id="Volare" value="#{addDefaultPackageBean.elelisfly}"
                    var="Ciao" itemValue="#{Ciao.name}" 
                    itemLabel="#{Ciao.name}" />
            </p:selectOneMenu>

            <p:commandButton actionListener="#{addDefaultPackageBean.sel()}" value="hesef" update=":form:voloGrid :form:HotelEscursioni" process=":form:Location,:form:voloGrid,@this"/>
            </h:panelGrid>


        </p:panel>

        <p:panel header="HotelEscurs" id="HotelEscursioni" rendered="#{addDefaultPackageBean.flagdopo}">


                <p:outputLabel for="Hotel">Hotel:</p:outputLabel>
                <p:selectOneMenu for="Hotel" value="#{addDefaultPackageBean.hotel}">
                    <f:selectItems id="Hotel"
                        value="#{addDefaultPackageBean.elelishotel}" var="ElementDTO"
                        itemValue="#{ElementDTO.name}" itemLabel="#{ElementDTO.name}" />
                </p:selectOneMenu>
                <p:message for="Hotel" />






                <p:dataTable id="Escursion" var="esc"
                    value="#{addDefaultPackageBean.elelisescursion}"
                    rowKey="#{esc.name}"
                    selection="#{addDefaultPackageBean.selectedEscursions}"
                    selectionMode="multiple">
                    <p:column headerText="Nome">  #{esc.name}  </p:column>

                    <p:column headerText="Costo">  #{esc.cost}  </p:column>

                    <p:column headerText="Data Iniziale">  #{esc.startingDate}  </p:column>

                    <p:column headerText="Data Fine">  #{esc.endingDate}  </p:column>

                    </p:dataTable>

                </p:panel>








    </h:form>
</h:body>

</html>