nullの可能性があるオブジェクトの文字列属性を出力する方法
質問
seam JSFページのdataTableの一部として、1つの列には名前の出力が必要です:
<h:outputText value="#{listing.staffMember.name}"/>
問題は、「staffMember」が一部のリストではnullの場合があるため、エラーが表示されます:
javax.el.ELException: /xxxxx.xhtml @42,67 value="#{listing.staffMember.name}": Error reading 'name' on type xxxx.model.AgentStaff_$_javassist_152
値がnullの場合、テキストをレンダリングしたくない。私はこれを試しました:
<h:outputText value="#{listing.staffMember.name}" rendered="#{listing.staffMember != null}"/>
しかし、同じエラーが発生します。
nullの可能性があるオブジェクトのプロパティを出力するにはどうすればよいですか
解決
おそらく次のような三項演算子。
value="#{listing.staffMember != null ? listing.staffMember.name : 'None'}"
または c:ifタグを使用できます。
他のヒント
これを試してもらえますか(いつも私のために働いていました):
<h:outputText value="#{listing.staffMember.name}"
rendered="#{not empty listing.staffMember}"/>
nullと比較した場合の違いはわかりません。
所属していません StackOverflow