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

我与影子孤独终老i 提交于 2020-01-10 10:35:06

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/22234918/why-to-add-process-this-explicitly-to-pcommandbutton-to-get-action-invoked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!