代码之家  ›  专栏  ›  技术社区  ›  Srikanth Ganji

为什么要将process=“@this”显式添加到p:commandButton以调用操作?

  •  19
  • Srikanth Ganji  · 技术社区  · 11 年前

    我知道我们需要明确添加 process="@this" 获得 p:commandbutton 动作被调用,我也知道process属性默认为 @form 在黄金脸上。

    由于进程默认为 @表单 按钮是否应该与表单中的其他元素一起处理,并且应该调用其操作。

    有人能解释这背后的确切原因吗?

    1 回复  |  直到 11 年前
        1
  •  51
  •   Jens Piegsa    8 年前

    过程 @form 指当前形式的 commandLink/Button
    过程 @this 指的是 命令链接/按钮 。检查以下代码。

    进程.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>  
    

    处理Bean.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);
        }
    }
    

    让我们来看看用户输入框

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

    点击 button1 全部的 JSF component(Eg : id, name, address) 整个表格将被处理。输出将为:

    001
    Jone
    London
    

    点击 button2 ,过程将是自己的(例如:button2)。没有用于的进程 id, name, address 。输出将为:

    null
    null
    null
    

    点击 button3 全部的 JSF component(Eg : name) 整个 panel_1 按钮3 将被处理。输出将为:

    null
    Jone
    null
    

    不调用您的操作方法?调用前可能存在验证或转换失败。