参数始终在JSF中返回null

时间:2016-04-26 15:20:53

标签: jsf-2

我正在尝试创建一个登录页面。我将成员保存在不同的.txt文件中。但“person”,“fileName”和“pageName”变量始终返回null。我尝试了一切,但无法让它发挥作用。这是我的托管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");
    }
}

这是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>

1 个答案:

答案 0 :(得分:1)

您的问题很容易解决。

只需将<h:selectOneMenu>移至其他表单

即可
    <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>