What can , and be used for?

前端 未结 2 1105
星月不相逢
星月不相逢 2020-11-21 05:17

Can anyone clarify how we can use in general, or a in real world example, this snippet?


    

        
2条回答
  •  北恋
    北恋 (楼主)
    2020-11-21 05:36

    Send params from View to an other View, from Sender View to Receiver View use viewParam and includeViewParams=true

    In Sender

    1. Declare params to be sent. We can send String, Object,…

    Sender.xhtml

    
          
    
    
    1. We’re going send param ID, it will be included with “includeViewParams=true” in return String of click button event Click button fire senderMB.clickBtnDetail(dto) with dto from senderMB._arrData

    Sender.xhtml

    
          
    
    

    In senderMB.clickBtnDetail(dto) we assign _strID with argument we got from button event (dto), here this is Sender_DTO and assign to senderMB._strID

    Sender_MB.java
        public String clickBtnDetail(sender_DTO sender_dto) {
            this._strID = sender_dto.getStrID();
            return "Receiver?faces-redirect=true&includeViewParams=true";
        }
    

    The link when clicked will become http://localhost:8080/my_project/view/Receiver.xhtml?*ID=12345*

    In Recever

    1. Get viewParam Receiver.xhtml In Receiver we declare f:viewParam to get param from get request (receive), the name of param of receiver must be the same with sender (page)

    Receiver.xhtml

    
    

    It will get param ID from sender View and assign to receiver_MB._strID

    1. Use viewParam In Receiver, we want to use this param in sql query before the page render, so that we use preRenderView event. We are not going to use constructor because constructor will be invoked before viewParam is received So that we add

    Receiver.xhtml

    
    

    into f:metadata tag

    Receiver.xhtml

    
    
    
    
    

    Now we want to use this param in our read database method, it is available to use

    Receiver_MB.java
    public void preRenderView(ComponentSystemEvent event) throws Exception {
            if (FacesContext.getCurrentInstance().isPostback()) {
                return;
            }
            readFromDatabase();
        }
    private void readFromDatabase() {
    //use _strID to read and set property   
    }
    

提交回复
热议问题