2017-11-03 6 views
1

gut morining jeder Körper,Thymeleaf Fehler Federverschluß JPA

i in einem Projekt arbeiten Frühjahr Boot mit und ich versuche thymleaf zu integrieren Informationen aus der Datenbank zu bekommen, es ist das erste Mal, dass ich thymleaf verwenden,

hier meine Entitätsklasse:

package com.org.model; 

import java.io.Serializable; 
import javax.persistence.*; 

import com.fasterxml.jackson.annotation.JsonIgnore; 
import com.fasterxml.jackson.annotation.JsonManagedReference; 

import java.math.BigDecimal; 
import java.util.List; 


/** 
* The persistent class for the countries database table. 
* 
*/ 
@Entity 
@Table(name="countries") 
//@NamedQuery(name="Country.findAll", query="SELECT c FROM Country c") 

public class Country implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    @Column(name="COUNTRY_ID") 
    private String countryId; 

    @Column(name="COUNTRY_NAME") 
    private String countryName; 

    //bi-directional many-to-one association to Region 

    @ManyToOne 
    @JoinColumn(name="REGION_ID") 
    private Region region; 

    //bi-directional many-to-one association to Location 
    @JsonIgnore 
    @OneToMany(mappedBy="country") 
    private List<Location> locations; 

    public Country() { 
    } 

    public String getCountryId() { 
     return this.countryId; 
    } 

    public void setCountryId(String countryId) { 
     this.countryId = countryId; 
    } 

    public String getCountryName() { 
     return this.countryName; 
    } 

    public void setCountryName(String countryName) { 
     this.countryName = countryName; 
    } 

    public Region getRegion() { 
     return this.region; 
    } 

    public void setRegion(Region region) { 
     this.region = region; 
    } 

    public List<Location> getLocations() { 
     return this.locations; 
    } 

    public void setLocations(List<Location> locations) { 
     this.locations = locations; 
    } 

    public Location addLocation(Location location) { 
     getLocations().add(location); 
     location.setCountry(this); 

     return location; 
    } 

    public Location removeLocation(Location location) { 
     getLocations().remove(location); 
     location.setCountry(null); 

     return location; 
    } 

} 

hier ist mein Controller:

package com.org.controller; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.bind.annotation.RestController; 

import com.org.model.Country; 
import com.org.repository.CountryRepository; 

    @Controller // This means that this class is a Controller 
@RequestMapping(path="/api", produces=MediaType.APPLICATION_JSON_VALUE) // This means that this class is a Controller 

public class CountryController { 
     @Autowired // This means to get the bean called userRepository 
     // Which is auto-generated by Spring, we will use it to handle the data 
    private CountryRepository countryRepository; 



     @RequestMapping(value="/country", method= RequestMethod.GET) 
     public String list(Model model){ 
      model.addAttribute("Country", countryRepository.findAll()); 


      // This returns a JSON or XML with the users 
      return "countries"; 
     } 


    // @RequestMapping(path="/country/{countryId}") 
    // public @ResponseBody Country getCountry(@PathVariable String countryId) { 
    //  return countryRepository.findOne(countryId); 
    // } 
    } 

mache ich eigentlich eine HTML-Datei, die Informationen plotten:

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security"> 
<head lang="en"> 

</head> 
<body> 
<div class="container"> 
    <!--/*/ <th:block th:include="fragments/header :: header"></th:block> /*/--> 

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

     <table class="table table-striped"> 
      <tr> 
       <th>Country Id</th> 
       <th>Country Name</th> 
       <th>Region Id</th> 
       <th>Location</th> 

      </tr> 
      <tr th:ForEach="Country : ${countries}"> 
       <td th:text="${Country.countryId}">id country</td> 
       <td th:text="${Country.countryName}">Country Name</td> 
       <td th:text="${Country.region}">Region Id</td> 
       <td th:text="${Country.locations}">Location</td> 

      </tr> 
     </table> 

    </div> 

</div> 

</body> 
</html> 

i th thymleaf Abhängigkeit

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>xxxx.org</groupId> 
    <artifactId>project</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 
    <name>webService</name> 
    <description>first project for university</description> 
    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.7.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 
    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
    </properties> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-rest</artifactId> 
     </dependency> 
<dependency> 
    <groupId>io.springfox</groupId> 
    <artifactId>springfox-swagger2</artifactId> 
    <version>2.6.1</version> 
    <scope>compile</scope> 
</dependency> 
<dependency> 
    <groupId>io.springfox</groupId> 
    <artifactId>springfox-swagger-ui</artifactId> 
    <version>2.6.1</version> 
    <scope>compile</scope> 
</dependency> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
    <groupId>org.modelmapper</groupId> 
    <artifactId>modelmapper</artifactId> 
    <version>1.1.0</version> 
</dependency> 
     <dependency> 
      <groupId>org.thymeleaf</groupId> 
      <artifactId>thymeleaf-spring4</artifactId> 
     </dependency> 
    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

ich keine Fehler kam zu dem pom.xml hinzugefügt, aber weißer Bildschirm kein erro in der Konsole:

[Screenshot des localhost] https://i.stack.imgur.com/6xx1l.png

+0

Sie verwenden 'Länder' zweimal ... '$ {Länder}' ist die Liste so '$ {countries.countryId}' verweist auf die Liste. Sie möchten wahrscheinlich etwas wie 'th: each =" country: $ {countries} "' dann verwenden Sie innerlich '$ {country.}' Anstelle von '$ {countries}'. –

+0

Ich weiß nicht warum, aber jetzt habe ich einen weißen screan, keinen Fehler in der Consol in Finsternis !! –

+0

für Ihren Fehler in der Konsole müssen Sie öffentliche Getter für Ihre Land Entitätsattribute haben. – Patrick

Antwort

0
<tr th:ForEach="countries : ${countries}"> 

In der obigen Zeile Sie sind Verwenden von Ländern, die auf Liste und jedes Element in der Liste verweisen und nicht differenzieren können. So können Sie versuchen, den folgenden Code-Schnipsel

<tr th:ForEach="country : ${countries}"> 
+0

Ja, ich habe bereits das und jetzt habe ich einen weißen screan, kein Fehler in der Consol in Eclipse !! –

0

Sie definieren das Attribut „Country“ in Ihrem Modell und es mit der Liste der Länder bevölkern:

model.addAttribute("Country", countryRepository.findAll()); 

Aber dann sind Sie auf der Suche nach "Länder":

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

So können Sie entweder tun:

model.addAttribute("countries", countryRepository.findAll()); 

Oder diese:

<div th:if="${not #lists.isEmpty(Country)}"> 
... 
<tr th:ForEach="element : ${Country}"> 
Verwandte Themen