Feedforward
Feedforward

Reputation: 4879

Reload bean property

Let's say, I have JSF page with common dropdown menu on the top (header) and a form for adding new values to this menu. And I have an absolutely correct attribute 'update' of 'ajax' tag inside the form (I mean, that dropdown menu on page is updated after submitting the form). The point is that dropdown menu and form for additing values have different view-classes.

First one:

@Named
@ViewScoped
public class DropdownMenuView {

    @Inject
    private ValuesService service;

    private List<String> values;

    @PostConstruct
    public void init() {
       value = service.getValues();
    }

    // Getters Setters...
}

Second one:

@Named
@ViewScoped
public class NewValuesView {

    @Inject
    private ValuesService service;

    public void addValue(String newValue) {
        service.addValue(newValue);
    }
}

Okay, now I add new value via form, and open dropdown list. But the list is not updated really because 'values' list on DropdownMenuView is still the same.

My question is, how to update 'values' after submitting form? Is there a way without injecting DropdowneMenuView to NewValuesView and updating it manually?

The only way I see now is implement something like "events class", fire an event after value updating and check this class in getter.

Event holer dummy implementation:

public class EventHolder {
    private static boolean event;

    public static void fire() {
        event = true;
    }

    public static boolean poll() {
         boolean res = event;
         event = false;
         return res;
    }
}

And page 2:

public class NewValuesView {

    @Inject
    private ValuesService service;

    public void addValue(String newValue) {
        service.addValue(newValue);
        EventHolder.fire();
    }
}

And page 1:

public class DropdownListView {

    ...

    public List<String> getValues() {
        if (EventHolder.poll()) {
            values = service.getValues();
        }
        return values;
    }
}

But I know that put logic to getters/setters is a bad way.
So, any suggestions?
Thanks.

Upvotes: 0

Views: 166

Answers (1)

i.merkurev
i.merkurev

Reputation: 465

Perhaps, it is because the default scope of the @Inject annotation is the dependent pseudo-scope @Dependent

CDI features the so-called dependent pseudo-scope. This is the default scope for a bean which does not explicitly declare a scope type. [...] An instance of a dependent bean is never shared between different clients or different injection points. It is strictly a dependent object of some other object. It is instantiated when the object it belongs to is created, and destroyed when the object it belongs to is destroyed.

So, when you declare your service ValuesService try to use a normal scope (http://docs.jboss.org/cdi/spec/1.0/html/contexts.html#dependentcontext)

Upvotes: 0

Related Questions