开发者

How to reference Java Collection entry from JSF 2 ManagedProperty?

开发者 https://www.devze.com 2023-04-06 11:19 出处:网络
I have an application that has a bean that holds a list of Contacts which are referenced from various domain objects throughout the application:

I have an application that has a bean that holds a list of Contacts which are referenced from various domain objects throughout the application:

@ManagedBean
@SessionScoped
public class ContactHolder implements Serializable {
    private ArrayList<Contact> contactsList;
    //getters and setters...
}

Contacts can be created and added to contactsList from various JSF pages and entries in this list need to be referenced as a ManagedProperty in various domain objects throughout the application. For instance, look at some of my domain objects:

@ManagedBean
public class Claim implements Serializable {
    private Contact insured; //needs to reference entry in contactsList
}

@ManagedBean
public class Vehicle implements Serializable {
    private Contact driver; //needs to reference entry in开发者_如何学Python contactsList
}

Because users will have the choice to select from an existing Contact or creating a new one for each domain object, the same Contact entry in contactsList could be referenced from more than one domain object. Is there a way to reference/inject a specific Java Collection entry as a ManagedProperty in JSF 2? Or is there a better approach to handling this scenario?

Thanks!


In theory, I see ways wherein you remember and pass list indexes around, but this will end up to be pretty clumsy.

Much better, if you're targeting a Servlet 3.0 / EL 2.2 capable container (Tomcat 7, Glassfish 3, etc) wherein invoking methods with additional arguments is supported, or can install JBoss EL which adds the same enhancement for EL 2.1, then you should be able to just pass that object as action method argument.

E.g.

<h:dataTable value="#{contactHolder.contacts}" var="contact">
    <h:column>
        <h:commandButton value="Claim" action="#{claim.doAction(contact)}" />
    </h:column>
</h:dataTable>

with in Claim managed bean:

public void doAction(Contact contact) {
    // ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号