防止JSF中的代码重复

时间:2014-04-09 07:54:07

标签: jsf optimization jsf-2 primefaces

大家好,我想知道我的问题是否有解决方案

基本上我正在使用超过10页重复的代码部分,而不是仅仅复制和粘贴代码我想知道我是否可以将其存储在模板文件中并在每次需要使用它来防止时调用它代码在所有页面上重复。唯一的问题是我需要更改每个页面上<p:tooltip部分的值

这是我希望添加到主文件并不断使用应用程序的代码

                    <p:column>  
                        <br></br>
                        <p:selectOneRadio id="radioButtons" value="#{formBean.number}" layout="grid" columns="1"   required = "True"
                                          requiredMessage="#{bundle.messageSelectMarkRange}" > 
                            <f:selectItem itemLabel="0 - 19" itemValue="1" /> 
                            <f:selectItem itemLabel="20 - 39" itemValue="2" />  
                            <f:selectItem itemLabel="40 - 49 " itemValue="4" />
                            <f:selectItem itemLabel="50 - 59" itemValue="5" />
                            <f:selectItem itemLabel="60 - 69" itemValue="6" />  
                            <f:selectItem itemLabel="70 - 79" itemValue="7" />  
                            <f:selectItem itemLabel="80 - 100" itemValue="8" />  
                            <p:ajax process="@this" update="mySpinnerPanel" />
                        </p:selectOneRadio>  
                    </p:column>
                    <p:column>
                        <p>
                            <p:button id="help0-19" icon="ui-icon-help">                           
                            </p:button>   
                            <p:tooltip for="help0-19" value="No statement or very generalised with vague aims or objectives" 
                                       showEffect="clip" hideEffect="explode" /> 
                        </p>
                        <p>
                            <p:button id="help20-39" icon="ui-icon-help">                           
                            </p:button>   
                            <p:tooltip for="help20-39" value="No statement or very generalised with vague aims or objectives" 
                                       showEffect="clip" hideEffect="explode" /> 
                        </p>
                        <p>
                            <p:button id="help40-49" icon="ui-icon-help">                           
                            </p:button>   
                            <p:tooltip for="help40-49" value="Problem topic and aims and objectives apparent Shows limited understanding and analysis" 
                                       showEffect="clip" hideEffect="explode" /> 
                        </p>
                        <p>
                            <p:button id="help50-59" icon="ui-icon-help">                           
                            </p:button>   
                            <p:tooltip for="help50-59" value="Problem topic and aims and objectives well presented, showing understanding and analysis. Key aspects of the context are covered"
                                       showEffect="clip" hideEffect="explode" /> 
                        </p>
                        <p>
                            <p:button id="help60-69" icon="ui-icon-help">                           
                            </p:button>   
                            <p:tooltip for="help60-69" value="Problem topic and aims and objectives well framed and viewed in wider context"
                                       showEffect="clip" hideEffect="explode" /> 
                        </p>
                        <p>
                            <p:button id="help70-79" icon="ui-icon-help">                           
                            </p:button>   
                            <p:tooltip for="help70-79" value="Problem is clearly shown to be challenging or shows originality and confidence in criticising assumptions"
                                       showEffect="clip" hideEffect="explode" /> 
                        </p>
                        <p>
                            <p:button id="help80-100" icon="ui-icon-help">                           
                            </p:button>   
                            <p:tooltip for="help80-100" value="Analysis of problem adds new insights"
                                       showEffect="clip" hideEffect="explode" /> 
                        </p>
                    </p:column>
                    <p:column>
                        <p:outputPanel id = "mySpinnerPanel" layout ="pageDirection">    
                            <!-- look into validation for this to prevent any unwanted values, better than the min max etc -->
                            <p>
                                <p:spinner id="ajaxspinner0-19" value="#{markingBean.markToEdit.markSectionOne}" 
                                           stepFactor = "1"  min="0" max="19" 
                                           disabled = "#{formBean.number != 1}" >  
                                    <p:ajax update="ajaxspinnervalue " process="@this" />  
                                </p:spinner> 

                            </p>
                            <p>
                                <p:spinner id="ajaxspinner20-39" value="#{markingBean.markToEdit.markSectionOne}" 
                                           stepFactor = "1"  min="20" max="39" disabled = "#{formBean.number != 2}">  
                                    <p:ajax update="ajaxspinnervalue" process="@this" />  
                                </p:spinner> 

                            </p>
                            <p>
                                <p:spinner id="ajaxspinner40-49" value="#{markingBean.markToEdit.markSectionOne}" 
                                           stepFactor = "1"  min="40" max="49"  disabled = "#{formBean.number != 4}">  
                                    <p:ajax update="ajaxspinnervalue" process="@this" />  
                                </p:spinner> 

                            </p>
                            <p>
                                <p:spinner id="ajaxspinner50-59" value="#{markingBean.markToEdit.markSectionOne}" 
                                           stepFactor = "1"  min="50" max="59" disabled = "#{formBean.number != 5}">  
                                    <p:ajax update="ajaxspinnervalue" process="@this" />  
                                </p:spinner> 

                            </p>            
                            <p>
                                <p:spinner id="ajaxspinner60-69" value="#{markingBean.markToEdit.markSectionOne}" 
                                           stepFactor = "1"  min="60" max="69" disabled = "#{formBean.number != 6}">  
                                    <p:ajax update="ajaxspinnervalue" process="@this" />  
                                </p:spinner> 

                            </p>
                            <p>
                                <p:spinner id="ajaxspinner70-79" value="#{markingBean.markToEdit.markSectionOne}" 
                                           stepFactor = "1"  min="70" max="79" disabled = "#{formBean.number != 7}">  
                                    <p:ajax update="ajaxspinnervalue" process="@this" />  
                                </p:spinner> 
                            </p>
                            <p>
                                <!--
                                     This acts as markingBean.getMarkSectionOne.getSpinnerField() when rendering
                             and as markingBean.getMarkSectionOne.setSpinnerField(spinner.getValue()) when sending the request to the server
                                -->
                                <p:spinner id="ajaxspinner80-100" value="#{markingBean.markToEdit.markSectionOne}"
                                           stepFactor="1"  min="80" max="100" disabled="#{formBean.number != 8}">
                                    <p:ajax update="ajaxspinnervalue" process="@this" />
                                </p:spinner>
                            </p>
                        </p:outputPanel>  
                    </p:column>

                    <f:facet name="footer">  
                        Total mark for this section is:
                        <h:outputText id="ajaxspinnervalue" value="#{markingBean.markToEdit.markSectionOne}" /> 
                    </f:facet>
                </p:panelGrid>
            </p>
            <!-- testing to make sure the correct value is passed  --> 
            <p>
                <p:commandButton value="#{bundle.buttonNextSection}"  icon ="ui-icon-disk" update="displayMark :growl" oncomplete="PF('dlg').show()" /> 
            </p>
            <p:dialog header="Confirm Mark" widgetVar="dlg" showEffect="fold" hideEffect="fold">  
                <h:panelGrid id="displayMark" columns="2" cellpadding="5">  
                    <h:outputText value="Mark to be submitted: " />  
                    <h:outputText value="#{markingBean.markToEdit.markSectionOne}" />  
                    <p:commandButton id="save"
                                     value="#{bundle.buttonSave}"
                                     actionListener ="#{markingBean.editMark}"
                                     update=":growl"
                                     icon="ui-icon-disk"/>
                </h:panelGrid>  
            </p:dialog>  

并且需要更改的唯一代码部分是每个p:button上的p:tootip

这可能吗?因为我认为它会简化应用程序,特别是新页面的生成

1 个答案:

答案 0 :(得分:2)

您可以使用<ui:include>。将已重复的html代码移动到名为commonForm.xhml的文件中。它应该看起来像这样

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core">

<p:column>
    <br></br>
    <p:selectOneRadio id="radioButtons" value="#{formBean.number}"
        layout="grid" columns="1" required="True"

    .......

        <p:button id="help20-39" icon="ui-icon-help">
        </p:button>
        <p:tooltip for="#{tooltipBean.tooltip20_30}"
            ....

请注意,工具提示从新Bean中获取了它的文本,我称之为TooltipBean。

@ManagedBean
@ViewScoped
public class TooltipBean implements Serializable {

private String tooltip20_30;

public String getTooltip20_30() {
    return tooltip20_30;
}

public void setTooltip20_30(String tooltip20_30) {
    this.tooltip20_30 = tooltip20_30;
}

public void setupForPageA(){
    tooltip20_30 = "No statement or very generalised with vague aims or objectives";
}

public void setupForPageB(){
    tooltip20_30 = "All Your Base are Belong to us";
}

您可以在要包含常用表单的每个xhtml页面中以不同方式设置tooltipBean

例如,您可以将它包含在pageA.xhtml中,如下所示:

<f:event listener="#{tooltipBean.setupForPageA}" type="preRenderView" />    
...
<ui:include src="../common/commonForm.xhtml"/>