Twitter Bootstrap + JSF2

10,208

For anyone having the same problem, this is how I got it to work:

  • you have to open the modal by javascript yourself.
  • if you don't want the modal to disappear on page-reload, you have to use <f:ajax /> to prevent page-reload
  • the only way I found to get the variable (here: #{_member}) to a backing bean is to evaluate the EL expression in the ajax-listener

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>

Backing Bean:

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

This way, the modal will open and you can access the #{_member} from the dataTable via #{ajaxBean.member} in the modal.

Sometimes the modal won't close on first click this way if you hit the "close" button. If so, you can replace it with

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

I'm almost certain that there is a more elegant way of doing this. If anyone knows how I'd be glad to hear from him/her.

Share:
10,208

Related videos on Youtube

user1682594
Author by

user1682594

Updated on September 15, 2022

Comments

  • user1682594
    user1682594 over 1 year

    I'm trying to incorporate twitter-boostrap into one of first JSF2 project.Finally managed to get it working following example from http://rkovacevic.blogspot.com/2012/05/jsf-2-twitter-bootstrap.html

    My question is how to get the selected index from the datatable so that when I click on the edit button is shows the modal form with the selected information

    My thinking was to replace the normal html href tag by h:link so that I can add an action event to set the selected record in my managed bean,but the h:link outcome is not picking up the "#myModal" reference,the URL of h:link is missing "#myModal" at the end.

    Hope this makes sense

    <!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…</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>