如何通过服务类而不是存储库进行页面请求?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何通过服务类而不是存储库进行页面请求?相关的知识,希望对你有一定的参考价值。
我正在尝试解决一个小练习,这是一个小应用程序,我可以在那里发帖子,或者向上或向下投票。这些帖子是根据他们的投票订购的,并且是分页的。我能够通过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";
}
}
<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>
答案
要从服务请求,您需要编写自定义代码。在我看来,我认为这不是一个好主意。如果你想这样做,你可以像这样实现
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;
}
另一答案
可能,你试图循环错误的对象:
改变这个:
<tr th:each="post : ${posts.content}">
这样:
<tr th:each="post : ${posts}">
希望这可以帮助!
以上是关于如何通过服务类而不是存储库进行页面请求?的主要内容,如果未能解决你的问题,请参考以下文章