JSF 2.0使用请求bean动态添加和删除表行

时间:2012-09-27 15:57:53

标签: java jsf-2

我是JSF的新手(使用它作为我的硕士论文),我正在努力实现以下目标:

我想动态添加和删除表格中的一行,并且只刷新该部分而不是整个页面

我的情况:

  1. 我在页面中有两个h:表单,第一个显示来自通过视图的对象的数据
  2. 在第二个h:表单中,我有一个 p:dataTable 和一个用于在表格中添加数据的输入表单。添加按钮使用f:ajax事件在添加输入后呈现第二个h:表单。到目前为止一切都有效...
  3. 问题是,每当我想在表中添加新条目时,旧条目将被删除,或者我无法向表中添加条目。该页面的负责bean是一个请求范围bean,每当我执行页面的渲染或更新时,都会创建一个新的bean ...

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

    如果需要,我可以提供此stange行为的代码......

    提前致谢

1 个答案:

答案 0 :(得分: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);
            }
相关问题