【问题标题】:p:commandButton is reloading the page to open dialogp:commandButton 正在重新加载页面以打开对话框
【发布时间】:2016-10-02 23:40:01
【问题描述】:

所以我有这个代码:

<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  
}

【问题讨论】:

  • 我注意到,如果我将对话框和 commandButton 分成两种不同的形式,@PostConstruct 方法不会在对话框打开时调用,但在单击取消按钮时仍会调用
  • 所以你一直在谈论一个bean,但没有发布代码。请创建一个minimal reproducible example
  • 我还注意到,如果我删除对话框内的 inputText,问题就不会发生
  • 我尝试从对话框外部删除表单并按照建议在对话框内添加一个,但行为仍然存在。正如我之前所说,出于某种原因,删除 inputText 会在对话框打开和取消按钮时停止页面重新加载。但我无法解释为什么

标签: jsf primefaces


【解决方案1】:

这是因为Commandbutton 提交了表单。你可以 改成这样:

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

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

【讨论】:

  • 感谢您的回答,当我将 type="button" 添加到对话框内的“取消”按钮时,它停止提交表单,但也没有关闭模式。
  • @CesarKuehl 编辑了我的答案,所以它关闭了。如果有帮助,请接受答案并投票:)顺便说一下,这里的一些答案是完全错误的
  • 快到了!添加 type="button" 和 onclick 标签确实对取消按钮起作用,但保存按钮没有
【解决方案2】:

尝试为您的“添加服务”和“取消”命令按钮元素设置以下属性:partialSubmit="true" process="@this"。 像这样的代码:

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

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

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

【讨论】:

  • 感谢您的回答,我会尽快尝试属性。至于标签值推荐,我使用的是资源包。 'messages' 不是 bean,它是我在 faces-config.xml 上声明的 var 名称,如下所示:bundlemessages -bundle>
  • 添加属性几乎解决了它。取消按钮现在可以正常工作,但保存按钮仍然会在触发 addServiceParameterGroup 方法之前尝试重新加载页面
  • 对“保存”按钮使用相同的属性: ... partialSubmit="true" process="@this"
  • 您的页面将不再重新加载。处理完毕后,可以尝试使用【消息标签】(primefaces.org/showcase/ui/message/messages.xhtml)告知用户处理结果。希望对您有所帮助!
【解决方案3】:

如果我没记错的话,你应该把你的 Dialog 放在你的主窗体之外,在你的正文的末尾或使用 appendTo="@(body)" 参数,然后,有另一个窗体在对话框中。

【讨论】:

    【解决方案4】:

    这个问题处理了很久,终于找到原因了。 我在支持 bean 中导入的注释 ViewScoped 来自包 javax.faces.view。 正确的是 javax.faces.bean

    感谢所有花时间提供帮助的人。

    【讨论】:

      猜你喜欢
      • 2013-09-08
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多