Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
点击复选框,JSF数据表行切换为可编辑_Jsf_Jsf 2_Datatable_Render_Jsf 2.2 - Fatal编程技术网

点击复选框,JSF数据表行切换为可编辑

点击复选框,JSF数据表行切换为可编辑,jsf,jsf-2,datatable,render,jsf-2.2,Jsf,Jsf 2,Datatable,Render,Jsf 2.2,我有一个数据表,应该是书店的管理页面。我已经做了所有的工作,除了我应该在每一行上都有一个复选框,它可以将输出转换为输入,并允许您编辑表中的一本书 我已经通过控制台确认它正在运行该方法并将布尔值设置为true,但它不会重新呈现要编辑的行。任何帮助都将不胜感激。我对JSF还是很陌生 我的索引页 <h:body> <h:form> <h1>Administrator Page</h1>

我有一个数据表,应该是书店的管理页面。我已经做了所有的工作,除了我应该在每一行上都有一个复选框,它可以将输出转换为输入,并允许您编辑表中的一本书

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



   }

}

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

好的,各位,我已经解决了我的问题。每次调用bookDatabase中的GetBooks时,我都在清除书籍数组

public List<Book> getBooks() throws SQLException {
books.clear();
我将此更改为if检查,以查看数组是否为空,它现在是否工作


Tiny我也接受了你的建议,将我的逻辑从书籍的get方法中移除。谢谢你的帮助。我在JSF方面进展缓慢,但确实越来越好

只需注意一点:您将关键业务逻辑放在通过引用getBooks的访问器方法中。此方法自然会被调用多次。考虑移动它的代码,例如,用@ PuthCuto注释的方法。在这样的场景中,不需要会话范围的bean。如果您使用的是JSF2.2/JavaEE7,那么视图范围的bean就足够了。我避免了javax.faces.view.ViewScoped CDI可用的[JSF-2]标记。也许,您的目标是一些旧的EOL JSF 1.x教程/书籍。
public List<Book> getBooks() throws SQLException {
books.clear();