如何在 Java Server Faces 中处理 bootstrap 模式?

How to handle bootstrap modal in Java Server Faces?

我喜欢 composite-components 的原理,但是这个和 bootstraps modal 不能一起工作。

管理多个自定义复合组件对话框并在 JSF 中使用此示例的最佳实践是什么-Table。 将所选行中的托管 Bean 值传递到对话框。

如果我将所有内容都写在一个页面文件中,这只对我有用。 看最后一个。

bootstrapModal.xhtml复合组件中的模态框

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:cc="http://xmlns.jcp.org/jsf/composite">

<cc:interface>

    <cc:attribute name="title" />
    <cc:attribute name="linkNameLable" />
    <cc:attribute name="linkNameValue" />
    <cc:attribute name="urlNameLable" />
    <cc:attribute name="urlNameValue" />
    <cc:attribute name="saveButtonText" />
    <cc:attribute name="saveButtonAction" 
                  method-signature="java.lang.String action()" />

</cc:interface>

<cc:implementation>

    <div id="#{cc.clientId}" class="modal fade myModal" tabindex="-1" role="dialog" aria-labelledby="myModal" aria-hidden="true" >
        <div class="modal-dialog">  
            <div class="modal-content">
                <div class="modal-body">
                    <h:form>
                        <h:outputLabel   value="#{cc.attrs.linkNameLable}" />
                        <h:inputText     value="#{cc.attrs.linkNameValue}" />
                        <h:outputLabel   value="#{cc.attrs.urlNameLable}" />
                        <h:inputText     value="#{cc.attrs.urlNameValue}"  /> 
                        <h:commandButton value="#{cc.attrs.saveButtonText}" action="#{cc.attrs.saveButtonAction}" />
                    </h:form>
                </div>
            </div>
        </div>
    </div>

</cc:implementation>
</html>

包装 div 不起作用。

<div id=#{cc.clientId}>...</div>

我也试过把id传给里面的表单

<h:form id=#{cc.clientId}

view.xhtml 复合组件不起作用。 f:ajax 无法从复合组件呈现 id


...
<script>
    function showModal() {
        $('#myModal').modal('show');
    }
</script>
...
<h:dataTable id="myTable" value="#{linkController.linkList}" var="o">   
...          
    <h:column>
        <f:facet name="header">Actions</f:facet>
        <h:form>
            <h:commandLink value="edit" onclick="showModal()" action="#{linkController.setLinkFromParam}">
                <f:ajax render="myModal value1 value2" />
                <f:param name="name" value="#{o.name}"  />
                <f:param name="url"  value="#{o.url}"   />
            </h:commandLink>
        </h:form>
    </h:column>
</h:dataTable>

<!-- 1.outside the Table rendering works fine -->

<h:outputText id="value1" value="#{linkController.name}" /><br />
<h:outputText id="value2" value="#{linkController.url}" />


<!-- 2.The render for this id does not work -->

<mahi:bootstrapModal    title="Edit Link" 
                        id="myModal" 
                        linkNameLable="Link Name:" 
                        linkNameValue="#{linkController.name}"
                        urlNameLable="URL:"
                        urlNameValue="#{linkController.url}"
                        saveButtonText="Save"
                        saveButtonAction="#{linkController.updateLink(link)}" />

view.xhtml 与复合组件的内容一起工作正常。 因为我可以直接用 id="myModalForm".

渲染 h:form
...
<script>
    function showModal() {
        $('#myModal').modal('show');
    }
</script>
...
<h:dataTable id="myTable" value="#{linkController.linkList}" var="o">   
...          
    <h:column>
        <f:facet name="header">Actions</f:facet>
        <h:form>
            <h:commandLink value="edit" onclick="showModal()" action="#{linkController.setLinkFromParam}">
                <f:ajax render="myModalForm" />
                <f:param name="name" value="#{o.name}"  />
                <f:param name="url"  value="#{o.url}"   />
            </h:commandLink>
        </h:form>
    </h:column>
</h:dataTable>

<!-- The Content from the Custom Composite Component works -->

<div id="myModal" class="modal fade myModal" tabindex="-1" role="dialog" aria-labelledby="myLinkModal" aria-hidden="true" >
    <div class="modal-dialog">  
        <div class="modal-content">
            <div class="modal-body">
                <h:form id="myModalForm">
                    <h:outputLabel   value="Name:" />
                    <h:inputText     value="#{linkController.name}" />
                    <h:outputLabel   value="URL:" />
                    <h:inputText     value="#{linkController.url}" /> 
                    <h:commandButton value="Save" action="#{linkController.saveLink(link)}" />
                </h:form>
            </div>
        </div>
    </div>
</div>

简单的解决方案:

将 class 添加到您的模式中以便调用它,并将您的模式称为 class 而不是像 id :

模态 :>

<div id="#{cc.clientId}" class="modal fade myModal"

脚本:>

<script>
function showModal() {
    $('.myModal').modal('show');
}