【问题标题】:JSF datatable line switching to editable on checkbox clickJSF 数据表行在复选框单击时切换为可编辑
【发布时间】:2015-02-27 06:13:26
【问题描述】:

好的,我有一个数据表,它应该是书店的管理页面。我已经完成了所有工作,除了我应该在每一行上都有一个复选框,它将输出切换到输入并让您在表格中编辑一本书

我已通过控制台确认它正在运行该方法并将布尔值设置为 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();



   }

}

再次感谢您提供的任何帮助

【问题讨论】:

  • 仅附注:您将关键业务逻辑置于访问器方法getBooks()(通过&lt;h:dataTable value="#{bookDatabase.books}"...&gt; 引用)中。该方法本质上会被多次调用。考虑移动它的代码,例如,在一个用@PostConstruct 注释的方法中。在这种情况下,不应需要会话范围的 bean。视图范围的 bean 应该就足够了(您使用的是 JSF 2.2/Java EE 7(我避免使用 [jsf-2] 标记),其中 javax.faces.view.ViewScoped (CDI) 可用)。也许,您的目标是某处的旧 EOL JSF 1.x 教程/书籍。

标签: jsf jsf-2 datatable render jsf-2.2


【解决方案1】:

好的,伙计们,我已经解决了我的问题。每次我打电话在 bookDatabase 中获取书籍时,我都在清除书籍数组

public List<Book> getBooks() throws SQLException {
books.clear();

我将其更改为 if 检查以查看数组是否为空并且它现在可以工作。

我也接受了你的建议,并将我的逻辑从书籍的 get 方法中移出。感谢您的帮助。我在 JSF 上慢慢地变得越来越好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    相关资源
    最近更新 更多