【问题标题】:How to get value of SelectBox when using AppEngine in Java, JavaScript and HTML在 Java、JavaScript 和 HTML 中使用 App Engine 时如何获取 Select Box 的值
【发布时间】:2015-01-08 12:41:28
【问题描述】:

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

  1. 获取 SelectBox 的值。

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

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

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

<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 表格中(如上面的代码所示)。

这会将参数发送到这个 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");

【问题讨论】:

    标签: java javascript html google-app-engine


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-11-13
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 2015-05-18
      相关资源
      最近更新 更多