我正在开发一个项目,以前的程序员在任何地方重复代码。我需要对一些JSF代码进行更改,但不想在每个地方复制我的更改。我认为必须有更好的方法。
这是场景;有多个dataTable显示在几个不同的选项卡上,所有选项卡都在同一页面上。除了id和值/数据源不同之外,每个dataTable都是相同的。我需要使用与之相同的技术,因此我无法更改版本或任何内容。
设定:
如何在包含dataTable时外化dataTable并定义id和值?这样,我不必为每张桌子多次进行相同的更改。
选项卡式面板,其中每个Edit.jsp页面包含下面的dataTable。
<odc:tabbedPanel id="sampleTab" styleClass="tabbedPanel_3"
slantActiveRight="4" slantActiveLeft="4" slantInactiveRight="4"
slantInactiveLeft="4" width="100%" height="100%"
variableTabLength="true" showBackNextButton="true" showTabs="true">
<!-- Start Test panel -->
<odc:bfPanel id="testsDisplayPanel" name="IPA"
showFinishCancelButton="false">
<jsp:include flush="true" page="editHeader.jsp" />
<jsp:include flush="true" page="testEdit.jsp" />
</odc:bfPanel>
<!-- Start Elisa panel -->
<odc:bfPanel id="elisaDisplayPanel" name="ELISA"
showFinishCancelButton="false">
<jsp:include flush="true" page="editHeader.jsp" />
<jsp:include flush="true" page="elisaEdit.jsp" />
</odc:bfPanel>
<!-- Start NMO Tests panel -->
<odc:bfPanel id="nmoDisplayTestPanel" name="NMO IF"
showFinishCancelButton="false">
<jsp:include flush="true" page="editHeader.jsp" />
<jsp:include flush="true" page="nmoTestEdit.jsp" />
</odc:bfPanel>
<!-- Start IF Tests panel -->
<odc:bfPanel id="ifDisplayTestsPanel" name="PNEO IF"
showFinishCancelButton="false" >
<jsp:include flush="true" page="editHeader.jsp" />
<jsp:include flush="true" page="ifTestEdit.jsp" />
</odc:bfPanel>
<f:facet name="back">
<hx:commandExButton id="tabbedPanel1back" style="display:none"
type="submit" value="< Back" />
</f:facet>
<f:facet name="next">
<hx:commandExButton id="tabbedPanel1next" style="display:none"
type="submit" value="Next >" />
</f:facet>
<f:facet name="finish">
<hx:commandExButton id="tabbedPanel1finish" style="display:none"
type="submit" value="Finish" />
</f:facet>
<f:facet name="cancel">
<hx:commandExButton id="tabbedPanel1cancel" style="display:none"
type="submit" value="Cancel" />
</f:facet>
</odc:tabbedPanel>
表示例(我缩短了它,它确实有20列。)
<h:dataTable id="elisaTestResults" styleClass="data-entry"
value="#{pc_SampleEdit.sample.test.elisaTestResults}" var="elisaTestResults" border="0"
cellpadding="0" cellspacing="0" rowClasses="none, grey-highlight">
<!-- hidden input column -->
<h:column>
<h:inputHidden value="#{elisaTestResults.testResultsIk}" />
<h:inputHidden value="#{elisaTestResults.test.testIk}" />
<h:inputHidden value="#{elisaTestResults.dirty}" id="mustSaveT1" />
</h:column>
<!-- Ordered column -->
<h:column id="testResultsOrderedCol">
<f:facet name="header">
<h:outputText id="ot01" styleClass="outputText" value="" />
</f:facet>
<h:selectOneMenu id="ordered" styleClass="selectOneMenu"
rendered="#{elisaTestResults.ordered != null}"
value="#{elisaTestResults.ordered}"
onchange="setDirty(this, 'T1')"
converter="javax.faces.convert.IntegerConverter">
<f:selectItems value="#{pc_UtilRefLists.ordered}" />
</h:selectOneMenu>
</h:column>
<!-- Test Name Column -->
<h:column id="testResultsTestNameCol">
<f:facet name="header">
<h:outputText id="ot02" styleClass="outputText" value="Test Name" />
</f:facet>
<h:outputText id="testResultsTestName" style="font-weight: bold"
value="#{elisaTestResults.testName}" />
</h:column>
<!-- Result1 Column -->
<h:column id="testResultsResult1Col">
<f:facet name="header">
<h:outputText id="ot03" styleClass="outputText" value="Result1" />
</f:facet>
<t:inputText id="testResultsResult1" style="font-weight: bold"
value="#{elisaTestResults.result1}" size="7" maxlength="15"
onchange="setTestDate(this, 'T1')"
displayValueOnly="#{elisaTestResults.valid1 != null && elisaTestResults.valid1}"
displayValueOnlyStyleClass="outputText"/>
</h:column>
<!-- Date1 Column -->
<h:column id="testResultsDate1Col">
<f:facet name="header">
<h:outputText id="ot04" styleClass="outputText" value="Date" />
</f:facet>
<h:panelGroup rendered="#{!elisaTestResults.valid1}">
<h:inputText id="testResultsDate1__DT" styleClass="inputText"
value="#{elisaTestResults.date1}"
onchange="setDirty(this, 'T1')"
size="7">
<f:convertDateTime type="date" pattern="MM/dd/yyyy" />
</h:inputText>
<hx:graphicImageEx id="testResultsDate1__DTCal"
styleClass="graphicImageEx" value="/shared/images/schedule.gif" />
<h:message styleClass="error-message" for="testResultsDate1__DT" />
</h:panelGroup>
<h:outputText id="testResultsDate1" styleClass="outputText"
value="#{elisaTestResults.date1}" rendered="#{elisaTestResults.valid1}">
<f:convertDateTime type="date" pattern="MM/dd/yyyy" />
</h:outputText>
</h:column>
<!-- Valid Result1 Column -->
<h:column id="testResultsValid1Col">
<f:facet name="header">
<h:outputText id="ot05" styleClass="outputText" value="OK" />
</f:facet>
<h:selectBooleanCheckbox id="testResultsValid1"
onchange="setDirty(this, 'T1')"
styleClass="selectBooleanCheckbox" value="#{elisaTestResults.valid1}" />
</h:column>
<!-- Result2 Column -->
<h:column id="testResultsResult2Col">
<f:facet name="header">
<h:outputText id="ot06" styleClass="outputText" value="Result2" />
</f:facet>
<t:inputText id="testResultsResult2" style="font-weight: bold"
value="#{elisaTestResults.result2}" size="7" maxlength="15"
onchange="setTestDate(this, 'T1')"
displayValueOnly="#{elisaTestResults.valid2 != null && elisaTestResults.valid2}"
displayValueOnlyStyleClass="outputText"/>
</h:column>
</h:dataTable>
答案 0 :(得分:0)
为了解决这个问题,我将jsf代码分解为几个jsp文件,并将它们插入到需要的地方。
dataTableElisa.jsp
<h:dataTable id="elisaTestResults" styleClass="data-entry"
value="#{pc_SampleEdit.sample.test.elisaTestResults}" var="elisaTestResults" border="0"
cellpadding="0" cellspacing="0" rowClasses="none, grey-highlight">
<jsp:include flush="true" page="innerTable.jsp"/>
</h:dataTable>
dataTableNmo.jsp
<h:dataTable id="nmoTestResults" styleClass="data-entry"
value="#{pc_SampleEdit.sample.test.nmoTestResults}" var="elisaTestResults" border="0"
cellpadding="0" cellspacing="0" rowClasses="none, grey-highlight">
<jsp:include flush="true" page="innerTable.jsp"/>
</h:dataTable>
innerTable.jsp
<!-- hidden input column -->
<h:column>
<h:inputHidden value="#{elisaTestResults.testResultsIk}" />
<h:inputHidden value="#{elisaTestResults.test.testIk}" />
<h:inputHidden value="#{elisaTestResults.dirty}" id="mustSaveT1" />
</h:column>
<!-- Ordered column -->
<h:column id="testResultsOrderedCol">
<f:facet name="header">
<h:outputText id="ot01" styleClass="outputText" value="" />
</f:facet>
<h:selectOneMenu id="ordered" styleClass="selectOneMenu"
rendered="#{elisaTestResults.ordered != null}"
value="#{elisaTestResults.ordered}"
onchange="setDirty(this, 'T1')"
converter="javax.faces.convert.IntegerConverter">
<f:selectItems value="#{pc_UtilRefLists.ordered}" />
</h:selectOneMenu>
</h:column>
<!-- Test Name Column -->
<h:column id="testResultsTestNameCol">
<f:facet name="header">
<h:outputText id="ot02" styleClass="outputText" value="Test Name" />
</f:facet>
<h:outputText id="testResultsTestName" style="font-weight: bold"
value="#{elisaTestResults.testName}" />
</h:column>
<!-- Result1 Column -->
<h:column id="testResultsResult1Col">
<f:facet name="header">
<h:outputText id="ot03" styleClass="outputText" value="Result1" />
</f:facet>
<t:inputText id="testResultsResult1" style="font-weight: bold"
value="#{elisaTestResults.result1}" size="7" maxlength="15"
onchange="setTestDate(this, 'T1')"
displayValueOnly="#{elisaTestResults.valid1 != null && elisaTestResults.valid1}"
displayValueOnlyStyleClass="outputText"/>
</h:column>
<!-- Date1 Column -->
<h:column id="testResultsDate1Col">
<f:facet name="header">
<h:outputText id="ot04" styleClass="outputText" value="Date" />
</f:facet>
<h:panelGroup rendered="#{!elisaTestResults.valid1}">
<h:inputText id="testResultsDate1__DT" styleClass="inputText"
value="#{elisaTestResults.date1}"
onchange="setDirty(this, 'T1')"
size="7">
<f:convertDateTime type="date" pattern="MM/dd/yyyy" />
</h:inputText>
<hx:graphicImageEx id="testResultsDate1__DTCal"
styleClass="graphicImageEx" value="/shared/images/schedule.gif" />
<h:message styleClass="error-message" for="testResultsDate1__DT" />
</h:panelGroup>
<h:outputText id="testResultsDate1" styleClass="outputText"
value="#{elisaTestResults.date1}" rendered="#{elisaTestResults.valid1}">
<f:convertDateTime type="date" pattern="MM/dd/yyyy" />
</h:outputText>
</h:column>
<!-- Valid Result1 Column -->
<h:column id="testResultsValid1Col">
<f:facet name="header">
<h:outputText id="ot05" styleClass="outputText" value="OK" />
</f:facet>
<h:selectBooleanCheckbox id="testResultsValid1"
onchange="setDirty(this, 'T1')"
styleClass="selectBooleanCheckbox" value="#{elisaTestResults.valid1}" />
</h:column>
<!-- Result2 Column -->
<h:column id="testResultsResult2Col">
<f:facet name="header">
<h:outputText id="ot06" styleClass="outputText" value="Result2" />
</f:facet>
<t:inputText id="testResultsResult2" style="font-weight: bold"
value="#{elisaTestResults.result2}" size="7" maxlength="15"
onchange="setTestDate(this, 'T1')"
displayValueOnly="#{elisaTestResults.valid2 != null && elisaTestResults.valid2}"
displayValueOnlyStyleClass="outputText"/>
</h:column>