每次执行操作时,@ ViewScoped都会调用post构造

时间:2014-01-22 10:31:46

标签: jsf jsf-2 primefaces

我问了一千次,但没有人给我一个有效的解决方案。请问为什么在我的xhtml页面中,当我按下按钮的时候总是调用post构造?我该如何解决这个问题呢?请一个有效的解决方案。我已经阅读了很多答案,但它们不符合我的问题。我在1周内遇到了麻烦。 我的xhtml范围没有改变:

update of a render component that updates another component

<!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="hotel and escursions" render="@form" update=":form:Hotel :form"/>
            </h:panelGrid>


        </p:panel>




                <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:commandButton actionListener="#{addDefaultPackageBean.add}" value="Add" update=":form:Volo :form:Hotel" render="@form" process="@this,:form:Volo,:form:Location"/>










    </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 implements Serializable {

    /**
     * 
     */

    @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();
        this.elelishotel=new ArrayList<ElementDTO>();
        this.elelisescursion=new ArrayList<ElementDTO>();
        this.elelisfly=new ArrayList<ElementDTO>();
        this.availableLocations=new ArrayList<String>();
        this.flyElem=new ElementDTO();
    }

    @PostConstruct
    public void init()
    {

        System.out.print("merda init ");
        if(this.getElelisfly().size()==0){System.out.print("Voli vuoto ");}
        this.setElelis(elemMgr.getAllElements());
        this.selectedEscursions=new ElementDTO[this.getElelis().size()];
        this.fly="";
        for(ElementDTO e:elelis)
        {
                if (this.availableLocations.contains(e.getLocation())==false)
                        {

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

                        }
        }   
    }


    public String add() {
        System.out.print("entrato nell'ultimo step!il mio hotel è "+hotel);
        //System.out.print("entrato nell'ultimo step!il mio hotel è "+this.selectedEscursions[0].getName());

    /*  //assegno location al pacchetto defpackDTO
        this.defpackDTO.setLocation(this.getFlyElem().getLocation());
        //assegno location a defpackDTO
        this.defpackDTO.getElem().add(this.getFlyElem());
        //assegno hotel a defpackDTO

        this.AssignElemHotelFromSelection();
        //assegno escursioni a defpackDTO
        for(int i=0;i<this.selectedEscursions.length;i++)
        {
            this.defpackDTO.getElem().add(this.selectedEscursions[i]);
        }
        defpackMgr.save(defpackDTO);
        */
        return "/employee/index?faces-redirect=true";
    }

    public void sel()
    {
        System.out.print("ehila"+this.fly );

        this.setElelis(this.elemMgr.getAllElementsByLocation(this.defpackDTO.getLocation()));
         for(ElementDTO e:elelis)
           {
               System.out.print("elemento della location Haiti "+e.getName());
           }
         for(ElementDTO e:elelis)
           {
               System.out.print("elementisfwefsf dei voli "+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())))
                {
                    System.out.print("ho un escursione tra gli elementi con le date giuste"+e.getName());  
                    this.getElelisescursion().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(){


       this.getElelisfly().clear();
       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;
}
}

1 个答案:

答案 0 :(得分:2)

如果托管bean不是CDI工件,请使用 import javax.faces.bean.ViewScoped;