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



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

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

我的案例:

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

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

如何添加一行(或删除一行)而不每次都创建一个新的bean?我只想保留添加的数据。

如果需要,我可以提供这种恶心行为的代码。。。

提前感谢

两个文件。。。。

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);
}

最新更新