将参数传递给命名查询

时间:2014-12-14 11:03:09

标签: html netbeans parameter-passing jsp-tags named-query

我的模型(供应商)类中有以下查询:

@NamedQuery(name = "Supplier.findSupplierKeyId", query = "SELECT s FROM Supplier s WHERE s.supplierid LIKE (':supplieridkey%')")

我在Controller(SupplierSerivce)类中有以下功能:

public List<Supplier> findSupplierKeyId(String supplierkeyid){
    List<Supplier> supplierList = mgr.createNamedQuery("Supplier.findSupplierKeyId").setParameter("supplieridkey", supplierkeyid).getResultList();
    return supplierList;
}

我想进入html文本字段:

<form action="SearchSupplierIdKey.jsp" method="POST">
                        <div>
                            <input type="text" name="supIdKey"/>
                            <input type="submit" value="Search" name="button"/>
                        </div>
</form>

然后从textfield获取参数并通过servlet将其传递给supService.findSupplierKeyId: public class SearchSupplierIdKey扩展了HttpServlet {

@PersistenceContext
EntityManager em;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        SupplierService supService = new SupplierService(em);
        HttpSession session = request.getSession();
        String supId = (String) session.getAttribute("supId");
        String button = (String) session.getAttribute("button");

        List<Supplier> supplierListResult = supService.findSupplierKeyId(supId);
        session.setAttribute("supplierListResult", supplierListResult);
        if (button.equals("Search")) {
            response.sendRedirect("ViewSupplierByIdKey.jsp");
        }


    } catch (Exception ex) {
        Logger.getLogger(AddSupplier.class.getName()).log(Level.SEVERE, null, ex);
    }
}

然后在ViewSupplierByIdKey.jsp中显示结果:

    <%@page import="java.util.List"%>
<%@page import="model.Supplier"%>

<!-- retrieve session object, itemList -->
<%
  List<Supplier> supplierListResult = (List)session.getAttribute("supplierListResult");
%>

<html>
    <head>
        <title>Supplier Search Result</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <table cellspacing="0" cellpadding="0" style="margin-left:auto; margin-right:auto; border:solid; width: 1000px">
        <tr style="border:solid">
            <td style="border:solid ">
                <h3 style="text-align: center">ABC Health Supplement Shop System</h3>
            </td>
        </tr>
        <tr style="border: solid">
            <td style="border: solid">
                <center><h1><i><b><font face="Segoe Script" color="#FF0000">Supplier Search Result(By ID Key)</font></b></i></h1></center>
            </td>
        </tr>
        <tr style="border:solid">
            <td style="border:solid">
                <center><div>
                    <table border="1">
                        <tr>
                            <th>Supplier ID</th>
                            <th>Supplier Name</th>
                            <th>Manufacturer</th>
                            <th>Contact Num</th>
                            <th>Address</th>
                        </tr>
                        <% for (Supplier supplier: supplierListResult){ %>
                            <tr>
                                <td><%= supplier.getSupplierid() %></td>
                                <td><%= supplier.getSuppliername()%> </td>
                                <td><%= supplier.getManufacturer()%> </td>
                                <td><%= supplier.getContactnum()%> </td>
                                <td><%= supplier.getAddress()%> </td>
                            </tr>
                        <% } %>
                    </table>
                    <br><br>
                    <p><a href="index.html">Back to Menu page</a></p>

                </div>
                </center>
            </td>

        </tr>
        </table>

    </body>
</html>

但是我不知道为什么我不能继续使用ViewSupplierByIdKey.jsp,它停留在控制器类 (SearchSupplierIdKey.java)。请帮助:( :(

1 个答案:

答案 0 :(得分:0)

我注意到的一件事是使用名称supId检索请求参数:

String supId = (String) session.getAttribute("supId");

但在HTML中指定为supIdKey

 <input type="text" name="supIdKey"/>

name上使用的input属性应与用于检索属性的密钥匹配。