【问题标题】:JSP does not render java list in Spring Boot 2JSP 在 Spring Boot 2 中不呈现 java 列表
【发布时间】:2020-01-24 10:15:54
【问题描述】:

我在带有 JSP 的 Spring Boot 2 中有一个简单的 Java 应用程序。 我使用 h2 数据库来测试数据。 它工作正常,但 JSP 不呈现来自控制器的列表数据。 JSP 中的列表是空的,而在控制器中它有 2 个值。 String 等其他属性工作正常。 我不明白问题出在哪里。

控制器:

@Controller
public class HomeController {

@Autowired
UserService userService;

@GetMapping("/users")
public String getUsers(ModelMap map) {
    this.userService.getAll().forEach(user -> System.out.println("user: " + user));
    map.addAttribute("users", this.userService.getAll());
    return "/users";
}
}

用户模型:

@NoArgsConstructor
@Getter
@Setter
@EqualsAndHashCode
@ToString
@Entity
public class User implements Serializable {

    @Id
    @GeneratedValue
    private Long id;
    private String firstName;
    private String lastName;
    private String country;

    public User(@JsonProperty Long id, @JsonProperty String firstName, @JsonProperty String lastName, @JsonProperty String country) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.country = country;
    }
}

JSP 页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <jsp:include page="header.jsp">
        <jsp:param name="title" value="LIBRARY - Users"/>
    </jsp:include>
        <!--NAVBAR-->
        <%@ include file="navbar.jsp"%>
        <!--CONTENT-->
        <div class="container-fluid h-100">        
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Index</th>
                        <th>Id</th>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Country</th>
                    </tr>
                </thead>
                <tbody>
                    <c:forEach items="${users}" var="user" varStatus="iteration">
                        <tr>
                            <td>${iteration.index}</td>
                            <td>${user.id}</td>
                            <td>${user.firstName}</td>
                            <td>${user.lastName}</td>
                            <td>${user.country}</td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
            <c:if test="${empty users}">
                <div class="d-flex justify-content-center">
                    <p>There are no records in the database</p>
                </div>            
            </c:if>
        </div>    
    <jsp:include page="footer.jsp" />

从控制器调试:

user: User(id=111111, firstName=Wick, lastName=England, country=John)
user: User(id=111112, firstName=Madman, lastName=USA, country=Andy)

UserService.cls 从 h2 返回 List 并且它在调试中,它 看起来不错。

@Transactional
public List<User> getAll() {
    return (List<User>) this.userRepository.findAll();
}

截图:

这可能是什么问题?

【问题讨论】:

  • jsp文件中不包含用户模型类。
  • @reporter 好点,我添加了导入但行为是相同的。 表还是空的。
  • @ArvindKumarAvinash JSP 名称为 users,与属性名称“users”相同。
  • 我不确定,但你能不能替换这部分:`map.addAttribute("users", this.userService.getAll());` 用删除一个列表并在你的模型中分配该列表喜欢你这里的吗?
  • @user404 我已经尝试过了,但它没有帮助;(我什至创建了具有相同属性的内部 tmp 类并将其添加到列表中以摆脱 User.cls 关系,但它没有帮助...

标签: java spring-boot jsp


【解决方案1】:

在 JSP 的顶部添加以下行:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

另外,替换

return "/users";

return "users";

【讨论】:

  • 我在 header.jsp 中添加了这个
  • 不,它不起作用。文档说它应该返回应该是“/users”的资源映射。它也可以被包裹在 ModelAndView 对象中。
  • 那些链接根本没有用,我在票证中提到 String 类型正在成功呈现。问题在于 Java 类对象的 List 类型。
  • 你检查过我的代码吗?我的代码是一样的,它不起作用。 JSP 中的列表是空的,那里有一个空检查。
  • 我在 header.jsp 中添加了我的“c”标签库以及我在每个页面上导入的其他通用标签。但是,它似乎不起作用,所以我将它添加到当前页面并开始工作。我将此答案标记为最佳答案,谢谢。
猜你喜欢
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
  • 2018-01-13
  • 2019-06-04
  • 2010-12-07
  • 2019-05-15
  • 2020-04-13
相关资源
最近更新 更多