Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsp 在一页spring mvc中显示数据_Jsp_Spring Mvc - Fatal编程技术网

Jsp 在一页spring mvc中显示数据

Jsp 在一页spring mvc中显示数据,jsp,spring-mvc,Jsp,Spring Mvc,我不明白问题出在哪里,无法显示数据。他有头衔(姓名、价格和数量),但没有数据 这是控制器 @Controller public class ProductListController { @Autowired ProductRepository productRepository; @RequestMapping("/productlist") public ModelAndView showProductList() {

我不明白问题出在哪里,无法显示数据。他有头衔(姓名、价格和数量),但没有数据

这是控制器

@Controller
public class ProductListController {

        @Autowired ProductRepository productRepository;

        @RequestMapping("/productlist")
        public ModelAndView showProductList() {
            ModelAndView mv = new ModelAndView("productlist");  
            List<Product> list = productRepository.findAll();
            mv.addObject("displayProduct",list);
            return mv;
        }

    }
@控制器
公共类ProductListController{
@自动连线产品储存库产品储存库;
@请求映射(“/productlist”)
公共模型和视图showProductList(){
ModelAndView mv=新的ModelAndView(“产品列表”);
List=productRepository.findAll();
mv.addObject(“显示产品”,列表);
返回mv;
}
}
这是productlist.jsp

<!DOCTYPE html >
<%@  page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<%@  taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c' %>
<%@  taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>

<html>
<head>
   <title>Product</title>
</head>
<body>
<form:form method="POST" modelAttribute="displayProduct">
<table border="1" >
 <tr>
   <td>Name</td>
   <td>Price</td>
   <td>Quantity</td>
 </tr>
<c:forEach items="${displayProduct}" var="p">
   <c:out value="${p.name}"/>
</c:forEach>

</table>

</body>
</html>

产品
名称
价格
量

确保呈现正确的标记

<c:forEach items="${displayProduct}" var="p">
    <tr>
        <td><c:out value="${p.name}"/></td>
        <td><c:out value="${p.price}"/></td>
        <td><c:out value="${p.quantity}"/></td>
    </tr>
</c:forEach>


另外,确保
productRepository.findAll()
实际返回一些结果。如果它是一个空的结果集,您将在JSP上看不到任何内容。

我使用控制台的方式测试了productRepository.findAll()方法,并按照您的建议更改了代码。我总是有同样的问题

<!DOCTYPE html >
<%@  page language="java" contentType="text/html; charset=ISO-8859-1"  pageEncoding="ISO-8859-1"%>
<%@  taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c' %>
<%@  taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>

<html>
<head>
   <title>Product</title>
</head>
<body>
    <form:form method="POST" modelAttribute="displayProduct">
    <h1>Product</h1>

    <table border="1" >

        <tr>
            <td>Name</td>
            <td>Price</td>
            <td>Quantity</td>
            <td colspan="2">Actions</td>

        </tr>

    <c:forEach items="${displayProduct}" var="p">
       <tr>
        <td><c:out value="${p.name}"/></td>
        <td><c:out value="${p.price}"/></td>
        <td><c:out value="${p.quantity}"/></td>
      </tr>
    </c:forEach>


    </table>

    <a href="<c:url value='/'/>">home page</a><br/>
    </form:form>


</body>
</html>

产品
产品
名称
价格
量
行动


看到您的产品对象会很好。 其中有三个getter getName()、getPrice()和getQuantity()吗


您的产品pojo必须与JSP表达式语言使用的JavaBeans命名约定相匹配:${p.name}、${p.price}和${p.quantity}

显示您的
产品
类。此外,
表单
未关闭。