【问题标题】:Display results on the same JSP issue显示同一 JSP 问题的结果
【发布时间】:2015-09-20 13:46:36
【问题描述】:

我创建了一个小型 Java Web 应用程序。我使用了 DAO 和一个 JSP 文件。我的问题是,当我在输入字段中输入 isbn 编号时,如果在数据库中找到这本书,它会显示“找到图书”和相应的详细信息。但是,如果我在输入字段中输入一个 isbn 编号,并且如果在数据库中找不到这本书,它仍然会显示“Book Found”,但 isbn 编号为 0,书名“null”,这是我不想要的。在这种情况下,它应该只显示“找不到书”。

注意问题主要出在JSP页面上。

这是我的完整代码:http://pastebin.com/cTZy4w6V(由于代码太长,使用 pastebin)

JSP 代码如下:

<jsp:useBean id = "bm" class="book.ManagerBook" scope = "session"/>    

    <h1> Welcome to ABC Library</h1>

    <form>
            <table>
                    <tr>
                            <td> Enter Details </td>
                            <td><input type="text" name="isbn"></td>
                            <td><input type="submit" name="find" value="find"></td>
                    </tr>
            </table>
                    <input type="hidden" name="submitted" value="true">
            </form>


            <%     
                    Boolean submitted = Boolean.parseBoolean(request.getParameter("submitted"));
                    if(submitted){
                    int isbn = Integer.parseInt(request.getParameter("isbn"));     

                    Book b2 = bm.findBook(isbn);           

                    %>
                    <table>
                    <tr>
                    <td colspan=2>
                    <h2>Book Found</h2>
                    </td>
                    </tr>

                    <tr>
                            <td><h3>ISBN</h3></td>
                            <td><h3>Title</h3></td>
                    </tr>  

                    <tr>
                            <td><%= b2.getIsbn()%></td>
                            <td><%= b2.getTitle() %></td>
                    </tr>
                    </table>

                    <%}else if(!submitted){ %>
                    <h3> Book Not Found</h3>
                    <% } %>

【问题讨论】:

    标签: java jsp


    【解决方案1】:

    如果可能,不要在 jsp 中使用 scriptlet。这是一种不好的做法。

    试试这样的:

                        if(submitted){
                        int isbn = Integer.parseInt(request.getParameter("isbn"));     
    
                        Book b2 = bm.findBook(isbn);           
                        if (b2.getTitle() != null) {
                        %>
                        <table>
                        <tr>
                        <td colspan=2>
                        <h2>Book Found</h2>
                        </td>
                        </tr>
    
                        <tr>
                                <td><h3>ISBN</h3></td>
                                <td><h3>Title</h3></td>
                        </tr>  
    
                        <tr>
                                <td><%= b2.getIsbn()%></td>
                                <td><%= b2.getTitle() %></td>
                        </tr>
                        </table>
    
                        <% }  else { %>
                        <h3> Book Not Found</h3>
                        <% } %> }
    

    【讨论】:

    猜你喜欢
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多