正确使用primefaces的process和update

process属性作用于服务端,用于指定在表单提交或Ajax请求时,哪些组件需要在JSF的生命周期中被处理。它有以下几个值:

  • @this:表示只处理当前组件。
  • @form:表示处理当前表单内的所有组件。

update属性作用于客户端,用于指定在Ajax请求完成后,HTML DOM树的哪些部分需要被更新以响应表单的提交或组件的变化。它有以下几个值:

  • @this:表示只更新当前组件。
  • @form:表示更新当前表单内的所有组件。
  • 组件ID,表示更新指定ID的HTML元素。

下面讲一个具体的示例。

XML 复制代码
<h:form id="dialogs">
    <p:dialog header="计量单位" showEffect="fade" modal="true" widgetVar="manageUnitDialog"
              closeOnEscape="true"  position="center" styleClass="s-360">               
        <p:outputPanel id="manage-unit-content">
            <p:outputPanel rendered="#{not empty unitBacking.current}">
                <div class="formgrid grid ui-fluid">
                    <div class="col-12 md:col-12 lg:col-12">
                        <div class="field">
                            <p:outputLabel for="name" >单位名称</p:outputLabel>
                            <p:inputText id="name" value="#{unitBacking.current.name}" autocomplete="off"/>
                            <h:message for="name" class="error"/>
                        </div>
                    </div>
                </div>
            </p:outputPanel>
        </p:outputPanel>
        <p:hotkey bind="ctrl+s" update="form:messages" action="#{unitBacking.save()}"/>
        <f:facet name="footer">
            <div class="dialog-header-button-bar">
                <p:commandButton value="保存" icon="pi pi-save" 
                                 actionListener="#{unitBacking.save}" 
                                 process="@form" 
                                 update="dialogs:manage-unit-content"/>
                <p:commandButton value="取消" icon="pi pi-times" onclick="PF('manageUnitDialog').hide()"
                                 styleClass="ui-button-secondary" type="button" />
            </div>
        </f:facet>
    </p:dialog>
    
    
    <p:confirmDialog widgetVar="deleteUnitDialog" showEffect="fade" closeOnEscape="true"
                     message="删除后不可恢复,请谨慎操作......" header="提示" severity="warn">                
        <p:commandButton value="删除" icon="pi pi-trash"
                         actionListener="#{unitBacking.delete(unitBacking.current.id)}"
                         process="@this"
                         oncomplete="PF('deleteUnitDialog').hide()"
                         styleClass="ui-button-danger"/>
        <p:commandButton value="取消" type="button"  icon="pi pi-times"
                         onclick="PF('deleteUnitDialog').hide()" styleClass="ui-button-secondary"/>
    </p:confirmDialog>
</h:form>

actionListener表示仅在当前页面上操作,不跳转页面;若要跳转页面则使用action。

process="@form" 表示处理事个表单,用于在修改数 据、添加数据 等操作。在保存、更新按钮用@form。

update="dialogs:manage-unit-content" 表示与表单验证结合,若没有此选项则表单验证失效。update的值是面板的ID值。

process="@this" 表示后台处理,不需要处理整个表单,通常用于删除某条记录、修改某条记录前加载它。在修改按钮删除按钮用@this。

相关推荐
桦说编程1 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅3 小时前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者4 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺4 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart5 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
NE_STOP6 小时前
MyBatis-mybatis入门与增删改查
java
孟陬10 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
想用offer打牌10 小时前
一站式了解四种限流算法
java·后端·go
华仔啊10 小时前
Java 开发千万别给布尔变量加 is 前缀!很容易背锅
java