elif erdil elif erdil - 7 months ago 43
Java Question

Parameters always return null in JSF

I am trying to make a login page. I keep the members in different .txt files. But "person", "fileName" and "pageName" variables always return null. I tried everything but can't get it work. Here is my managed bean.

package hw;

import java.io.IOException;

import javax.faces.bean.ManagedBean;
@ManagedBean(name="choose")

public class Choose{

private String person=new String();
private String email;
private String password;
public String pageName,fileName,fileName2;
private boolean ifExists;
FileDatabase fileDatabase = new FileDatabase();

//getters and setters
public String getPerson() {
return person;
}

public void setPerson(String person) {
this.person = person;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
//chooses which file to read to check if the member exists
//according to the selectMenu value and set the returning page name.
//doesnt enter any of the if blocks.
public String chooseFile() {
if(person.equals("1")){
fileName="admins.txt";
pageName="admin-page";
}
else if(person.equals("2")){
fileName="instructors.txt";
pageName="instructor-page";
}
else if(person.equals("3")){
fileName="assistants.txt";
pageName="assistant-page";
}
else if(person.equals("4")){
fileName="students.txt";
pageName="student-page";
System.out.println("Filename:"+fileName);
System.out.println("Pagename:"+pageName);
}
return fileName;
}
//gets the fileName and checks the member but fileName is always null,
//person and pageName as well.
public String login() throws IOException {
fileName2=chooseFile();
System.out.println("Person:"+person);
System.out.println("Filename2:"+fileName2);
System.out.println("Pagename:"+pageName);
ifExists = fileDatabase.ifExistsMember(fileName2, email, password);
if(ifExists)
return(pageName);
else
return("index");
}

public String register() throws IOException{
return("index");
}
}


Here is the body part of the index.xhtml:

<h:body>
<h1 class="title">Welcome!</h1>

<fieldset>
<h:form>
<h3>Which one are you?</h3>
<h:selectOneMenu value="#{choose.person}">
<f:selectItem itemValue="1" itemLabel="Admin" />
<f:selectItem itemValue="2" itemLabel="Instructor" />
<f:selectItem itemValue="3" itemLabel="Assistant" />
<f:selectItem itemValue="4" itemLabel="Student" />
</h:selectOneMenu>
</h:form>
</fieldset>
<fieldset>
<h:form>
Email<br/><h:inputText value="#{choose.email}"/><br/>
Password<br/><h:inputSecret value="#{choose.password}"/><br/><br/>
<h:commandButton value="Login" action="#{choose.login}" />
<h:commandButton value="Register" action="#{choose.register}" />
</h:form>
</fieldset>
</h:body>

Answer

Your problem is easy to solve.

Just move your <h:selectOneMenu> to the other form

    <h:body>
    <h1 class="title">Welcome!</h1>
    <h:form>
    <fieldset>
        <h3>Which one are you?</h3>
        <h:selectOneMenu value="#{choose.person}">
            <f:selectItem itemValue="1" itemLabel="Admin" />
            <f:selectItem itemValue="2" itemLabel="Instructor" />
            <f:selectItem itemValue="3" itemLabel="Assistant" />
            <f:selectItem itemValue="4" itemLabel="Student" />              
        </h:selectOneMenu>
     </fieldset>
     <fieldset>
        Email<br/><h:inputText value="#{choose.email}"/><br/>
        Password<br/><h:inputSecret value="#{choose.password}"/><br/><br/>
        <h:commandButton value="Login" action="#{choose.login}" />
        <h:commandButton value="Register" action="#{choose.register}" />
      </fieldset>
   </h:form>
  </h:body>
Comments