问题出现在Java中通过jsp显示列表时。

huangapple 未分类评论48阅读模式
英文:

Problem with displaying List by jsp in Java

问题

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>博客网站-帖子</title>
  5. </head>
  6. <body>
  7. <div class="container">
  8. <table class="table table-striped">
  9. <thead>
  10. <tr>
  11. <th>描述</th>
  12. <th>日期</th>
  13. <th>已完成</th>
  14. <th>作者</th>
  15. </tr>
  16. </thead>
  17. <tbody>
  18. <c:forEach items="${posts}" var="post">
  19. <tr>
  20. <td>${post.postId}</td>
  21. <td><fmt:formatDate pattern="dd/MM/yyyy"
  22. value="${post.postDate}" /></td>
  23. <td>${post.text}</td>
  24. <td>${post.authorId}</td>
  25. </tr>
  26. </c:forEach>
  27. </tbody>
  28. </table>
  29. </div>
  30. </body>
  31. </html>
  1. @Controller
  2. public class PostController {
  3. @Autowired
  4. PostService postService;
  5. @RequestMapping(value="/posts",method = RequestMethod.GET)
  6. public String showPosts(ModelMap model) {
  7. model.addAttribute("posts",postService.findall());
  8. return "posts";
  9. }
  10. }
  1. public class Post {
  2. @Id
  3. @GeneratedValue(strategy=GenerationType.IDENTITY)
  4. private int postId;
  5. //@Column(unique=true)
  6. //@NotEmpty
  7. private String title;
  8. //@Column(length=20000)
  9. @Size(max=20000, message="消息不能超过20000个字符!")
  10. //@NotEmpty
  11. private String text;
  12. private Date postDate;
  13. private int authorId;
  14. }

希望在 JSP 中显示帖子列表,以查看其工作原理,但显示效果如图所示。是否有 JSP 或控制器的问题?

英文:

Hi i have a problem with my project,That's my posts.jsp

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html&gt;
  3. &lt;head&gt;
  4. &lt;title&gt;Blog Site-posts&lt;/title&gt;
  5. &lt;/head&gt;
  6. &lt;body&gt;
  7. &lt;div class=&quot;container&quot;&gt;
  8. &lt;table class=&quot;table table-striped&quot;&gt;
  9. &lt;thead&gt;
  10. &lt;tr&gt;
  11. &lt;th&gt;Description&lt;/th&gt;
  12. &lt;th&gt;Date&lt;/th&gt;
  13. &lt;th&gt;Completed&lt;/th&gt;
  14. &lt;th&gt;Written by&lt;/th&gt;
  15. &lt;/tr&gt;
  16. &lt;/thead&gt;
  17. &lt;tbody&gt;
  18. &lt;c:forEach items=&quot;${posts}&quot; var=&quot;Post&quot;&gt;
  19. &lt;tr&gt;
  20. &lt;td&gt;${Post.postId}&lt;/td&gt;
  21. &lt;td&gt;&lt;fmt:formatDate pattern=&quot;dd/MM/yyyy&quot;
  22. value=&quot;${post.postDate}&quot; /&gt;&lt;/td&gt;
  23. &lt;td&gt;${Post.text}&lt;/td&gt;
  24. &lt;td&gt;${Post.authorId}&lt;/td&gt;
  25. &lt;/tr&gt;
  26. &lt;/c:forEach&gt;
  27. &lt;/tbody&gt;
  28. &lt;/table&gt;
  29. &lt;/div&gt;
  30. &lt;/body&gt;
  31. &lt;/html&gt;

That's my PostController

  1. @Controller
  2. public class PostController {
  3. @Autowired
  4. PostService postService;
  5. @RequestMapping(value=&quot;/posts&quot;,method = RequestMethod.GET)
  6. public String showPosts(ModelMap model) {
  7. model.addAttribute(&quot;posts&quot;,postService.findall());
  8. return &quot;posts&quot;;
  9. }
  10. }

and that is my post class

  1. public class Post {
  2. @Id
  3. @GeneratedValue(strategy=GenerationType.IDENTITY)
  4. private int postId;
  5. //@Column(unique=true)
  6. //@NotEmpty
  7. private String title;
  8. //@Column(length=20000)
  9. @Size(max=20000, message=&quot;Message cannot be longer than 20000 characters!&quot;)
  10. //@NotEmpty
  11. private String text;
  12. private Date postDate;
  13. private int authorId;

I want to display elements of List with posts in jsp to see how it works , but it looks like this :

enter image description here

What's wrong with jsp or controller?

答案1

得分: 0

将以下内容添加到 posts.jsp 文件的 html 标签之前:

  1. &lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot;
  2. pageEncoding=&quot;ISO-8859-1&quot;%&gt;
  3. &lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot;%&gt;
英文:

Add this to posts.jsp before tag html :

  1. &lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot;
  2. pageEncoding=&quot;ISO-8859-1&quot;%&gt;
  3. &lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot;%&gt;

huangapple
  • 本文由 发表于 2020年5月19日 20:21:59
  • 转载请务必保留本文链接:https://java.coder-hub.com/61890955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定