1

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

我想从表中动态添加和删除一行并仅刷新该部分而不是整个页面

我的情况:

  1. 我在一个页面中有两个 h:forms,第一个显示来自通过视图传递的对象的数据
  2. 在第二个 h:form 中,我有一个p:dataTable和一个用于在表中添加数据的输入表单。添加按钮使用 f:ajax 事件在添加输入后呈现第二个 h:form。到目前为止一切正常......

问题是,每次我想在表中添加新条目时,旧条目都会被删除,或者我无法向表中添加条目。页面的负责 bean 是请求范围 bean,每次我为页面执行渲染 oder 更新时,都会创建一个新 bean...

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

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

提前致谢

4

1 回答 1

0

两个文件……

演示.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>

用户Bean.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);
            }
于 2016-04-22T04:57:12.467 回答