HADEV
HADEV

Reputation: 447

set a managed bean property from composite component

I have model class called Person and a view scope managed bean PersonController which contain a list of person

I have created a composite component that take this list of persons . What i want to do is to set the List of persons in other managed bean called TestCompositeComponent from the composite component directly .. Any solution ?.. This is my code :

@ManagedBean
@ViewScoped
public class PersonController {

    private List<Person> persons;

    public List<Person> getPersons() {
        return persons;
    }
    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }

    @PostConstruct
    public void init() {
        persons = new ArrayList<>();
        Person person1 = new Person();
        person1.setFirstname("blah");
        person1.setLastname("blah");

        Person person2 = new Person();
        person2.setFirstname("blah");
        person2.setLastname("blah");

        persons.add(person1);
        persons.add(person2);

    }

}



@ManagedBean
@ViewScoped
public class TestCompositeComponentController  {


    private List<Person> persons;

    public List<Person> getPersons() {
        return persons;
    }

    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }


}



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:composite="http://java.sun.com/jsf/composite">

<composite:interface componentType="testCompositeController">

    <composite:attribute name="persons" />


</composite:interface>

<composite:implementation>

    <h:outputText value="composite"></h:outputText>
</composite:implementation>

</html>


<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:test="http://java.sun.com/jsf/composite/test"
    xmlns:f="http://java.sun.com/jsf/core">
<h:head></h:head>
<h:body>
    <test:test persons="#{personController.persons}" />
</h:body>
</html>

Upvotes: 2

Views: 460

Answers (1)

Yamada
Yamada

Reputation: 723

Do you want to change the List persons passed from the PersonController to the TestCompositeComponentController back to PersonController?

If this is the case, change the scope of PersonController to @SessionScoped and inject it to TestCompositeComponentController. This way you will be able to modify the persons in the PersonController.

@ManagedBean
@ViewScoped
public class TestCompositeComponentController  {


    private List<Person> persons;
    @Inject PersonController personController;

    public List<Person> getPersons() {
        return persons;
    }

    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }

    public void updateTheOtherList() {
       personController.setPersons(this.persons);
    }
}

In a real case scenario, the modifications in your composite component would be persisted in a database and when you navigate back the changes would be visible by a new fetch on the person list.

Upvotes: 0

Related Questions