Problem using JSF ApplicationScope

4

I'm having a problem using ApplicationScoped on JSF to save my list of countries.

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 good, I debugged and the value is coming right. The real problem is to get this value.

I tried to get through ExternalContext and every time I run this code:

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

I have NULL value. Does anyone know why? Am I doing something wrong?

Well, thanks for the attention!

    
asked by anonymous 28.04.2015 / 20:55

1 answer

1

Well, I ended up solving it. I'll post the final code:

ApplicationScope bean:

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 from 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 get the value I want:

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

Thanks to all, hugs.

    
28.04.2015 / 21:59