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

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

Problem with displaying List by jsp in Java

问题

<!DOCTYPE html>
<html>
<head>
<title>博客网站-帖子</title>
</head>
<body>
<div class="container">
    <table class="table table-striped">
        <thead>
            <tr>
                <th>描述</th>
                <th>日期</th>
                <th>已完成</th>
                <th>作者</th>
            </tr>
        </thead>
        <tbody>
            <c:forEach items="${posts}" var="post">
                <tr>
                    <td>${post.postId}</td>
                    <td><fmt:formatDate pattern="dd/MM/yyyy"
                            value="${post.postDate}" /></td>
                    <td>${post.text}</td>
                    <td>${post.authorId}</td>
                </tr>
            </c:forEach>
        </tbody>
    </table>
</div>
</body>
</html>
@Controller
public class PostController {
    
    @Autowired
    PostService postService;

    @RequestMapping(value="/posts",method = RequestMethod.GET)
    public String showPosts(ModelMap model) {
        model.addAttribute("posts",postService.findall());
        return "posts";
    }
}
public class Post {
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int postId;
    
    //@Column(unique=true)
    //@NotEmpty
    private String title;
    //@Column(length=20000)
    @Size(max=20000, message="消息不能超过20000个字符!")
    //@NotEmpty
    private String text;
    private Date postDate;
    private int authorId;
}

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

英文:

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

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Blog Site-posts&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
 &lt;div class=&quot;container&quot;&gt;
	&lt;table class=&quot;table table-striped&quot;&gt;
		&lt;thead&gt;
			&lt;tr&gt;
				&lt;th&gt;Description&lt;/th&gt;
				&lt;th&gt;Date&lt;/th&gt;
				&lt;th&gt;Completed&lt;/th&gt;
				&lt;th&gt;Written by&lt;/th&gt;
			&lt;/tr&gt;
		&lt;/thead&gt;
		&lt;tbody&gt;
			&lt;c:forEach items=&quot;${posts}&quot; var=&quot;Post&quot;&gt;
				&lt;tr&gt;
					&lt;td&gt;${Post.postId}&lt;/td&gt;
					&lt;td&gt;&lt;fmt:formatDate pattern=&quot;dd/MM/yyyy&quot;
							value=&quot;${post.postDate}&quot; /&gt;&lt;/td&gt;
					&lt;td&gt;${Post.text}&lt;/td&gt;
					&lt;td&gt;${Post.authorId}&lt;/td&gt;
				&lt;/tr&gt;
			&lt;/c:forEach&gt;
		&lt;/tbody&gt;
	&lt;/table&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

That's my PostController

@Controller
public class PostController {
	
	@Autowired
	PostService postService;

	@RequestMapping(value=&quot;/posts&quot;,method = RequestMethod.GET)
	public String showPosts(ModelMap model) {
		model.addAttribute(&quot;posts&quot;,postService.findall());
		return &quot;posts&quot;;
	}
}

and that is my post class

public class Post {

@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int postId;
	
	//@Column(unique=true)
	//@NotEmpty
	private String title;
	//@Column(length=20000)
	@Size(max=20000, message=&quot;Message cannot be longer than 20000 characters!&quot;)
	//@NotEmpty
	private String text;
	private Date postDate;
	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 标签之前:

&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot;
    pageEncoding=&quot;ISO-8859-1&quot;%&gt;
&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 :

&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot;
    pageEncoding=&quot;ISO-8859-1&quot;%&gt;
&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:

确定