JSF将值传递到其他页面

时间:2019-05-10 12:58:38

标签: jsf

在将值传递到另一页时遇到问题。

我的索引页面,其中有学生姓名的输入文字

<h:form>
<h:inputText id="name" 
                        value="#{student.name}"
                        label="name"
                        required="true" 
                        requiredMessage="Name is required"/>
<h:message for="name" styleClass="error"/>            
<h:commandButton value="Add" action="#{school.add(name)}" />
</h:form>

受管豆学生:(必须在请求范围内查看)

@Named(value = "student")
@RequestScoped
public class Student {
    private String name;

    public Student() {
    }

    public Student(String name){
        this.name = name;
    }

    public String getName(){
        return this.name;
    }

    public void setName(String name){
        this.name = name;        
    }
}

豆类学校:

@Named(value = "school")
@SessionScoped
public class School implements Serializable{
    private ArrayList<Student> student;

    public School() {
        students = new ArrayList<>();
    }

    public ArrayList<Student> getStudents() {
        return students;
    }

    public void setStudents(ArrayList<Student> students) {
        this.students = students;
    }

    public void add(Student student) throws IOException{
        if(students.size() < 1){
            this.students.add(new Student(student.getName()));
            FacesContext.getCurrentInstance().getExternalContext().redirect("list.xhtml");
        } else {
            FacesContext.getCurrentInstance().getExternalContext().redirect("error.xhtml");
        }        
    }    
}

如果有很多学生,我需要在最后一页上获得学生的姓名,我尝试<h3>#{student.name}</h3>,但它什么都没有显示。

有人知道我如何使它工作吗?

0 个答案:

没有答案