[英]JSF adding object to arrayList
I have been trying to add an Student object to an arrayList using Java Server Faces, but I can figure it out how to do it, here is hat i got so far. 我一直在尝试使用Java Server Faces将Student对象添加到arrayList中,但是我可以弄清楚如何做到这一点,这是我到目前为止的帽子。 what method I need to use, and where do i put it?
我需要使用什么方法,我应该放在哪里?
Index 指数
<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>
Student bean 学生豆
@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;
}
}
List 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);
}
I am not sure that I understood you. 我不确定我是否了解您。 But if your intention is just to save
name
and age
in an object and add it to a list, the following should serve the purpose: 但是,如果您只是想在一个对象中保存
name
和age
并将其添加到列表中,则应满足以下目的:
The managed bean in SessionScope
so that you can access it in the next page (`show.xhtml??): SessionScope
的托管bean,以便您可以在下一页(`show.xhtml ??)中进行访问:
@Named(value = "student") @SessionScoped public class StudentBean { private ArrayList<Student> studentList = new ArrayList<>(); private String name; private int age; public StudentBean(String name, int age) { this.name = name; this.age = age; } public StudentBean() { } public String showGo(){ Stundent student = new Student(name, age); studentList.add(student); 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; } public List<Student> getStundentList() { return studentList; } }
Define just a simple Sundent
class as JavaBean: 将一个简单的
Sundent
类定义为JavaBean:
public class Stundent { private String name; private int age; public Stundent() { } public Stundent(String name, int age) { this.name = name; this.age = age; } // getters and setters }
If you still have a question leave me a comment. 如果您还有问题,请给我留言。
You could try to do something like this: 您可以尝试执行以下操作:
<p:commandButton value="Add" action="#{list.addStudent()}"></p:commandButton>
and in addStudent : 并在addStudent中 :
public String addStudent()
{
StudentBean student = new StudentBean();
studentList.add(student);
return "show";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.