好的,我有一个数据表应该是书店的管理页面。我已经完成了所有工作,除了我应该在每一行上都有一个复选框,它会将输出切换到输入并让你在表格中编辑一本书
我已通过控制台确认它正在运行该方法并将布尔值设置为true,但它不会将该行重新呈现为可编辑。任何帮助将不胜感激。我还是JSF的新手
我的索引页
<h:body>
<h:form>
<h1>Administrator Page</h1>
<br></br>
<br></br>
<h:dataTable value="#{bookDatabase.books}"
rowClasses="evenRow,oddRow"
id="edit"
var="book">
<h:column>
<f:facet name="header">edit</f:facet>
<h:selectBooleanCheckbox value="#{book.editable}"
onclick="submit()"/>
</h:column>
<h:column>
<f:facet name="header">Title</f:facet>
<h:inputText value="#{book.title}"
rendered="#{book.editable}" size="10"/>
<h:outputText value="#{book.title}"
rendered="#{not book.editable}"/>
</h:column>
<h:column>
<f:facet name="header">Author</f:facet>
<h:inputText value="#{book.author}"
rendered="#{book.editable}" size="10"/>
<h:outputText value="#{book.author}"
rendered="#{not book.editable}"/>
</h:column>
<h:column>
<f:facet name="header">Price</f:facet>
<h:outputText id="unitPrice2" value="#{book.price}"
rendered="#{not book.editable}">
<f:convertNumber currencyCode="USD" type="currency" />
</h:outputText>
<h:inputText id="unitPrice" value="#{book.price}"
rendered="#{book.editable}" size="10">
<f:convertNumber currencyCode="USD" type="currency" />
</h:inputText>
</h:column>
<h:column>
<h:commandLink value="Delete"
action="#{bookDatabase.removeBook(book.title)}"/>
</h:column>
</h:dataTable>
<br></br>
Title: <h:inputText id="title" value="#{bookDatabase.title}"/> <br></br>
Author: <h:inputText id="author" value="#{bookDatabase.author}"/> <br></br>
Price: <h:inputText id="price" value="#{bookDatabase.price}"/> <br></br>
<h:commandButton id="submit" value="Submit" action="#{bookDatabase.addBook()}"/>
<br></br>
</h:form>
</h:body>
书类
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
public class Book implements Serializable {
String title;
String author;
double price;
String orderNumeber;
int quan;
double sub;
double total;
boolean editable;
public Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
Book() {
title = null;
author = null;
price = 0.0;
orderNumeber = "";
quan = 0;
sub = 0.0;
total = 0.0;
editable = false;
}
public String editableCheck(String title) {
System.out.print(title);
if (this.title.equals(title)) {
if (editable == true) {
editable = false;
} else {
editable = true;
}
}
System.out.print(editable);
return null;
}
public boolean isEditable() {
return editable;
}
public void setEditable(boolean editable) {
this.editable = editable;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public int getQuan() {
return quan;
}
public void setQuan(int quan) {
this.quan = quan;
}
public double getSub() {
return sub;
}
public void setSub(double sub) {
this.sub = sub;
}
public String getOrderNumeber() {
return orderNumeber;
}
public void setOrderNumeber(String orderNumeber) {
this.orderNumeber = orderNumeber;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
我不认为我的bookDatabase类对它有任何影响,但这里只是以防万一
@Named(value = "bookDatabase")
@SessionScoped
public class BookDatabase implements Serializable {
/**
* Creates a new instance of BookDatabase
*/
private List<Book> books;
@Resource(name = "jdbc/database2")
private DataSource ds;
private boolean display;
private boolean edit;
private boolean add;
private boolean remove;
private String title;
private String author;
private String price;
private String bookToRemove;
Book addBook;
@PostConstruct
public void init() {
books = new ArrayList<>();
display = false;
edit = false;
add = false;
remove = false;
addBook = new Book();
title = null;
author = null;
price = null;
bookToRemove= null;
}
public String getBookToRemove() {
return bookToRemove;
}
public void setBookToRemove(String bookToRemove) {
this.bookToRemove = bookToRemove;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public void removeBook() throws SQLException
{
if (ds == null) {
throw new SQLException("ds is null; Can't get data source");
}
Connection conn = ds.getConnection();
if (conn == null) {
throw new SQLException("conn is null; Can't get db connection");
}
try {
System.out.print(bookToRemove);
PreparedStatement ord = conn.prepareStatement("Delete from APP.BOOK where Title = ?");
ord.setString(1, bookToRemove);
ord.execute();
bookToRemove = null;
} finally {
conn.close();
}
}
public void removeBook(String toRemove) throws SQLException
{
bookToRemove = toRemove;
removeBook();
}
public void addBook() throws SQLException {
if (ds == null) {
throw new SQLException("ds is null; Can't get data source");
}
Connection conn = ds.getConnection();
if (conn == null) {
throw new SQLException("conn is null; Can't get db connection");
}
try {
if(author != null && title != null)
{
PreparedStatement ord = conn.prepareStatement("INSERT INTO APP.BOOK (TITLE, AUTHOR, PRICE) "
+ " VALUES (?, ?, ?)");
ord.setString(1, title);
ord.setString(2, author);
ord.setDouble(3, Double.parseDouble(price));
ord.execute();
author = null;
title=null;
price = null;
}
} finally {
conn.close();
}
}
public void renderDisplay() {
display = true;
edit = false;
add = false;
remove = false;
}
public void renderEdit() {
display = false;
edit = true;
add = false;
remove = false;
}
public void renderAdd() {
display = false;
edit = false;
add = true;
remove = false;
}
public void renderRemove() {
display = false;
edit = false;
add = false;
remove = true;
}
public boolean isDisplay() {
return display;
}
public void setDisplay(boolean display) {
this.display = display;
}
public boolean isEdit() {
return edit;
}
public void setEdit(boolean edit) {
this.edit = edit;
}
public boolean isAdd() {
return add;
}
public void setAdd(boolean add) {
this.add = add;
}
public boolean isRemove() {
return remove;
}
public void setRemove(boolean remove) {
this.remove = remove;
}
public List<Book> getBooks() throws SQLException {
books.clear();
if (ds == null) {
throw new SQLException("ds is null; Can't get data source");
}
Connection conn = ds.getConnection();
if (conn == null) {
throw new SQLException("conn is null; Can't get db connection");
}
try {
PreparedStatement ps = conn.prepareStatement(
"select TITLE, AUTHOR, PRICE from BOOK"
);
ResultSet result = ps.executeQuery();
while (result.next()) {
Book b = new Book();
b.setAuthor(result.getString("AUTHOR"));
b.setTitle(result.getString("TITLE"));
b.setPrice(result.getDouble("PRICE"));
books.add(b);
}
} finally {
conn.close();
}
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
public String getSpecificTitle(int number) {
Book currentBook = (Book) books.get(number);
return currentBook.getTitle();
}
public String getSpecificAuthor(int number) {
Book currentBook = (Book) books.get(number);
return currentBook.getAuthor();
}
public double getSpecificPrice(int number) {
Book currentBook = (Book) books.get(number);
return currentBook.getPrice();
}
}
再次非常感谢你能给我的任何帮助
答案 0 :(得分:0)
好的伙计我已经弄明白了我的问题。每次我在bookDatabase中调用book书时,我都在清理书籍。
public List<Book> getBooks() throws SQLException {
books.clear();
我将其更改为if,检查数组是否为空,现在可以正常工作。
Tiny我也接受了你的建议,并将我的逻辑从书籍的get方法中移除。谢谢您的帮助。我在JSF慢慢地变得越来越好。