1

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.

fridberg
  • 161
  • 4
  • 14
  • 1
    Is your app server generating a new jsession id on login? – John Ament Oct 31 '13 at 23:06
  • It is generating a session id when I use: ` String sessid = FacesContext.getCurrentInstance().getExternalContext().getSessionId(false);` and print it out. When I navigate further and I print this code out again it prints a different id – fridberg Nov 01 '13 at 11:29

1 Answers1

1

After much debugging I finally found my error. It didn't have anything to do with injecting of beans, but rather that an <a> tag with a logout method was invoked during rendering because I didn't use commandLink with action attribute. I guess the href attribute in the <a> tag wants to have its outcome string right away and will thus run the method I had in the tag. Now I know. Stupid mistake...

fridberg
  • 161
  • 4
  • 14