单击链接时如何传输数据?

时间:2018-10-17 14:19:00

标签: java jsp

我与servlets合作,将产品名称添加到购物车时需要获取它!

我以这种方式添加产品,然后单击“添加到购物车”时尝试转移名称。

<c:forEach items="${sessionScope.allProducts}" var="products">
                     <div class="col-md-4 fashion-grid">
                         <a href="single.jsp"><img src="images/product/${products.imageName}" width="250" height="350" alt=""/>
                             <div class="product">
                                 <h3>PRODUCT NAME:</h3>
                                 <h3>${products.name}</h3>
                                 <p>${products.size}</p>
                                 <p>${products.color}</p>
                                 <p>${products.category.name}</p>
                                 <p>${products.manufacturer.name}</p><br></br>
                                 <p><span></span>${products.price}</p>
                             </div>
                         </a>
                         <div class="fashion-view"><span></span>
                                    <div class="clearfix"></div>
                                 <h4><a href="addToCart?name=${products.name}">Add to cart</a></h4>
                         </div>

但是在我的servlet中,我得到了这个例外

java.sql.SQLException: No value specified for parameter 1
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1042)
    at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    at dao.impl.ProductDAOImpl.getProductByName(ProductDAOImpl.java:84)
    at service.impl.ProductServiceImpl.lambda$getProductByName$2(ProductServiceImpl.java:34)
    at db.TransactionManager.doInTransaction(TransactionManager.java:26)
    at service.impl.ProductServiceImpl.getProductByName(ProductServiceImpl.java:34)
    at servlet.AddToCartServlet.doGet(AddToCartServlet.java:32)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

我添加到购物车servlet:

@Override
    public void init() throws ServletException {
        cartService = (CartService) getServletContext().getAttribute(CONTEXT_CART_SERVICE);
        productService = (ProductService) getServletContext().getAttribute(CONTEXT_PRODUCT_SERVICE);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        cartService.add(productService.getProductByName(req.getParameter("name")));

        req.getSession().setAttribute("totalPrice", cartService.getCartPrice());
        req.getSession().setAttribute("cartItems", cartService.getProductForOrder());

        LOG.debug("Total price: " + req.getSession().getAttribute("totalPrice"));
    }

我的ProductServlet:

@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ProductFormExecute formExecute = new ProductFormExecute();
        ProductsForm form = formExecute.executeFormRequest(req);
        String sqlProductQuery = filteredProduct(form);

        List<Category> allCategory = categoryService.getAllCategory();
        req.getSession().setAttribute(ALL_CATEGORIES, allCategory);

        List<CompanyManufacturer> allCompany = manufacturerService.getAllCompanyManufacturer();
        req.getSession().setAttribute(ALL_COMPANIES, allCompany);

        List<Product> allProduct = productService.getProductByParameter(sqlProductQuery);
        req.getSession().setAttribute(ALL_PRODUCTS, allProduct);

        if (allProduct.isEmpty()) {
            req.getSession().removeAttribute(ALL_PRODUCTS);
            req.setAttribute(ERROR_PRODUCTS, ERROR_PRODUCTS_MESSAGE);
        }

        int countProducts = productService.getAllProduct(productCount(sqlProductQuery)).size();
        int productMax = form.getProductMax();

        req.getSession().setAttribute(COUNT_PRODUCTS, countProducts);
        req.getSession().setAttribute(PRODUCT_ON_PAGE_LIMIT, productMax);
        req.getSession().setAttribute(SORTING, form.getSort());
        req.getSession().setAttribute(FIND_BY_PRICE_MIN, form.getMinPrice());
        req.getSession().setAttribute(FIND_BY_PRICE_MAX, form.getMaxPrice());


        LOG.debug("Product max: " + productMax + " " + countProducts);
        LOG.debug(productService.getAllProduct(productCount(sqlProductQuery)));

        req.getRequestDispatcher(PRODUCT_PAGE).forward(req, resp);
    }

我的SQL查询:

> SELECT *, COUNT(*) AS 'count' FROM product INNER JOIN product_category
> on product.product_category_id = product_category.id INNER JOIN
> company_manufacturer_product on
> product.company_manufacturer_product_id=company_manufacturer_product.id
> WHERE pName IN('?') GROUP BY pName ORDER BY pName ;

当我单击链接时,为什么会出现此错误以及如何正确删除数据?预先谢谢你

1 个答案:

答案 0 :(得分:3)

require_once

应该是

<a href="addToCart" name="${products.name}">

当您获得请求参数“名称”时。

但是,还有其他技术,请在网上查找示例。

(为清楚起见,项目var名称应为<a href="addToCart?name=${products.name}"> ,而不是product。)