【问题标题】:Listing objects from database to html page via springboot and thymeleaf通过 springboot 和 thymeleaf 从数据库中列出对象到 html 页面
【发布时间】:2018-01-15 15:52:48
【问题描述】:

我在我的项目中使用 Springs MVC 框架和 thymeleaf,我在网上搜索了有关如何通过表格在 html 页面上显示数据库中的对象列表的教程,但没有成功。如果你们有任何推荐的教程,请告诉我,或者如果你知道如何实现我正在尝试做的事情

这是我的控制器代码,因为它不正确,所以什么也没做:

@Controller
public class HomeController {
@Autowired
TeamRepository teamRepository;
  @RequestMapping(value="/showteams", method=RequestMethod.GET)
   public Set<Team> teams()
   {       
       Set<Team> teams = (Set<Team>) teamRepository.findAll();         
       return teams;
   }

我的仓库代码:

@Repository
public interface TeamRepository extends JpaRepository<Team, Long> {     
    Team findByTeamName(String teamname);
    Set<Team> listAllTeams();    
}

这是我的团队实体代码:

@Entity
@Table(name = "team")   
public class Team implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String teamName;
    private int leaguePosition;
    public String teamAddress;
    public String level;

    @OneToOne
    private Manager manager;

    @OneToOne
    private Physio physio;

    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
     public Set<CoachingStaff> coachingStaff;

    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
     public Set<Player> players;

    //getter setter constructors

}

还有我的html代码:

<title>Insert title here</title>
</head>
<body>
 <div class="container">

 <div th:if="$(not #lists.isEmpty(teams))">

    <h2> Teams List </h2>

    <table class = "table table-striped">       
    <tr>
    <th> Team Name </th>
    </tr>       
    <tr th:each="team : ${teams]">
    <td th:text="${team.teamName}"></td>
    </tr>

    </table>
 </div>        
 </div>   
</body>
</html>

运行我的应用时遇到的错误:

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-01-15 15:55:58.377 ERROR 7584 --- [  restartedMain] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'homeController': Unsatisfied dependency expressed through field 'teamRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property listAllTeams found for type Team!
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at com.FYP.Club.ClubApplication.main(ClubApplication.java:10) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_60]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_60]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_60]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.8.RELEASE.jar:1.5.8.RELEASE]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property listAllTeams found for type Team!
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    ... 24 common frames omitted
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property listAllTeams found for type Team!
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:247) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:398) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:378) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:86) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:64) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:213) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:77) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:436) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:221) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    ... 34 common frames omitted

【问题讨论】:

  • 您测试了哪些内容,遇到了哪些堆栈跟踪错误?
  • @MikeTung 我用跟踪错误更新了我的问题,谢谢

标签: java html spring-mvc spring-boot thymeleaf


【解决方案1】:

首先在您的HomeController 中返回Set&lt;Team&gt; 而不是百里香视图。您应该将您的集合添加到模型中以在您的视图中使用它。

@RequestMapping(value="/showteams", method=RequestMethod.GET)
public String teams()
{
   Set<Team> teams = (Set<Team>) teamRepository.findAll();
   model.addAttribute("teams", teams);
   return "teams";//here your name of your view (html)
}

第二个问题是你的 html。有一个错误

<tr th:each="team : ${teams]">

改成:

<tr th:each="team : ${teams}">

我看到的第三个问题是您的 Repository 注释。通常你不需要指定你的存储库。所以删除注释。

对于您的例外:您需要为您的 listAllTeams() 方法指定自定义 @Query()。但是,如果您想查找所有团队,您扩展的 JPARepository 已经有一个用于该findAll() 的方法。使用它并删除Set&lt;Team&gt; listAllTeams(); 条目。

【讨论】:

    【解决方案2】:

    您好,更改您的存储库添加 @Qualifier("teamRepository") 如下所示,希望它会起作用。

    @Qualifier("teamRepository")
    @Repository
    public interface TeamRepository extends JpaRepository<Team, Long> {     
        Team findByTeamName(String teamname);
        Set<Team> listAllTeams();    
    }
    

    【讨论】:

    • 为什么要@Qualifier("teamRepository") 提供帮助?你对此有什么解释吗?
    • 在控制器中,他正在使用“TeamRepository teamRepository”;我发现了这样的错误 - 错误创建名为'teamRepository'的bean:调用init方法失败;嵌套异常是 org.springframework.data.mapping.PropertyReferenceException: No property listAllTeams found for type Team!
    • 出现错误No property listAllTeams found for type Team! 是因为实体团队中没有设置属性。当有多个相同类型的 bean 时,@Qualifier 注释用于解决自动装配冲突。 @Qualifier 注解可以用在任何用@Component 注解的类或用@Bean 注解的方法上。此注解也可以应用于构造函数参数或方法参数。
    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 2018-10-10
    • 2020-12-20
    • 1970-01-01
    • 2020-04-24
    • 2021-07-05
    • 2013-08-15
    • 1970-01-01
    相关资源
    最近更新 更多