Problem using JSF Applicationscope

Asked

Viewed 161 times

4

I’m having a problem using the ApplicationScoped in JSF to save my country list.

I made this managedBean:

package view.point;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import view.country.CountryHelper;
import model.country.Country;

@ManagedBean(eager = true)
@ApplicationScoped
public class ApplicationScope {

    private Country country = CountryHelper.findAll().get(1);

    public ApplicationScope() {
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

}

Well, so far so good, debugged and the value is coming straight. The real problem is to get this value.

I tried to get through the Externalcontext and whenever I execute that code:

FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get("country");

I have as value NULL. Does anyone know why? I’m doing something wrong?

Well, thank you for your attention!

1 answer

1


Well, I managed to solve it. I will post the final code:

Bean da Applicationscope:

package view.point;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import view.country.CountryHelper;
import model.country.Country;


@ManagedBean(eager = true, name = "pauloMB")
@ApplicationScoped

public class ApplicationScope {

private Country country = CountryHelper.findAll().get(1);

public ApplicationScope() {
}

public Country getCountry() {
    return country;
}

public void setCountry(Country country) {
    this.country = country;
}
}

And in my other bean, where I want to access the value, I created an object of my application managedbean and injected it:

@ManagedProperty("#{pauloMB}")
private ApplicationScope applicationScope;

OBS: I put an alias called pauloMB just for testing. Using the same class name I could not do, for some bizarre reason it error.

And here I take the amount I want:

@PostConstruct
public void initCountry() {
    System.out.println(applicationScope.getCountry());
}

Thank you all, hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.