如何将不同的子类注入为 ManagedProperty JSF 2?
我是 JSF 的新手,我想知道是否可以根据不同的情况将基类的不同子类作为 MangedProperty 注入?例如,我有这个托管 bean:
@ManagedBean
@SessionScoped
public class Claim implements Serializable {
private Loss lossDetails; //need to inject one of two subclasses
}
以及以下基类:
public class Loss implements Serializable {
private String lossCause;
private String lossDescription;
}
它有两个子类:
public class AutoLoss extends Loss implements Serializable {
private List<String> vehicles;
//...
}
public class PropLoss extends Loss implements Serializable {
private String property;
private boolean weatherRelated;
//...
}
根据在我的应用程序的 JSF 页面上所做的选择,我想将其中一个子类注入为 lossDetails
Claim
托管 bean 中的 ManagedProperty。由于我无法为两个子类提供相同的托管 bean 名称,并且我不提前知道需要注入哪一个子类,所以这可以在 JSF 中完成吗?或者我应该考虑采取不同的方法?
谢谢!
I'm new to JSF and I'm wondering if it's possible to inject different subclasses of a base class as a MangedProperty, depending on different situations? For instance, I have this managed bean:
@ManagedBean
@SessionScoped
public class Claim implements Serializable {
private Loss lossDetails; //need to inject one of two subclasses
}
And the following base class:
public class Loss implements Serializable {
private String lossCause;
private String lossDescription;
}
Which has two subclasses:
public class AutoLoss extends Loss implements Serializable {
private List<String> vehicles;
//...
}
public class PropLoss extends Loss implements Serializable {
private String property;
private boolean weatherRelated;
//...
}
Depending on selections that are made on my application's JSF pages, I want to inject one of the subclasses as the lossDetails
ManagedProperty in the Claim
managed bean. Since I can't give the two subclasses the same managed bean name and I don't know ahead of time which one needs to be injected, is this something that can be accomplished in JSF? Or is there a different approach I should be considering?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能也不应该。
而是将其作为方法参数传递:
在
Claim
托管 bean 中:You can't and shouldn't.
Rather pass it as method argument instead:
With in
Claim
managed bean: