如何通过服务类而不是存储库进行页面请求?

时间:2017-12-29 12:18:43

标签: java spring spring-mvc pagination

我正在尝试解决一个小应用程序的小练习,在那里我可以发布帖子并向上或向下投票。这些帖子是根据他们的投票订购的,并且是分页的。我可以通过PageRequest方法通过存储库findAll()方法在控制器中创建它,但我想通过服务类来实现,但在这种情况下我有以下错误:

2017-12-29 12:50:54.928 ERROR 1756 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "posts": Exception evaluating SpringEL expression: "posts.content" (posts:32)
2017-12-29 12:50:54.941 ERROR 1756 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "posts.content" (posts:32)] with root cause

这是我的代码,有人可以帮忙解决这个问题吗?

@Entity
public class Post {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String content;
    private int score;

    public Post(String post, int score) {
        this.content = post;
        this.score = score;
    }

    public Post(String post) {
        this.content = post;
        score = 0;
    }

    public Post() {

    }
    //getters, setters
}

服务

@Service
   public class PostService {

       @Autowired
       PostRepo postRepo;

       public List<Post> returnPosts(PageRequest pageRequest) {
       return postRepo.findAll();
       }
       ...
    }

控制器

@Controller
public class PostController {

    @Autowired
    PostRepo postRepo;

    @Autowired
    PostService postService;


    @GetMapping({"", "/"})
    public String listPosts(Model model, @RequestParam(defaultValue = "0") int page) {
        model.addAttribute("posts", postService.returnPosts(new PageRequest(page, 5, Sort.Direction.DESC, "score")));
        model.addAttribute("currentPage", page);
        return "posts";
    }
}

HTML

<div class="container">
    <div class="card">
        <div class="card-header">
            <button class="btn btn-success nBtn">New Post</button>
        </div>
        <div class="card-block">
            <table class="table table-hover table-condensed">
                <thead>
                <tr>
                    <th class="th-id">Id</th>
                    <th class="th-content">Content</th>
                    <th class="th-vote"></th>
                    <th class="th-vote"></th>
                    <th class="th-score">Score</th>
                    <th class="th-edit">Edit</th>
                    <th class="th-del">Delete</th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="post : ${posts.content}">
                    <td th:text="${post.id}"></td>
                    <td th:text="${post.content}"></td>
                    <td class="td-vote"><a th:href="'/' + ${post.id} + '/upvote'"><img th:src="@{/arrow-top.png}" /></a></td>
                    <td class="td-vote"><a th:href="'/' + ${post.id} + '/downvote'"><img th:src="@{/arrow-down.png}" /></a></td>
                    <td class="td-score" th:text="${post.score}"></td>
                    <td class="th-edit"><a th:href="@{findOne/(id=${post.id})}" class="btn btn-primary eBtn">Edit</a></td>
                    <td class="th-del"><a th:href="@{delete/(id=${post.id})}" class="btn btn-danger delBtn">Delete</a></td>
                </tr>
                </tbody>
            </table>
            <hr/>
            <ul class="nav nav-pills">
                <li class="nav-item" th:each="i : ${#numbers.sequence(0, posts.totalPages-1)}">
                    <a th:href="@{/(page=${i})}" th:text="${i}" class="nav-link"
                       th:classappend="${currentPage} == ${i} ? 'active' : ''"></a>
                </li>
            </ul>
        </div>
    </div>
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

可能您正试图绕过错误的对象:

改变这个:

<tr th:each="post : ${posts.content}">

由此:

<tr th:each="post : ${posts}">

希望这有帮助!

答案 1 :(得分:0)

要从服务提出请求,您需要编写自定义代码。在我看来,我认为这不是一个好主意。如果你想这样做,你可以像这样实现

public Page<Post> returnPosts(PageRequest pageRequest) {
    Page<T> page = null;
    try {

        String queryStr = "Your Query";
        Query query = entityManager.createNativeQuery(queryStr, Post.class);
        query.setFirstResult((pageRequest.getpageNumber()) * pageRequest.getPageSize());
        query.setMaxResults(pageSize);
        List<Post> posts= query.getResultList();
        page = new PageImpl<>(posts, pageRequest, 0);
    }catch(Exception ex){
        ex.printStackTrace();
    }
    return page;
}