【完结,小白错误】Invocation of init method failed;

【完结,小白错误】Invocation of init method failed;今天在写基于MongoDB,freemarker和springboot结合的项目中,出现了以下问题找不到对应id,但是我本身没有用到‘_id’,‘id’?这两个属性,rg.springframework.beans.factory.UnsatisfiedDependencyException:Errorcreatingbeanwithname’userController’:Unsatisfieddependencyexpressedthroughfield’mongoTemp

今天在写基于MongoDB,freemarker和springboot结合的项目中,出现了以下问题
找不到对应id,但是我本身没有用到 ‘_id’,‘id’?这两个属性,

rg.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'mongoTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMongoRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property find found for type User! Did you mean '_id','id'?
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1395) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
	at cn.wolfcode.demo.MongoDbApplication.main(MongoDbApplication.java:10) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMongoRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property find found for type User! Did you mean '_id','id'?
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1762) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1247) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	... 19 common frames omitted
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property find found for type User! Did you mean '_id','id'?
	at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:94) ~[spring-data-commons-2.1.5.RELEASE.jar:2.1.5.RELEASE]
	at 

经过检查,发现是在controller中出现问题,但是具体问题源没有找到,后来通过逐段注释,发现问题出在find方法上

@Controller
public class UserController { 
   
    @Autowired
    private UserMongoRepository userMongoRepositor;
    @Autowired
    private MongoTemplate mongoTemplate;
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){ 
   
        Query query=new Query();
        long count = mongoTemplate.count(query, "users");
        int maxPage;
        if (count%qo.getPageSize()==0){ 
   
            maxPage= (int) (count/qo.getPageSize());
        }else { 
   
            maxPage= (int) (count/qo.getPageSize()+1);
        }
        query.skip(qo.getStart()).limit(qo.getPageSize());
        List<User> list = mongoTemplate.find(query, User.class, "users");
        PageResult<User> result=new PageResult<>();
        result.setTotalcount(count);
        result.setList(list);
        result.setMaxPage(maxPage);
        result.setPageSize(qo.getPageSize());
        result.setCurrentPage(qo.getCurrentPage());
        result.setPrev(qo.getCurrentPage()>1?qo.getCurrentPage()-1:0);
        result.setNext(qo.getCurrentPage()<maxPage?qo.getCurrentPage()+1:0);
        model.addAttribute("result", result);
        return "list";
    }

仔细寻找后发现是调用出错,应调用mongoTemplate,之前错误调用userMongoRepositor
尝试运行


freemarker.core.NonSequenceOrCollectionException: The value you try to list is an extended_hash+string (cn.wolfcode.demo.page.PageResult wrapped into f.e.b.StringModel), thus you must specify two loop variables after the "as"; one for the key, and another for the value, like <#... as k, v>).

----
FTL stack trace ("~" means nesting-related):
	- Failed at: #list result as employee [in template "list.ftl" at line 41, column 21]
----
	at freemarker.core.IteratorBlock$IterationContext.executedNestedContentForCollOrSeqListing(IteratorBlock.java:341) ~[freemarker-2.3.28.jar:2.3.28]
	at freemarker.core.IteratorBlock$IterationContext.executeNestedContent(IteratorBlock.java:271) ~[freemarker-2.3.28.jar:2.3.28]

报错,继续排查前端

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>员工管理</title>
    <link href="jquery/j/bootstrap/css/bootstrap.css">
    <script src="jquery/j/jquery.min.js"></script>
    <script src="jquery/j/jquery.twbsPagination.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="jquery/j/bootstrap/js/bootstrap.min.js"></script>

    <script>
            $('#pagination-demo').twbsPagination({ 
   
                totalPages: ${result.maxPage},
                visiblePages: 3,
                startPage:${result.currentPage},
                version: '1.1',
                onPageClick: function (event, page) { 
   
                    $("#abc").val(page);
                    $("form").submit();
                }
            });
    </script>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
            <h1>员工管理</h1>
    <form action="/list">
        <input id="abc" type="hidden" name="currentPage">
    </form>
    <table class="table table-hover table-bordered">
                    <thead>
                    <tr>
                        <th><input type="checkbox" id="allCb"></th>
                        <th>编号</th>
                        <th>名称</th>
                        <th>email</th>
                        <th>年龄</th>
                        <th>部门</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <#list result.list as employee>
                        <tr>
                            <td><input type="checkbox" class="cb" data-id="${employee.id}"></td>
                            <td>${employee_index+1}</td>
                            <td>${employee.age!}</td>
                            <td>${employee.id!}</td>
                            <td>${employee.name!}</td>
                            <td>${(employee._id)!}</td>
                            <td>
                                <a href="/employee/input.do?id=${employee.id}" class="btn btn-info btn-xs btn_redirect">
                                    <span class="glyphicon glyphicon-pencil"></span> 编辑
                                </a>
                                <a href="/delete.do?id=${employee._id}" class="btn btn-danger btn-xs btn_delete">
                                    <span class="glyphicon glyphicon-trash"></span> 删除
                                </a>
                            </td>
                        </tr>
                    </#list>
                </table>
                <!--分页-->

            </div>

        </section>
    </div>
</div>
<ul id="pagination-demo" class="pagination-sm"></ul>
</body>
</html>

发现问题,页面顺序读取,

<ul id="pagination-demo" class="pagination-sm"></ul>

必须要放在

<script>
    $('#pagination-demo').twbsPagination({ 
   
        totalPages: ${result.maxPage},
        visiblePages: 3,
        startPage: ${result.currentPage},
        onPageClick: function (event, page) { 
   
            // $('#page-content').text('Page ' + page);
            $("#abc").val(page);
            $("form").submit();
        }
    });
</script>

前,

如下正确代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>员工管理</title>
    <link href="jquery/j/bootstrap/css/bootstrap.css">
    <script src="jquery/j/jquery.min.js"></script>
    <script src="jquery/j/jquery.twbsPagination.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="jquery/j/bootstrap/js/bootstrap.min.js"></script>


</head>
<body class="hold-transition skin-blue sidebar-mini">

<div class="wrapper">
    <h1>员工管理</h1>
    <form action="/list">
        <input id="abc" type="hidden" name="currentPage">
    </form>
    <table class="table table-hover table-bordered">
        <thead>
        <tr>
            <th><input type="checkbox" id="allCb"></th>
            <th>编号</th>
            <th>名称</th>
            <th>email</th>
            <th>年龄</th>
            <th>部门</th>
            <th>操作</th>
        </tr>
        </thead>
        <#list result.list as employee>
            <tr>
                <td><input type="checkbox" class="cb" data-id="${employee.id}"></td>
                <td>${employee_index+1}</td>
                <td>${employee.age!}</td>
                <td>${employee.id!}</td>
                <td>${employee.name!}</td>
                <td>${(employee._id)!}</td>
                <td>
                    <a href="/employee/input.do?id=${employee.id}" class="btn btn-info btn-xs btn_redirect">
                        <span class="glyphicon glyphicon-pencil"></span> 编辑
                    </a>
                    <a href="/delete.do?id=${employee._id}" class="btn btn-danger btn-xs btn_delete">
                        <span class="glyphicon glyphicon-trash"></span> 删除
                    </a>
                </td>
            </tr>
        </#list>
    </table>
    <!--分页-->

</div>

</section>
</div>
</div>

<script>
    $('#pagination-demo').twbsPagination({ 
   
        totalPages: ${result.maxPage},
        visiblePages: 3,
        startPage: ${result.currentPage},
        onPageClick: function (event, page) { 
   
            // $('#page-content').text('Page ' + page);
            $("#abc").val(page);
            $("form").submit();
        }
    });
</script>
<ul id="pagination-demo" class="pagination-sm"></ul>
</body>
</html>

运行,仍然报错,但是前端分页框出现了,证明问题出在后端
修改了引入类,

 public String list(Model model, QueryObject qo){ 
   
        Query query=new Query();
        long count = mongoTemplate.count(query, "users");
        int maxPage;
        if (count%qo.getPageSize()==0){ 
   
            maxPage= (int) (count/qo.getPageSize());
        }else { 
   
            maxPage= (int) (count/qo.getPageSize()+1);
        }
        query.skip(qo.getStart()).limit(qo.getPageSize());
        List<User> list = mongoTemplate.find(query,User.class,"users");
        PageResult<User> result=new PageResult<>();
        result.setTotalcount(count);
        result.setList(list);
        result.setMaxPage(maxPage);
        result.setPageSize(qo.getPageSize());
        result.setCurrentPage(qo.getCurrentPage());
        result.setPrev(qo.getCurrentPage()-1);
        result.setNext(qo.getCurrentPage()+1);
        model.addAttribute("result", result);
        return "list";
    }

运行成功
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/11526.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注