因此,我有一个JSF页面,当我更改输入字段文本时,该页面应自动更改表。
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:inputText value = "#{listBook.title}">
<f:ajax event="keyup" render="myTable" execute="@this" />
</h:inputText>
<table id="myTable">
<ui:repeat var="b" value="#{listBook.foundBooks}" varStatus="status">
<tr>
<td>#{b.id}</td>
<td>#{b.title}</td>
<td>#{b.author}</td>
<td>#{b.price}</td>
<td>#{b}</td>
</tr>
</ui:repeat>
</table>
<h:commandButton value="Back" action="index" ></h:commandButton>
<div id="message">
== #{listBook.message} ==
</div>
</h:form>
对应的类:
@ManagedBean(name = "listBook")
@RequestScoped
public class ListBook {
String title;
String message;
ArrayList<Book> foundBooks;
public ListBook() {
}
public String getBooks() throws SQLException {
String retUrl = "ListBooks";
try {
Database db = Database.getInstance();
foundBooks = db.getBookByTitle(title);
message = foundBooks.isEmpty() ? "no books found" : "some books found";
} catch (Exception ex) {
message = "error happened: " + ex.getMessage();
ex.printStackTrace();
}
return retUrl;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
System.out.println("yeah i am here");
try {
Database db = Database.getInstance();
foundBooks = db.getBookByTitle(title);
message = foundBooks.isEmpty() ? "no books found" : "some books found";
} catch (Exception ex) {
message = "error happened: " + ex.getMessage();
ex.printStackTrace();
}
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public ArrayList<Book> getFoundBooks() {
return foundBooks;
}
public void setFoundBooks(ArrayList<Book> foundBooks) {
this.foundBooks = foundBooks;
}
}
所以我遇到的问题是,当我更改文本字段的文本时,表格不会刷新。尽管调用了setTitle函数,并且arraylist更改了,但是表和消息没有更改。