p:commandButton正在重新加载页面以打开对话框

时间:2016-06-03 03:08:17

标签: jsf primefaces

所以我有这段代码:

<h:form id="serviceCustomFormForm">
        <p:dialog id="parameterGroupAddDialog" widgetVar="parameterGroupAddDialog" header="#{messages.addParameterGroup}" modal="true" resizable="false">       
            <p:inputText value="#{serviceCustomFormBean.serviceParameterGroup.name}" styleClass="Wid90" />
            <br />
            <br />

            <p:commandButton value="#{messages.save}" styleClass="Fright BlueButton" update="serviceCustomFormForm" actionListener="#{serviceCustomFormBean.addServiceParameterGroup}" oncomplete="PF('parameterGroupAddDialog').hide()" />
            <p:commandButton value="#{messages.cancel}" styleClass="Fright RedButton" oncomplete="PF('parameterGroupAddDialog').hide()"/>
        </p:dialog>

        <div class="Container100">
            <div class="ContainerIndent">
                <p:commandButton value="#{messages.addParameterGroup}" icon="fa fa-plus-circle" styleClass="Fright CyanButton FloatNoneOnMobile" oncomplete="PF('parameterGroupAddDialog').show()" />
                <div class="EmptyBox10 ShowOnMobile"></div>             
            </div>
        </div>
</h:form>

首次加载页面时,将调用@PostConstruct方法。 当我单击commandButton打开对话框时,它再次被调用。当我按下对话框内的取消按钮时,它会再次被调用。

在应用程序的其他部分中不会发生此行为,我无法看到我在这里缺少的内容。

更新:根据要求,Bean代码在此处:

@ManagedBean
@ViewScoped
public final class ServiceCustomFormBean implements Serializable {

   private ServiceParameterGroup serviceParameterGroup = new ServiceParameterGroup();

   // Other attributes

   @PostConstruct
   private void init() {
       // Reads attributes sent from previous page  
   }

   public void addServiceParameterGroup() {
      // Saves the serviceParameterGroup to database
   }

   // Getters and Setters  
}

4 个答案:

答案 0 :(得分:2)

这是因为Commandbutton提交了表单。您可以 改为:

<p:commandButton type="button" ...onclick="PF('parameterGroupAddDialog').hide()" 

类型button告诉primefaces不要提交表单。如果表单未提交,则永远不会调用oncomplete。所以它是onclick

答案 1 :(得分:0)

尝试将以下属性设置为“添加服务”&#39;和&#39;取消&#39; commandButton elements:partialSubmit =&#34; true&#34;过程=&#34; @这&#34 ;. 像这样的代码:

<commandButton value="#{messages.addParameterGroup}" ... partialSubmit="true" process="@this" ... />

默认情况下,pf commandButtons尝试提交整个表单,而在这两种情况下,您只想调用被调用的方法而不进行提交。有了这个,你要说的是你不想全部提交(partialSubmit = true),并且你只想处理按钮本身的调用(process = @ this)。也许那是你的问题。

作为一个额外的评论,我不认为从bean获取按钮的标签值是一个好主意(除非你想有意识地动态更改标签),因为你最终会做出过多的请求豆子。最好尝试使用消息属性文件,如http://www.mkyong.com/jsf2/jsf-2-0-and-resource-bundles-example/中所述。

答案 2 :(得分:0)

如果我没记错的话,你应该把你的Dialog放在你的主要表格之外,身体的末端或使用 appendTo =&#34; @(body)&#34; param,然后,在对话框中有另一个表单。

答案 3 :(得分:0)

经过长时间处理这个问题,我终于找到了原因。 我在辅助bean中导入的注释ViewScoped来自包 javax.faces.view 。 正确的是 javax.faces.bean

感谢所有花时间寻求帮助的人。

相关问题