如何显示当前用户数据?

时间:2019-01-17 12:13:54

标签: java spring

如何显示当前用户数据?在这里,我试图显示已登录的用户帖子。但我无法解决问题。我不明白如何显示当前用户数据。

后控制器类:

@RequestMapping(value="/viewcurrentpost")
public String showCurrentPost(Model m, Principal principal) {
    boolean hasPost = false;
    if(principal != null) {
        hasPost = postService.hasPost(principal.getName());
    }
    m.addAttribute("hasPost", hasPost);
    return "viewcurrentpost";
}

邮政服务课程:

public boolean hasPost(String name) {
    if(name == null)
        return false;
    List<Post> post = postDao.getCurrentPost(name);
    if(post.size() == 0)
        return false;
    return true;
} 

道课后:

  public List<Post> getCurrentPost(String username) {
    return jdbc.query("select * from Post, users where post.username=users.username and users.enabled=true and post.username=:username", 
            new MapSqlParameterSource("username", username), new RowMapper<Post>() {

        @Override
        public Post mapRow(ResultSet rs, int rowNum) throws SQLException {
            User user = new User();
            user.setUsername(rs.getString("username"));
            Post post = new Post();
            post.setId(rs.getInt("id"));
            post.setImage(rs.getString("image"));
            post.setTitle(rs.getString("title"));
            post.setDate(rs.getString("date"));
            post.setDescription(rs.getString("description"));
            post.setUser(user);
            return post;
            }

        });
    }

帖子查看页面:

<tr>
    <th>ID</th><th>IMAGE</th><th>TITLE</th><th>DATE</th<th>DESCRIPTION</th><th>USERNAME</th><th>ACTION</th>
 </tr>
             <c:forEach var="hasPost" items="${hasPost}">
                <tr>
                    <td>${hasPost.id}</td>
                    <td>${hasPost.image}</td>
                    <td>${hasPost.title}</td>
                    <td>${hasPost.date}</td>
                    <td>${hasPost.description}</td>
                    <td >${hasPost.username}</td>
                </tr>
                </c:forEach>

1 个答案:

答案 0 :(得分:0)

您不是从控制器发送action="/users/register"。您可以做的一件事是。您可以单独获取帖子列表。

List<Post>

在HTML中,您可以像

一样发送
@RequestMapping(value="/viewcurrentpost")
public String showCurrentPost(Model m, Principal principal) {
    boolean hasPost = false;
    if(principal != null) {
        hasPost = postService.hasPost(principal.getName());
    }
   List<Post> posts = postDao.getCurrentPost(principal.getName());
           m.addAttribute("post", posts);
    m.addAttribute("hasPost", hasPost);

    return "viewcurrentpost";
}