【问题标题】:Binding With Multiple Tables Spring Thymeleaf绑定多个表 Spring Thymeleaf
【发布时间】:2021-06-10 15:56:11
【问题描述】:

我知道这个问题的标题不清楚,但在不喜欢之前让我解释一下我的问题。

我有一个使用 JPA 和百里香的 java spring 社交媒体。

用户有 ID、用户名和创建日期。

在帖子表中有一个名为“authorid”的字段,因此当有人发布新帖子时,系统只会写入他的用户 ID,而不是他的用户名。

现在,我在网站上有一个页面,位于“localhost:8080/showAllPosts/”,它显示了从最近到最近的所有应用程序帖子。控制器看起来像这样:

@Controller
public class MyController {

  @GetMapping("/showAllPosts") 
  public String showAllPosts(Model model) {

    List<Post> post = new ArrayList<>();
    model.addAttribute("post", post);

    List<Post> posts = postRepository.findAll();
    model.addAttibute("posts", posts);

    return "showallposts.html";
  }
}

showallposts.html 页面如下所示:

<table>
                    <tbody>
                        <tr th:if="${posts.empty}">
                            <td colspan="2">No Posts Available</td>
                        </tr>
                        <tr th:each="post : ${posts}">
                            <td>                                
<span th:text="'posted by ' + ${post.authorid}>
                                <span th:text="${post.postcontent}"></span>
                                <br>                                
                            </td>
                            <td>                            
                            </td>
                        </tr>
                    </tbody>
                </table>

如您所见,它显示了帖子内容,以及“发布者”加上作者。唯一的问题是 post.authorid 返回发布用户的数字 ID,而我想要用户名。我们可以对用户模型做些什么吗?

请注意,在用户存储库中我有这个方法:

@Query(value = "SELECT u.username FROM Users u WHERE u.userid = ?1")
public String findUsernameById(Long userid);

感谢所有帮助!

【问题讨论】:

    标签: java spring jpa data-binding thymeleaf


    【解决方案1】:

    或者,您可以使用 DTO 将带有作者姓名的帖子移动到 Thymeleaf 视图中。

    1. 创建一个像 PostDto 这样的新类:
    public class PostDto {
        private Long id;
        private String postContent;
        private Long authorId;
        private String String authorName;
    
        // other fields
        // getters and setters
    }
    
    1. 使用 Stream API 将您的帖子实体类转换为 PostDto。在映射过程中,可以通过UserRespository通过authorId获取用户名,并在post dto对象中补充作者姓名。
    @Controller
    public class MyController {
    
        @Autowired
        private UserRepository userRepository;
    
        @GetMapping("/showAllPosts") 
        public String showAllPosts(Model model) {
            List<Post> post = new ArrayList<>();
    
            model.addAttribute("post", post);
    
            List<Post> posts = postRepository.findAll().stream()
                .map(post -> {
                    PostDto dto = toDto(post);
                    dto.setAuthorName(userRepository.findUsernameById(post.getAuthorId()));
                })
    
            model.addAttibute("posts", posts);
    
            return "showallposts.html";
        }
    
    
        private PostDto toDto(Post post) {
            PostDto dto = new PostDto();
            dto.setId(post.getId);
            dto.setPostContent(post.getPostContent());
            dto.setAuthorId(post.getAuthorId());
        }
    }
    
    1. 在 Thymeleaf 视图中使用 ${post.authorName}
    <table>
        <tbody>
            <tr th:if="${posts.empty}">
                <td colspan="2">No Posts Available</td>
            </tr>
            <tr th:each="post : ${posts}">
                <td>
                    <span th:text="'posted by ' + ${post.authorName}>
                    <span th:text="${post.postcontent}"></span>
                    <br> 
                </td>
                <td>
                </td>
            </tr>
        </tbody>
    </table>
    

    【讨论】:

    • 嗨@mamadaliev,首先感谢您的回答和分享工作知识。一切正常,但在 MyController 类中,您提出的代码在为 List 对象进行流式传输时会出错。错误是:此行有多个标记 - Lambda 表达式的参数 post 无法重新声明在封闭范围内定义的另一个局部变量。 - Stream 类型中的方法 map(Function super Post,? extends R>) 不适用于参数 (( post) -> {})。
    • 请。回答。
    猜你喜欢
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 2019-04-14
    • 1970-01-01
    • 2019-10-21
    • 2016-02-28
    相关资源
    最近更新 更多