在 Java、JavaScript 和 HTML 中使用 App Engine 时如何获取选择框的值

How to get value of SelectBox when using AppEngine in Java, JavaScript and HTML

我正在尝试创建一种过滤存储在数据存储中的数据的方法。我为此使用的方法是 HTML、Javascript 和 Java Servlet 的混合。创建一个表单,允许将输入到 Web 应用程序的数据发送到 Java Servlet。然后 Servlet 执行所需的操作并将用户发送回具有更新数据的页面。我的问题是我必须在此表单中使用 SelectBox,它允许用户 select 他们希望过滤的属性,例如标题, 作者.按下提交按钮时,SelectBox 的值不会作为参数发送。这意味着出现错误 500,因为没有足够的参数来执行该功能。我需要:-

  1. 获取选择框的值。

  2. 将其传递给带有文本字段中参数值的函数。 (即将完成)

  3. 将 idvBook 的值设置为等于发送回 Webb 应用程序的列表。

首先,这是我使用 SelectBox、TextInput 和 ButonInput 创建表单的方式:-

<div class="headline">Filter the DataStore</div>
    <div class="info">To view individual records, select the attribute 
    you'd like to filter by and enter the value you'd like to find. Click 
    the button to generate the table based on your search criteria.</div>
    <script type="text/javascript">
         function filter(){
             document.filterBooks.action="/get";
             var idvBook = document.filterBooks.submit();
         }
    </script>
    <form name="filterBooks">
        <table>
            <tr>
                <td valign="top"><label for="attribute">Attribute</label></td>
                <td>
                    <select id="attributeSelect">
                        <option value="void">Select Attribute</option>
                        <option value="id">ID</option>
                        <option value="title">Title</option>
                        <option value="author">Author</option>
                        <option value="publisher">Publisher</option>
                    </select>

                </td>

            </tr>
            <tr>
                <td valign="top"><label for="value">Value</label></td>
                <td><input type="text" name="value" id="value"></input></td>
            </tr>
            <tr>
                <td colspan="2" align="right"><input type="submit" value="Filter" onclick="filter()"></td>
            </tr>
        </table>
    </form>
    <table>
        <tr>
            <th>Book ID</th>
            <th>Title</th>
            <th>Description</th>
            <th>Author</th>
            <th>Publisher</th>
            <th>Publish Date</th>
            <th>Remove Book</th>
        </tr>
        <%
            for (Book book : idvBook) {
        %>
        <tr>
            <td><%=book.getId()%></td>
            <td><%=book.getTitle()%></td>
            <td><%=book.getDescription()%></td>
            <td><%=book.getAuthor()%></td>
            <td><%=book.getPublisher()%></td>
            <td><%=book.getPublishDate()%></td>
            <td><a class="done" href="/done?id=<%=book.getId()%>">Remove</a></td>
        </tr>
        <%
            }
        %>
    </table>
</div>

完成后,将其放置在表单下方的 HTML table 中(如上面的代码所示)。

这会将参数发送到此 servlet:-

public class ServletGetBooks extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse resp) throws IOException 
    {
        resp.setContentType("text/plain");
        resp.getWriter().println("Hello, world");
        String attribute = request.getParameter("attributeSelect").toString();
        String param = request.getParameter("value");
        Dao.INSTANCE.getBooks(attribute, param);
        resp.sendRedirect("/TodoApplication.jsp");
    }
}

你可以在这里看到另一个函数是运行。这个函数看起来像这样:-

public List<Book> getBooks(String attribute, String param) {
    EntityManager em = EMFService.get().createEntityManager();
   Query q = null;

    if(attribute.equals("id"))
    {
        q = em.createQuery("select b from Book b where b.id = :id");
        q.setParameter("id", param);
    }
    else if(attribute.equals("title"))
    {
        q = em.createQuery("select b from Book b where b.title = :title");
        q.setParameter("title", param);
    }
    else if(attribute.equals("author"))
    {
        q = em.createQuery("select b from Book b where b.author = :author");
        q.setParameter("author", param);
    }
    else if(attribute.equals("publisher"))
    {
        q = em.createQuery("select b from Book b where b.publisher = :publisher");
        q.setParameter("publisher", param);
    }
    List<Book> books = q.getResultList();
    return books;
}

我怎样才能得到 SelectBox 的值,运行 函数并将其结果设置为等于 idvBook?

编辑---------------------------------------- ---------------------------------------------- ---

我会简化事情。我需要在 <% ... %> 标签内:-

List<Book> idvBook = new ArrayList<Book>();
idvBook = dao.getBook("Value in SelectBox","Value in TextInput");

您的 select 框没有名称属性,因此它的值不会与表单数据一起发送。