2016-05-28 146 views
1

我一直在嘗試使用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); 
} 
+0

你試圖解決什麼問題?你能形容它嗎? – ujulu

+0

用戶輸入名字和年齡,我想將名字和年齡存儲在類StudentBean的對象中,然後我可以將該對象添加到ArrayList Exit

+0

並且'#{student.showGo()}'應該在哪裏引導您或什麼應該顯示在'show.xhtml'中? – ujulu

回答

2

我不知道,我瞭解你。但是,如果你的目的只是爲了節省nameage一個對象,並將其添加到列表,下面應該服務宗旨:

  1. 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; 
        } 
    } 
    
  2. 定義只是一個簡單的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 
    } 
    

    如果您仍有問題,請留下評論。

+0

謝謝,它的作品!...最後一個問題:爲什麼需要一個學生課? – Exit

+0

Student類表示域對象;。這就是你編寫程序的原因。您可以將它傳遞給應用程序的不同層。但是,StudentBean由運行時環境管理;在這種情況下,由Web容器?你不能創建或銷燬它:它的主要功能是控制GUI的邏輯:導航,事件處理,...... – ujulu

0

你可以嘗試做這樣的事情:

<p:commandButton value="Add" action="#{list.addStudent()}"></p:commandButton> 

傳遞addStudent

public String addStudent() 
{ 
    StudentBean student = new StudentBean(); 
    studentList.add(student); 
    return "show"; 
}