2

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!

4

1 回答 1

2

你不能也不应该。

  • 不可能将请求范围的值作为托管属性注入到会话范围的 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) {
    // ...
}
于 2011-09-22T15:28:16.233 回答