2012-09-27 24 views
1

我是新来的JSF(使用它为我的硕士论文),我想才达到以下删除表行:JSF 2.0动态地添加和使用请求Bean

我要动态地添加和从表中删除一行,并刷新只有一部分,而不是整个页面

我的情况:

  1. 我有两个H:形式在一个页面,第一个显示的数据,从穿过物体视图
  2. 在第二H:形成我有一个号码:dataTable中和输入形式在表中添加数据。将输入后的形式:ADD按钮时,使用F:AJAX事件来呈现第二小时。一切到现在的作品...

问题是,每当我想在表中添加一个新的条目旧的被删除,或者我不能添加一个条目到表中。页面的责任bean是一个请求作用域bean,每次执行页面的渲染或更新时,都会创建一个新的bean ...

如何在不创建每个时间的情况下添加一行(或删除一行)一个新的豆子?我只想保留添加的数据。

如果需要,我可以提供这个STANGE行为的代码...

在此先感谢

回答

0

两个文件....

demo.xhtml:

<h:head> 

    <title> Student database </title> 
</h:head> 
<h:body> 
    <h:form> 
     <h2 style="color: darkmagenta"> The Student Database <hr color="darkmagenta"/></h2> 

     <h:dataTable value="#{stu.bookList}" var="o" style="color: black"> 

      <h:column> 
       <f:facet name="header"> Student Id </f:facet>#{o.id} 
      </h:column> 
      <h:column> 
       <f:facet name="header"> Student Name </f:facet>#{o.name} 
      </h:column> 
      <h:column> 
       <f:facet name="header"> Phone No </f:facet>#{o.phoneno} 
      </h:column> 
      <h:column> 
       <f:facet name="header"> Address </f:facet>#{o.address} 
      </h:column> 
      <h:column> 
       <f:facet name="header"> Action</f:facet> 
       <h:commandLink value="Delete" action="#{stu.deleteAction(o)}" />/
       <h:commandLink value="Edit" action="#{stu.editAction(o)}" /> 

        </h:column> 
       </h:dataTable> 

     <br/> <br/> 
     <h2 style="color: darkmagenta" >To Add/Update Enter The Details <hr color="darkmagenta"/></h2> 
      <table style="color: black"> 

     <tr> 
      <td>Student Id :</td> 
      <td><h:inputText size="20" value="#{stu.book.id}" /></td> 
     </tr> 
     <tr> 
      <td>Student Name :</td> 
      <td><h:inputText size="20" value="#{stu.book.name}" /></td> 
     </tr> 
     <tr> 
      <td>Phone No:</td> 
      <td><h:inputText size="20" value="#{stu.book.phoneno}" /></td> 
     </tr> 
     <tr> 
      <td>Address :</td> 
      <td><h:inputText size="20" value="#{stu.book.address}" /></td> 
     </tr> 
     </table> 
      <h:commandButton value="Add" action="#{stu.addAction}" disabled="#{stu.enable}" /> <h:commandButton value="Update" action="#{stu.updateAction}" disabled="#{stu.disable}" /> 




    </h:form> 
</h:body> 

UserBean.java:

@ManagedBean(name = "stu") 
@SessionScoped 
public class UserBean implements Serializable { 

    private static final long serialVersionUID = 1L; 


    private Book book = new Book(); 
    private boolean disable=true; 
    private boolean enable=false; 
    private ArrayList<Book> bookList = new ArrayList<Book>(); 


    public UserBean() { 
     book = new Book(); 

    } 

    public boolean isEnable() { 
     return enable; 
    } 

    public void setEnable(boolean enable) { 
     this.enable = enable; 
    } 

    public boolean isDisable() { 
     return disable; 
    } 

    public void setDisable(boolean disable) { 
     this.disable = disable; 
    } 

    public Book getBook() { 
     return book; 
    } 

    public void setBook(Book book) { 
     this.book = book; 
    } 






    public ArrayList<Book> getBookList() { 
     return bookList; 
    } 

    public void setBookList(ArrayList<Book> bookList) { 
     this.bookList = bookList; 
    } 




    public void addAction() { 


     System.out.println("Going to Add Data"); 

      bookList.add(book); 
     book = new Book(); 
     System.out.println("The Data is Added Succesfully"); 

    } 

    public String deleteAction(Book book) { 
     System.out.println("Going to Delete Data"); 
     bookList.remove(book); 
     System.out.println("The Data is Deleted Succesfully"); 
     return null; 
    } 

    public void editAction(Book book1) { 
     setDisable(false); 
     setEnable(true); 
     book = new Book(); 
     book.setId(book1.getId()); 
     book.setName(book1.getName()); 
     book.setPhoneno(book1.getPhoneno()); 
     book.setAddress(book1.getAddress()); 


    } 
public void updateAction(){ 
System.out.println("Going to update Data"); 
for (Iterator<Book> it = bookList.iterator(); it.hasNext();) { 
      Book result = it.next(); 
      System.out.println(result.id); 
      if (result.id == book.getId()) { 
       System.out.println(result.id); 
       System.out.println("address" + result.address); 
       System.out.println("naame" + result.name); 
       System.out.println(result.phoneno); 

       result.setName(book.getName()); 
       result.setPhoneno(book.getPhoneno()); 
       result.setAddress(book.getAddress()); 
       book = new Book(); 
       setDisable(true); 
       setEnable(false); 
      } 
      else{ 
       System.out.println("No Data To Update"); 
       setDisable(true); 
      } 
+0

钍为你的答案。请另外添加一些解释。 – Sampada

+0

现在参照上面的代码我粘贴了UserBean.java也.... – Logesam

+0

@Logesam我把Java源代码,并分开XML,请不要张贴在一个巨大的源XML和Java ... :) – Gewure