Im using JSF 2 and GlassFish Server 4 with CDI (importing javax.enterprise.context...). I am trying to Inject a SessionBean, A, into another SessionBean, B, however when I try to access B on the second http request, it does not exist anymore. Instead a new instance of B is created. Thus, there is no info in any of the beans. Here is some of my code:
Bean A: (This bean keeps all the login info)
@Named("login")
@SessionScoped
public class LoginBean implements Serializable {
private Farmer aFarmer;
private Admin anAdmin;
private User loginInfo;
private int tries;
private boolean loggedIn;
private int accessLvl;
/**
* Constructor for a LoginBean, setting default values
*/
public LoginBean() {
}
@PostConstruct
public void init() {
loginInfo = new User();
tries = 0;
loggedIn = false;
}
Bean B: (This bean keeps all the farmer info and is used for actions on the farmer)
@Named("farmer")
@SessionScoped
public class FarmerController implements Serializable{
private @Inject LoginBean login;
private Farmer theFarmer;
private ArrayList<Integer> dates;
private ArrayList<Integer> months;
private ArrayList<Integer> years;
private int date; //date place holder
private int month; //month place holder
private int year; //year place holder
private String response; //confirmation response
private String confPwd;
private String oldPwd;
private String email;
private String password;
/**
* Default constructor for FarmerController, sets standard calendar values
*/
public FarmerController() {
}
@PostConstruct
public void init() {
theFarmer = new Farmer();
//standard values for dates
dates = new ArrayList<>();
for (int i = 0; i < 31; i++) {
dates.add(i+1);
}
//standard values for months
months = new ArrayList<>();
for (int i = 0; i < 12; i++) {
months.add(i+1);
}
//standard values for year, current year - 100
Calendar cal = new GregorianCalendar();
int tempyear = cal.get(Calendar.YEAR);
years = new ArrayList<>();
for (int i = 0, j = tempyear; i < 100 && j > tempyear - 100; i++, j--) {
years.add(j);
}
//check when bean is constructed
System.out.println("FarmerController constructed");
}
index.xhtml: (this is where I log in, and this works fine)
<h:form class="form-horizontal form-group">
<fieldset>
<div class="input-group">
<h:inputText id="username" class="form-control" p:placeholder="Brukernavn/Email" value="#{farmer.email}"/>
</div>
<div class="input-group">
<h:inputSecret id="password" class="form-control" p:placeholder="Passord" value="#{farmer.password}"/>
</div>
<div class="input-group">
<h:commandButton id="signin" value="Logg inn" class="btn btn-success" action="#{farmer.login()}"/>
</div>
</fieldset>
</h:form>
After logging in, I land on a second .xhtml page where everything is displayed correctly. However, when I then navigate further, to another page which references this FarmerController, it creates a new bean which doesnt have any of the information from logging in. Why is this happening? What am I doing wrong?
Update: I have also tried merging the two beans (using only 1 for all actions), and I have the same problem where the SessionScoped bean gets destroyed after logging in.