如何将不同的子类注入为 ManagedProperty JSF 2?

发布于 2024-12-06 00:53:43 字数 900 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我心暖 2024-12-13 00:53:43

你不能也不应该。

  • 无法将请求范围值作为托管属性注入会话范围 bean 中。
  • 实体不应被视为托管 bean。

而是将其作为方法参数传递:

<h:dataTable value="#{lossManager.losses}" var="loss">
    <h:column>
        <h:commandButton value="Claim" action="#{claim.doAction(loss)}" />
    </h:column>
</h:dataTable>

Claim 托管 bean 中:

public void doAction(Loss loss) {
    // ...
}

You can't and shouldn't.

  • It's not possible to inject a request scoped value as managed property in a session scoped bean.
  • Entities should not be treated as managed beans.

Rather pass it as method argument instead:

<h:dataTable value="#{lossManager.losses}" var="loss">
    <h:column>
        <h:commandButton value="Claim" action="#{claim.doAction(loss)}" />
    </h:column>
</h:dataTable>

With in Claim managed bean:

public void doAction(Loss loss) {
    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文