gpt4 book ai didi

java - Spring模型属性/jsp不可见

转载 作者:行者123 更新时间:2023-12-02 07:06:53 25 4
gpt4 key购买 nike

学习spring+jsp。

我正在 Controller 的方法中设置模型属性

@RequestMapping("/viewExpenses")
public String viewAllExpenses(Model model){

List<Expense> expenses;
expenses = expenseService.getAllExpenses();
model.addAttribute(expenses);
for(Expense e : expenses)
System.out.println(e);
return "viewExpenses";
}

其中费用大小 > 1

Following is my jsp page:
<%@ taglib prefix="s" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<h2>
Hello ${user.firstName} <br> <a href="homepage">HOME</a>
</h2>
<div>All Expenses</div>
<span style=""></span>
<c:forEach var="expense" items="${expenses}">
<c:out value="${expense.expenseId}"></c:out>
<c:out value="${expense.expenseName}"></c:out>
<c:out value="${expense.expenseType}"></c:out>
<c:out value="${expense.expensePrice}"></c:out>
<c:out value="${expense.purchaseDate}"></c:out>
<c:out value="${expense.expenseComments}"></c:out>
</c:forEach>
</body>
</html>

以下是我的 jsp 输出(没有错误):你好普拉瓦特家所有费用

我想知道为什么我的jsp中没有填充费用

最佳答案

您需要为您的属性指定一个属性名称:

model.addAttribute("expenses", expenses);

背景:当未提供名称时,Spring 使用与变量名称不同的生成名称。 docs解释一下:

Determine the conventional variable name for the supplied Object based on its concrete type. The convention used is to return the uncapitalized short name of the Class, according to JavaBeans property naming rules: So, com.myapp.Product becomes product; com.myapp.MyProduct becomes myProduct; com.myapp.UKProduct becomes UKProduct.

关于java - Spring模型属性/jsp不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15992412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com