如何使用JSF2处理多态? [英] How to handle polymorphism with JSF2?

查看:95
本文介绍了如何使用JSF2处理多态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示/编辑多态实体。

I need to display/edit polymorphic entities.

我的抽象类是
我的具体课程是 PhysicalPerson MoralPerson

每个具体的课程都有自己的自定义属性。

Each concrete class has its own custom attributes.

如何根据实体类使用适当的显示/编辑(复合)组件?

How can I use the appropriate display/edit (composite) component according to entity class ?

谢谢! :)

推荐答案

EL中没有 instanceof 这样的东西。但是,您可以(ab)使用 Object#getClass() 并访问 Class 。然后只需确定组件的呈现属性中的结果。

There is no such thing as instanceof in EL. You can however (ab)use Object#getClass() and access the getters of Class in EL as well. Then just determine the outcome in the component's rendered attribute.

<h:panelGroup rendered="#{entity.class.name == 'com.example.PhysicalPerson'}">
    <p>According to Class#getName(), this is a PhysicalPerson.</p>
</h:panelGroup>
<h:panelGroup rendered="#{entity.class.simpleName == 'MoralPerson'}">
    <p>According to Class#getSimpleName(), this is a MoralPerson.</p>
</h:panelGroup>

然而,自定义EL功能会更干净。请注意,由于EL中允许的属性名限制极为严格,因此上述内容不适用于Tomcat 7和克隆。不再允许使用Java保留的文字,例如 class 。您需要#{entity ['class']。name} 等等。

A custom EL function would be more clean however. Note that the above doesn't work on Tomcat 7 and clones due to extremely restrictive restrictions of allowed propertynames in EL. Java reserved literals such as class are not allowed anymore. You'd need #{entity['class'].name} and so on instead.

这篇关于如何使用JSF2处理多态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆