我有一个带风险矩阵的应用程序。标题是5个严重性(来自我的示例中的severitiesBean
)和5个可能性(来自我示例中的likelihoodsBean
)。 (标题在文献中使用不同的名称,例如暴露或概率或后果。)在每个单元格中都有风险评级(从riskRatingsBean
列举)。为了让您了解它在视觉上的外观,有一个示例here。
我能够很好地显示矩阵,似乎所有人都在询问关于SO的相关问题。但是,我想显示并允许系统管理员编辑网格中每个单元格的风险。 h:或p :( PrimeFaces)中的selectOneMenu
下拉列表似乎是允许选择的好方法。
这是JSF:
<h:panelGroup id="editArea">
<table border="1" style="border-collapse:collapse;" class="riskMatrix">
<tr>
<td rowspan="2" colspan="2"></td>
<th colspan="5">Severity</th>
</tr>
<tr>
<!-- Severities going across the top -->
<ui:repeat var="severity" value="#{severitiesBean.severities}">
<th style="text-align: center;">
<h:outputText value="#{severity.name}"/>
</th>
</ui:repeat>
</tr>
<ui:repeat id="likelihoodLoop" var="likelihood" value="#{likelihoodsBean.likelihoods}" varStatus="lkhdvar">
<tr>
<ui:fragment rendered="#{lkhdvar.index == 0}">
<th rowspan="5">Likelihood</th>
</ui:fragment>
<!-- Likelihoods going down the side -->
<th>
<h:outputText value="#{likelihood.name}"/>
</th>
<!-- The rating for each cell going across -->
<ui:repeat id="severitiesLoop" var="severity" value="#{severitiesBean.severities}">
<td class="#{riskRatingsBean.riskRatings.get(riskMatrixBean.riskMatrix[severity.id-1][likelihood.id-1]-1).cssClass}" style="text-align: center;">
<p:selectOneMenu
id="RiskRatingDropDown" effect="none"
value="#{riskMatrixBean.riskMatrix[severity.id-1][likelihood.id-1]}">
<f:selectItems value="#{riskRatingsBean.riskRatings}" var="rating" itemLabel="#{rating.name}" itemValue="#{rating.id}"/>
</p:selectOneMenu>
</td>
</ui:repeat>
</tr>
</ui:repeat>
</table>
</h:panelGroup>
<h:messages id="riskMatrixErrorMessages"/>
<p:commandButton id="SaveButton" value="Update" actionListener="#{riskMatrixBean.saveRiskMatrix}"
update="editArea"/>
在riskMatrixBean
中,矩阵的定义如下:
public int[][] getRiskMatrix()
{
return riskMatrix;
}
这一切都完美展现。但是,我希望能够更新值。当我点击提交按钮时,我得到一个例外:
javax.faces.component.UpdateModelException: java.lang.ClassCastException: Unable to add an object of type [java.lang.Integer] to an array of objects of type [int]
at javax.faces.component.UIInput.updateModel(UIInput.java:866)
at javax.faces.component.UIInput.processUpdates(UIInput.java:749)
at com.sun.faces.facelets.component.UIRepeat.process(UIRepeat.java:633)
at com.sun.faces.facelets.component.UIRepeat.processUpdates(UIRepeat.java:880)
at com.sun.faces.facelets.component.UIRepeat.process(UIRepeat.java:633)
at com.sun.faces.facelets.component.UIRepeat.processUpdates(UIRepeat.java:880)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1291)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1291)
at javax.faces.component.UIForm.processUpdates(UIForm.java:281)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1291)
at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:1254)
at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:78)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:650)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:357)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:581)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: Unable to add an object of type [java.lang.Integer] to an array of objects of type [int]
at javax.el.ArrayELResolver.setValue(ArrayELResolver.java:96)
at com.sun.faces.el.DemuxCompositeELResolver._setValue(DemuxCompositeELResolver.java:255)
at com.sun.faces.el.DemuxCompositeELResolver.setValue(DemuxCompositeELResolver.java:281)
at org.apache.el.parser.AstValue.setValue(AstValue.java:218)
at org.apache.el.ValueExpressionImpl.setValue(ValueExpressionImpl.java:253)
at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:131)
at javax.faces.component.UIInput.updateModel(UIInput.java:832)
... 33 more
我不知道自己做错了什么。显然updateModel
期间的updateModelValuesPhase
不知道如何处理int[][]
以及我如何指定selectOneMenu
value
属性。我怀疑这种方法有问题,但我不确定是什么。也许这与嵌套的ui:repeat
有关?我尝试更改为使用Integer[][]
和List<List<Integer>>
,但仍然没有运气,尽管我可能在此过程中做了其他错误。我愿意根据需要更改我的数据类型。
提前致谢!