Search code examples
formsjsfprimefacescommandbutton

Why to add process="@this" explicitly to p:commandButton to get action invoked?


I know that we need to add explicitly process="@this" to get the p:commandbutton action get invoked and I also know that process attribute defaults to @form in primefaces.

Since process is defaulted to @form shouldn't the button also get processed along with the other elements in the form and its action should get invoked.

Can anyone explain the exact reason behind this?


Solution

  • Process @form mean the current form of the commandLink/Button
    Process @this mean the current component of the commandLink/Button. Check below code.

    process.xhtml

    <h:form id="form1">
        <h:inputText value="#{ProcessBean.id}" id="id"/><br/>
        <h:panelGroup id="panel_1">
            <h:inputText value="#{ProcessBean.name}" id="name"/><br/>
        </h:panelGroup>
        <h:panelGroup id="panel_2">
            <h:inputText value="#{ProcessBean.address}"/>
            <br/>
            <p:commandButton process="@form" value="Btm1" id="button1" action="#{ProcessBean.show}"/><!-- Default -->
            <p:commandButton process="@this" value="Btm2" id="button2" action="#{ProcessBean.show}"/>
            <p:commandButton process="@this form1:panel_1" value="Btm3" id="button3" action="#{ProcessBean.show}"/>
        </h:panelGroup>
    </h:form>  
    

    ProcessBean.java

    @ManagedBean(name = "ProcessBean")
    public class ProcessBean {
        private String id;
        private String name;
        private String address;
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
    
        public void show() {
            System.out.println(id);
            System.out.println(name);
            System.out.println(address);
        }
    }
    

    Let's user input inputbox

    001     -> id
    Jone    -> name
    London  -> address
    

    Click button1, all JSF component(Eg : id, name, address) entire of the form will be process. Output will be :

    001
    Jone
    London
    

    Click button2, The process will be itself (Eg : button2). No process for id, name, address. Output will be:

    null
    null
    null
    

    Click button3, all JSF component(Eg : name) entire of the panel_1 and button3 will be process. Output will be :

    null
    Jone
    null
    

    Does not invoke your action method? There might be validation or conversion failed before invoke.