我一直在嘗試使用Java Server Faces將一個Student對象添加到arrayList,但我可以知道如何做到這一點,這裏是我到目前爲止獲得的帽子。我需要使用什麼方法,以及我在哪裏放置它?JSF將對象添加到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;
}
}
列表豆
@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);
}
你試圖解決什麼問題?你能形容它嗎? – ujulu
用戶輸入名字和年齡,我想將名字和年齡存儲在類StudentBean的對象中,然後我可以將該對象添加到ArrayList –
Exit
並且'#{student.showGo()}'應該在哪裏引導您或什麼應該顯示在'show.xhtml'中? – ujulu