pageRequest无法转换为root用户可分页

时间:2017-10-28 01:10:53

标签: java spring-mvc spring-data-jpa

我是Java Spring初学者。我正在创建一个博客项目,我需要在其中加载5个最新帖子。我遇到了这个问题而且我不知道如何修复它 //忽略我//忽略我//忽略我//不理我//不理我//不理我//不理我 PostRepository:

import java.awt.print.Pageable;
import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

public interface PostRepository extends CrudRepository<Post, Long>{
    @Query("SELECT p FROM Post p LEFT JOIN FETCH p.author ORDER BY p.date DESC")
    List<Post> findLatest5Posts(Pageable pageable);
}

PostServicesImp:

import java.awt.print.Pageable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import havan.blog.demo.models.Post;
import havan.blog.demo.models.PostRepository;

@Service
@Primary
public class PostServicesImpl implements PostServices{
    @Autowired
    private PostRepository postRepo;

    @Override
    public List<Post> findAll() {
        // TODO Auto-generated method stub
        return (List<Post>) this.postRepo.findAll();
    }

    @Override
    public List<Post> findLatest5() {
        // TODO Auto-generated method stub
        return (List<Post>) this.postRepo.findLatest5Posts((Pageable)new PageRequest(0, 5));
    }

HomeController中:

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import havan.blog.demo.models.LoginForm;
import havan.blog.demo.models.Post;
import havan.blog.demo.services.NotiService;
import havan.blog.demo.services.PostServices;
import havan.blog.demo.services.UserService;

@Controller
public class HomeController {

    @Autowired
    private PostServices pServices;

    @RequestMapping(path="/*")
    public String index(Model model){
        List<Post> latest5Posts=pServices.findLatest5();
        model.addAttribute("latest5posts",latest5Posts);

        List<Post> allPosts= pServices.findAll();
        model.addAttribute("allPosts",allPosts);

        return "index";
    }
}

//忽略我//忽略我//忽略我//忽略我//忽略我//忽略我//忽略我

1 个答案:

答案 0 :(得分:4)

已经很晚了,但也许对其他人有帮助。

您已从import 'package:flutter/material.dart'; class GridWidget extends StatefulWidget { @override _GridWidgetState createState() => new _GridWidgetState(); } class _GridWidgetState extends State<GridWidget> { @override Widget build(BuildContext context) { Color cellColor = Colors.white; Text cellText = new Text(''); // when a cell is tapped, change the color and text _changeCell(index) { setState(() { cellColor = Colors.lightBlue; cellText = new Text('clicked'); }); print("Container clicked " + index.toString()); } // create a 5 by 5 grid return new GridView.count( crossAxisCount: 5, children: new List.generate(5, (index) { return new GestureDetector( onTap: _changeCell(index), child: new Container( width: double.infinity, height: double.infinity, decoration: new BoxDecoration( color: cellColor, ), child: new Center( child: cellText, ), ), ); }), ); } } 导入了类Pageable,但需要来自Spring的java.awt.print.Pageable

Pageable