【问题标题】:pass a parameter into named query将参数传递给命名查询
【发布时间】:2014-12-14 11:03:08
【问题描述】:

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

@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>

然后从文本字段中获取参数并通过servlet将其传递给supService.findSupplierKeyId: 公共类 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)。请帮助:( :(

【问题讨论】:

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


    【解决方案1】:

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

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

    但在 HTML 中指定为 supIdKey

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

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

    【讨论】:

      猜你喜欢
      • 2013-04-08
      • 2017-09-27
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-26
      • 2015-03-03
      相关资源
      最近更新 更多