JSF列表根据所选值返回空指针错误。

huangapple go评论57阅读模式
英文:

JSF list depending on selected value returning nullPointer

问题

在我的jsf页面中,在对话框中,我有一个依赖于数据表中所选EntityA的EntityB列表。
当我首次加载页面时,由于一开始没有选择任何内容,它会给我一个null指针异常。有人能告诉我如何防止这种情况发生吗?

编辑:我在打开对话框的链接上添加了一个动作监听器,并且收到了这个错误:

>无法将DemandesDocsAdministratif类型的DemandesDocsAdministratif转换为javax.faces.event.ActionEvent类

JSF:

<p:commandLink value="#{demande.idDemandeDocAdministratif}"
					oncomplete="PF('dlg2').show()" process="@this"
					update=":form:pg" actionListener="#{gestionDemandesMB.fillEntityB}">
					<f:setPropertyActionListener
						target="#{gestionDemandesMB.SelectedEntityA}" value="#{demande}" 
						/>
				</p:commandLink>
<form>
<datatable>
</datatable>
    <p:dialog>
    	<p:selectOneMenu id="Signataires"
    							value="#{gestionDemandesMB.entityB}">
    							<f:selectItems value="#{gestionDemandesMB.listEntityB}"
    								var="sign" itemLabel="#{sign.libRole}"
    								itemValue="#{sign.idPoste}" />
    						</p:selectOneMenu>
    </p:dialog>
</form>

Bean:

public List<EntityA> getListEntityB() {
	if ( selectedentityA != null ){
	return entityBService.ListByentityA(selectedEntityA)
			; } else {
	return Collections.emptyList() ; }

我正在处理的Bean监听器:

public void fillSignataires(ActionEvent event)
	{
listB = entityBService.ListByentityA(selectedEntityA)

		signaRender = true ;
	}

这是获取Entity B列表的getter方法,我正在寻找一种在打开对话框时要么获取一个空列表要么只在需要时调用的方法。

英文:

In my jsf page, in a dialog, I have a list of EntityB which depends on selected EntityA in a datatable.
When I first load the page it's giving me nullPpointer exception because nothing is selected in the first place. Can anyone tell me how to prevent this?

EDIT: I added an actionlistener to the link who open the dialog
and getting this error:

>Cannot convert DemandesDocsAdministratif type DemandesDocsAdministratif to class javax.faces.event.ActionEvent

JSF:

<p:commandLink value="#{demande.idDemandeDocAdministratif}"
					oncomplete="PF('dlg2').show()" process="@this"
					update=":form:pg" actionListener="#{gestionDemandesMB.fillEntityB}">
					<f:setPropertyActionListener
						target="#{gestionDemandesMB.SelectedEntityA}" value="#{demande}" 
						/>
				</p:commandLink>
<form>
<datatable>
</datatable>
    <p:dialog>
    	<p:selectOneMenu id="Signataires"
    							value="#{gestionDemandesMB.entityB}">
    							<f:selectItems value="#{gestionDemandesMB.listEntityB}"
    								var="sign" itemLabel="#{sign.libRole}"
    								itemValue="#{sign.idPoste}" />
    						</p:selectOneMenu>
    </p:dialog>
</form>

Bean:

public List<EntityA> getListEntityB() {
	if ( selectedentityA != null ){
	return entityBService.ListByentityA(selectedEntityA)
			; } else {
	return Collections.emptyList() ; }

Bean listener that I'm working with now:

public void fillSignataires(ActionEvent event)
	{
listB = entityBService.ListByentityA(selectedEntityA)

		signaRender = true ;
	}

this is the getter of entity B list, I'm looking for a way to either get an empty list or call only when I open dialog.

答案1

得分: 0

你可以使用 rendered="#{not empty bean.list}" 来阻止在对象填充之前渲染 selectOnMenu

英文:

You could use rendered="#{not empty bean.list}" to prevent the rendering of the selectOnMenu until you have the object filled.

答案2

得分: 0

你还可以添加一个带有“选择”标签和空itemValue(例如itemValue = "")的f:selectItem。顺便提一下,永远不要有一个空列表,列表不应为null,但可以是空的。这是最佳实践。因此,您可以在bean的post construct中将您的listEntityB初始化为空列表。

<p:selectOneMenu id="Signataires" value="#{gestionDemandesMB.entityB}">
<f:selectItem itemLabel="Select" itemValue="" />
<f:selectItems value="#{gestionDemandesMB.listEntityB}"
var="sign" itemLabel="#{sign.libRole}"
itemValue="#{sign.idPoste}" />
</p:selectOneMenu>

英文:

You can also add a f:selectItem with a "Select" label and empty itemValue such as itemValue = " ". By the way, never have a null list, list shouldn't be null but it can be empty. It's the best practice. So you can initialize your listEntityB as an empty list at post construct of your bean.

    &lt;p:selectOneMenu id=&quot;Signataires&quot;
                            value=&quot;#{gestionDemandesMB.entityB}&quot;&gt;
                            &lt;f:selectItem  itemLabel=&quot;Select&quot; itemValue=&quot;&quot; /&gt;
                                var=&quot;sign&quot; itemLabel=&quot;#{sign.libRole}&quot;
                            &lt;f:selectItems value=&quot;#{gestionDemandesMB.listEntityB}&quot;
                                var=&quot;sign&quot; itemLabel=&quot;#{sign.libRole}&quot;
                                itemValue=&quot;#{sign.idPoste}&quot; /&gt;
                        &lt;/p:selectOneMenu&gt;

huangapple
  • 本文由 发表于 2020年8月28日 08:32:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63625867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定