【问题标题】:Jsf commandbutton call javascript function then refreshing the whole pageJsf commandbutton调用javascript函数然后刷新整个页面
【发布时间】:2015-07-25 18:46:14
【问题描述】:

我在表单中有一个 jsf 页面和一个 commandButton。

<h:form>
     ...
    <h:commandButton action="#{mainViewController.showRelatedPayments()}" id="openpay" onclick="openModal();" value="Tst"></h:commandButton>
     ...
</h:form>

在同一个 jsf 页面我有一个模态 div..

 <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
     <div class="modal-dialog">
         <div class="modal-content">
              <div class="modal-header">
                   <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                   <h4 class="modal-title" id="myModalLabel">Modal titles</h4>
              </div>
              <div class="modal-body">
                   ....
                   Dynamic content must be here 
                   ....
              </div>
              <div class="modal-footer">
                   <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

              </div>
        </div>
  </div>

我还有一个包含 openModal() 函数的 javascript 文件。

function openModal() {

      $('#myModal').modal('show');
  }

当我点击按钮时,它会调用 javascript 函数并显示模态 div,但随后页面刷新和打开模态消失..

按钮也调用了我的backing bean方法:

public void showRelatedPayments() {
        LOG.info("creating data for page..");
        /*...
           ...
            ...
        */
    }

我要做的是单击按钮调用支持 bean 方法,该方法为模式内容创建数据,而不是刷新整个 jsf 页面..

这可能吗?有人可以帮我吗?

【问题讨论】:

  • 你可能应该从谷歌搜索 ajax 和 jsf 开始,我认为有一些组件/标签是为此设计的

标签: javascript jquery twitter-bootstrap jsf


【解决方案1】:
<h:commandButton action="#{mainViewController.showRelatedPayments()}" 
    id="openpay" onclick="openModal();" value="Tst" />

该代码确实是这样的:

  • 打开模式对话框。
  • 调用命令按钮操作来加载数据。
  • 重新加载整个页面。

但你实际上想要这个:

  • 调用命令按钮操作来加载数据。
  • 仅重新加载模式对话框。
  • 打开模式对话框。

您需要按所需顺序移动任务并使用 ajax 魔法来实现部分页面重新加载。 &lt;f:ajax&gt; 对此很有帮助。

<h:commandButton action="#{mainViewController.showRelatedPayments()}" 
    id="openpay" value="Tst">
    <f:ajax execute="@form" render=":relatedPayments" 
        onevent="function(data) { if (data.status == 'success') openModal(); }" />
</h:commandButton>

并将其添加到应动态更新的模态对话框的内容中。

<h:panelGroup id="relatedPayments" layout="block">
    ....
    Dynamic content must be here 
    ....
</h:panelGroup>

【讨论】:

  • 谢谢。我做的和你告诉我的完全一样,但是现在只调用了支持 bean 方法.. javascript 方法没有,并且模式没有显示.. 你能帮我解决什么问题吗?
  • 我的 javascript 函数现在看起来像这样: function openModal(data) { alert("run"); $('#myModal').modal('show'); } 并且当我按下按钮页面重新加载时,我看到一个闪烁但没有模式.. 并且没有警报消息.. :(
  • 这样生成的html看起来像。
  • 我在onevent 中犯了一个错误,我忘了把它包装在一个匿名函数中。查看更新的答案。
  • 它仍然没有出现.. :( 我的 web 控制台在我的 xhtml 的这一行显示一个 js 错误... 我真的不明白发生了什么.. :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 2012-08-13
  • 2014-03-21
  • 2015-04-24
相关资源
最近更新 更多