3

我正在尝试将 twitter-boostrap 合并到第一个 JSF2 项目中。最后设法让它按照http://rkovacevic.blogspot.com/2012/05/jsf-2-twitter-bootstrap.html的示例工作

我的问题是如何从数据表中获取选定的索引,以便当我单击编辑按钮时显示带有选定信息的模态表单

我的想法是用 h:link 替换普通的 html href 标签,这样我就可以添加一个动作事件来在我的托管 bean 中设置选定的记录,但是 h:link 结果没有选择“#myModal”引用, h:link 的 URL 最后缺少“#myModal”。

希望这是有道理的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">

  <ui:composition template="WEB-INF/templates/default.xhtml">
    <ui:define name="content">
      <div class="well">
        <h2>Members</h2>
        <br />
        <h:panelGroup rendered="#{empty members}">
          <em>No registered members.</em>
        </h:panelGroup>

        <h:dataTable id="dataTable" var="_member" value="#{members.memberList}"
            rendered="#{not empty members}" styleClass="table table-striped table-bordered">
          <h:column>
            <f:facet name="header">Id</f:facet>
            #{_member.memberId}
          </h:column>
          <h:column>
            <f:facet name="header">Name</f:facet>
            #{_member.firstName}
          </h:column>
          <h:column>
            <f:facet name="header">Email</f:facet>
            #{_member.lastname}
          </h:column>
          <h:column>
            <f:facet name="header">Phone #</f:facet>
            #{_member.contactNumber}
          </h:column>
          <h:column>
            <f:facet name="header">REST URL</f:facet>
            <a href="#{request.contextPath}/rest/members/#{_member.memberId}">/rest/members/#{_member.memberId}</a>
          </h:column>
          <h:column>
            <f:facet name="header">Action</f:facet>
            <a href="#myModal" role="button" class="btn" data-toggle="modal">Edit</a>
          </h:column>
          <h:column>
            <f:facet name="header">Action</f:facet>
            <h:link href="#myModal"  role="button" class="btn" data-toggle="modal" value="Edit"></h:link>
          </h:column>
        </h:dataTable>
      </div>

      <!-- Button to trigger modal -->
      <a href="#myModal" role="button" class="btn" data-toggle="modal">Launch demo modal</a>

      <!-- Modal -->
      <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h3 id="myModalLabel">Modal header</h3>
        </div>
        <div class="modal-body">
          <p>One fine body…&lt;/p>
        </div>
        <div class="modal-footer">
          <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
          <button class="btn btn-primary">Save changes</button>
        </div>
      </div>
    </ui:define>
  </ui:composition>
</html>
4

1 回答 1

2

对于任何有同样问题的人,这就是我让它工作的方式:

  • 您必须自己通过 javascript 打开模式。
  • 如果您不希望模式在页面重新加载时消失,则必须使用<f:ajax />来防止页面重新加载
  • 我发现将变量(此处:)获取#{_member}到支持 bean 的唯一方法是评估 ajax-listener 中的 EL 表达式

xhtml/javascript:

<h:form>
  <h:dataTable id="dataTable" var="_member" value="#{members.memberList}"
                                            rendered="#{not empty members}"
                                            styleClass="table table-striped table-bordered">
    <h:column>
       <f:facet name="header">Action</f:facet>
       <h:commandLink value="Edit">
           <!-- the listener sets the variable in backing bean and onevent opens the modal -->
           <!-- you also have to render the form inside the modal, so the values get updated with new ones from backing bean -->
           <f:ajax listener="#{ajaxBean.handleEvent}" onevent="openModal('myModal');" render=":modalForm" />
       </h:commandLink>
    </h:column>
  </h:dataTable>
</h:form>

<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <h:form id="modalForm>
       <div class="modal-header">
           <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
           <h3 id="myModalLabel">Modal header</h3>
       </div>
       <div class="modal-body">
          <p>Use backing Bean property here... <h:outputText value="#{ajaxBean.member.name}</p>
       </div>
       <div class="modal-footer">
           <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
           <button class="btn btn-primary">Save changes</button>
       </div>
    </h:form>
</div>
<script type="text/javascript">
    function openModal(modalName) {
       $(modalName).modal('show');
       return false;
    }
</script>

支持豆:

package somePackage;

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;

@ManagedBean
public class AjaxBean {
    private Member member;



    public final void handleEvent(final AjaxBehaviorEvent event) {
        //get the member from the FacesContext.
        FacesContext context = FacesContext.getCurrentInstance();
        this.member = context.getApplication().evaluateExpressionGet(context, "#{_member}", Member.class);
    }

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }
}

这样,模态将打开,您可以通过模态中的#{ajaxBean.member} 从数据表中访问#{_member}。

如果您点击“关闭”按钮,有时模态不会在第一次单击时关闭。如果是这样,您可以将其替换为

<h:commandLink onclick="$(#myModal).modal('hide');" />

我几乎可以肯定有一种更优雅的方法可以做到这一点。如果有人知道我会很高兴收到他/她的来信。

于 2013-08-27T21:09:24.890 回答