2015-01-08 18 views
0

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

  1. 获取SelectBox的值。

  2. 将它传递给具有参数值的函数在文本字段中。 (几乎完成)

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

首先,这是怎么了创建与选择框的形式,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表形式(如上面的代码所示)。

这将参数发送这个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; 
} 

我怎样才能得到它,这样我可以得到选择框的值,执行此功能,这样做的结果设定为等于idvBook?

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

我会简化一些事情。我需要这个<%以内...%>标签: -

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

回答

0

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

+0

试过了,没有什么区别。 –