tags:

views:

652

answers:

3

Hello,

I have an ICEFaces we application. One page has two beans that display different things on the page.

I want to be able to notify one bean when another bean changes something on the bean so that the first bean update its content on the page.

Is that possible in ICEFaces? if so how?

Thanks,

Tam

+3  A: 

What you can do is to "inject" bean1 into bean2, so the bean2 will have access to any method present in bean1.

If you are using Spring, this can be easily done when defining the beans:

<bean id="bean1" class="foo.bar.Bean1"/>
<bean id="bean2" class="foo.bar.Bean2">
    <property id="bean1" ref="bean1"/>
</bean>

and in Java code of bean2:

public class Bean2 {

    private Bean1 bean1 = null;

    // The setter will be used by Spring to inject Bean1 in Bean2...
    public void setBean1(Bean1 bean1) {
        this.bean1 = bean1;
    }

    ...

    public void someMethod() {
        ...
        // Now, you can call the bean1 instance to update what you want...
        bean1.updateSomething();
    }

}

If you are not using Spring:

You can directly access the bean1 instance within bean2 code like that:

Bean1 bean1 = (Bean1) FacesContext.getCurrentInstance().getCurrentInstance()
    .getExternalContext().getSessionMap().get("bean1");
romaintaz
+1 this really worked well for me.
Nico
A: 

I was going to post some examples of my own work, but the guys on the ICEFaces blog already have a really good blog post of their own. Take a look.

KG
+3  A: 

As has already been noted, JSF can do simple injection as well. Something like this in your faces-config.xml file:

<managed-bean>
    <managed-bean-name>bean1</managed-bean-name>
    <managed-bean-class>org.icefaces.sample.Bean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<managed-bean>
    <managed-bean-name>bean2</managed-bean-name>
    <managed-bean-class>org.icefaces.sample.Bean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
        <property-name>bean1</property-name>
        <value>#{bean1}</value>
    </managed-property>
</managed-bean>

As for updating the user interface when bean values change, that can be triggered through regular client interaction with the page. However, if you are doing a collaborative type application (where one user's change can update values that other user's can see), then ICEfaces has a feature called Ajax Push that you can use. Check their docs for more info.

watnotte