【发布时间】:2016-09-26 19:42:06
【问题描述】:
我一直在尝试使用 Java Server Faces 将 Student 对象添加到 arrayList,但我可以弄清楚如何做到这一点,这是迄今为止我得到的帽子。我需要使用什么方法,我应该把它放在哪里?
索引
<h:body>
<h:form >
<h:panelGrid columns="2" >
<h:outputText value="Name"/>
<p:inputText value="#{student.name}" required="true">
<h:outputText value="Age"/>
<p:inputText value="#{student.age}" required="true">
<p:commandButton value="Add" action="#{student.showGo()}"><!--go to another JSF page-->
<!-- ActionListener needed-->
</p:commandButton>
</h:panelGrid>
</h:form>
</h:body>
学生豆
@Named(value = "student")
@RequestScoped
public class StudentBean {
private String name;
private int age;
public StudentBean(String name, int age) {
this.name = name;
this.age = age;
}
public StudentBean() {
}
public String showGo(){
return "show";
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
列表 bean
@Named(value = "list")
@RequestScoped
public class List {
private ArrayList<StudentBean> studentList = new ArrayList<>();
public List() {
}
public void addStudent()
{
StudentBean student = new StudentBean();
studentList.add(student);
}
【问题讨论】:
-
您要解决的问题是什么?你能描述一下吗?
-
用户输入姓名和年龄,我想将姓名和年龄存储在 StudentBean 类的对象中,这样我就可以将该对象添加到 ArrayList
-
#{student.showGo()}应该引导你到哪里,或者show.xhtml中应该显示什么?