SpringBoot+thymeleaf做分页时出现了这个问题,怎么解决呢?

SpringBoot+thymeleaf做分页时出现了这个问题,请问怎么解决

Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "page.content" (template: "admin/typesadmin" - line 62, col 21)

Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "page.content" (template: "admin/typesadmin" - line 62, col 21)

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'content' cannot be found on null

 

附部分代码

                <tr th:each="type,iterStat : ${page.content}">
                    <th th:text="${iterStat.count}" class="m-text-thin center aligned"></th>
                    <th th:text="#{type.name}" class="m-text-thin center aligned">失物招领</th>
                    <th class="m-text-thin center aligned">
<!--                        <a href="#" th:href="@{/admin/types/{id}/input(id=${type.id})}" class="ui mini teal basic button">编辑</a>-->
<!--                        <a href="#" th:href="@{/admin/types/{id}/delete(id=${type.id})}" class="ui mini red basic button">删除</a>-->
                    </th>
                </tr>

Contoller层

package com.example.hzw.web.admin;

import com.example.hzw.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author :hzw
 * @date :Created in 2021/4/26 20:07
 */
@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private TypeService typeService;
    @GetMapping("/types")
    public String types(@PageableDefault(size = 8 ,sort ={"id"} ,direction = Sort.Direction.DESC)
                                    Pageable pageable, Model model){
        model.addAttribute("page",typeService.ListType(pageable));
        return "admin/types";
    }
    @GetMapping("/typesadmin")
    public String typesadmin(){
        return "admin/typesadmin";

    }

    @GetMapping("/types/input")
    public String input(){
        return "admin/typesadmin";

    }
}

 

service实现

package com.example.hzw.service;

import com.example.hzw.NotFoundException;
import com.example.hzw.dao.TypeRepository;
import com.example.hzw.po.Type;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author :hzw
 * @date :Created in 2021/4/26 19:14
 */
@Service
public class TypeServiceImpl implements TypeService{

    @Autowired
    private TypeRepository typeRepository;

    @Transactional
    @Override
    public Type saveType(Type type) {
        return typeRepository.save(type);
    }

    @Transactional
    @Override
    public Type getType(Long id) {

        return typeRepository.findById(id).orElse(null);
    }

    @Transactional
    @Override
    public Page<Type> ListType(Pageable pageable) {

        return typeRepository.findAll(pageable);
    }

    @Transactional
    @Override
    public Type updateType(Long id, Type type) {
        Type t = typeRepository.findById(id).orElse(null);
        if(t == null)
        {
            throw new NotFoundException("类型不存在!");
        }
        BeanUtils.copyProperties(type,t);

        return typeRepository.save(t);
    }

    @Transactional
    @Override
    public void deleteType(Long id) {
        typeRepository.deleteById(id);

    }
}

 

 Property or field 'content' cannot be found on null,没有content变量,看看后台设置的变量名称是不是对的,名称应该是page吧

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

 

看看你前端调用时,接口给你返回的数据

 

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y