首页 > 解决方案 > 将图像添加到产品详细信息页面

问题描述

我尝试将图像添加到产品详细信息 JSP 页面。下面是table的表结构:

CREATE TABLE PRODUCTS (
ID VARCHAR(25) PRIMARY KEY,
NAME VARCHAR(50),
DESCRIPTION VARCHAR(250),
UNIT_PRICE DECIMAL,
MANUFACTURER VARCHAR(50),
CATEGORY VARCHAR(50),
CONDITION VARCHAR(50),
UNITS_IN_STOCK BIGINT,
UNITS_IN_ORDER BIGINT,
DISCONTINUED BOOLEAN
);

这是我数据库中的产品:

INSERT INTO PRODUCTS VALUES ('P1234', 'iPhone 6s', 'Apple iPhone 6s smartphone with 4.00-inch 640x1136 display and 8-megapixel rear camera','500','Apple','Smartphone','New',450,0,false);

INSERT INTO PRODUCTS VALUES ('P1235', 'Dell Inspiron', 'Dell Inspiron 14-inch Laptop (Black) with 3rd Generation Intel Core processors',700,'Dell','Laptop','New',1000,0,false);

INSERT INTO PRODUCTS VALUES ('P1236', 'Nexus 7', 'Google Nexus 7 is the lightest 7 inch tablet With a quad-core Qualcomm Snapdragon™ S4 Pro processor', 300,'Google','Tablet','New',1000,0,false);

下面是 Product 控制器的内容:

// sayfa 103'te eklendi
@RequestMapping("/product")
public ModelAndView getProductById(@RequestParam("id") String productId, Model model) {

    ModelAndView netice = new ModelAndView("product");

    netice.addObject("product", productService.getProductById(productId));

    System.out.println("109");
    // model.addAttribute("product",productService.getProductById(productId));
    return netice;
}

@RequestMapping("/product2")
public String getProduct2ById(@RequestParam("id") String productId, Model model) {

    System.out.println("117");
    
    Product product = productService.getProductById(productId);
    
    model.addAttribute("product", product);

    return "product2";
    
}

这是与工作示例相关的product.jsp的内容:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet"
href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<title>Products</title>
</head>
<body>
    <section>
        <div class="jumbotron">
           <div class="container">
               <h1>Products</h1>
           </div>
       </div>
   </section>
   <section class="container">  
        <div class="row">
           <div class="col-md-5">
               <!-- The point that prints photo to the user  -->
               <img src="<c:url value="/img/${product.productId}.png"> </c:url>" alt="image" style = "width:100%"/>
               <h3>${product.name}</h3>
               <p>${product.description}</p>
               <p>
                   <strong>Item Code : </strong><span class="label label warning">${product.productId}
                   </span>
            </p>
            <p>
                <strong>manufacturer</strong> : ${product.manufacturer}
            </p>
            <p>
                <strong>category</strong> : ${product.category}
            </p>
            <p>
                <strong>Available units in stock </strong> : ${product.unitsInStock}
            </p>
            <h4>${product.unitPrice}USD</h4>
            <p>

                <a href="<spring:url value="/listproducts" />"
                    class="btn btn-default"> <span
                    class="glyphicon-hand-left glyphicon"></span> back
                    
                </a> <a href="#" class="btn btn-warning btn-large"> <span
                    class="glyphicon-shopping-cart glyphicon"> </span> Order Now
                </a>
            </p>
        </div>
    </div>
</section>
</body>
</html>

以下是与错误示例相关的product2.jsp的内容:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet"
href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<title>Products</title>
</head>
<body>
    <section>
        <div class="jumbotron">
            <div class="container">
                <h1>Products</h1>
            </div>
        </div>
    </section>
    <section class="container">
        <div class="row">
            <div class="col-md-5">  
                <!-- The point that prints photo to the user  -->           
                <img src="<c:url value="/img/${product.productId}.png"> </c:url>" alt="image" style = "width:100%"/>
                <h3>${product.name}</h3>
                <p>${product.description}</p>
                <p>
                    <strong>Item Code : </strong>
                    <span class="label label warning">${product.productId} </span>
                </p>
                <p>
                    <strong>manufacturer</strong> : ${product.manufacturer}
                </p>
                <p>
                    <strong>category</strong> : ${product.category}
                </p>
                <p>
                    <strong>Available units in stock </strong> :
                ${product.unitsInStock}
                </p>

                <p>
                    <a
                    href=" <spring:url value="/product2?id=${product.productId}" /> "
                    class="btn btn-primary"> <span
                    class="glyphicon-info-sign glyphicon" /> 
                </span> Details
                    </a>
                </p>

                <a href="<spring:url value="product" />" class="btn btn-default"> <span  class="glyphicon-hand-left glyphicon"></span> back
                </a>

            </div>
        </div>
    </section>
</body>
</html>

我在 product2.jsp 收到以下错误:

org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping for GET /springmvc197/%3Cc:url%20value=

我错了什么?提前致谢。

Update-1
与错误示例相关的 product2.jsp 缺少<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>指令。在下面的评论中,allkenang 先生直观地找到了解决方案。原始帖子未包含 jsp 文件中的指令。

只是一个想法,可能不是问题..您能否在您的 product2.jsp 页面上仔细检查您的标签库 (c:) 导入?因为错误状态为“/springmvc197/<c:url value=",所以无法理解c:

标签: javaspring-mvc

解决方案


在我看来,您的网址中有错字。它应该是:

<spring:url value="/product2?id=${product.productId}" /> 

代替

 <spring:url value="/product?id=${product.productId}" /> 

推荐阅读