1

I'm new to JSF (using it for my master thesis) and I'm trying to achive the following:

I want to dynamically add and delete a row from a table and to refresh only that part and not the entire page

My case:

  1. I have two h:forms in a page, the first one displays data from an object passed through views
  2. In the second h:form I have a p:dataTable and a input form to add data in the table. The add button is using an f:ajax event to render the second h:form after adding the input. Everything till now works...

The problem is, that everytime I want to add a new entry in the table the old ones are deleted, or I cannot add an entry to the table. The responsible bean for the page is a request scope bean and everytime I execute a render oder update for the page, a new bean is created...

How can I add a row (or delete one) without creating every time a new bean? I just want to keep the added data.

If needed I can provide the code for this stange behavior...

Thanks in advance

Ioana
  • 59
  • 4

1 Answers1

0

Two files....

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);
            }
Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
Logesam
  • 1
  • 2