Solution 1 :

change variable name from film to films in controller:

model.addAttribute("films",film);

and in html file :

<li th:each ="film: ${films}">

Problem :

I’m trying to display data from a database in a spring application.

<div class="container-fluid">
    <h1>Welcome</h1>
    <p>Some text</p>
</div>
<ul>
    <!--/@thymesVar id="film" type="java.util.List<com.example.project1.accessToData.model.Film>"/-->
    <li th:each ="film:${film}">
       <span th_text="${film}"></span>
    </li>
</ul>

public FilmIndexControler(FilmService filmService) {
    this.filmService = filmService;
}

    @GetMapping
    String showFilms(Model model){
        Iterable<Film> film = filmService.findAll();
        model.addAttribute("film",film);
        return "index";
}

public class Film {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotEmpty(message="Cant be empty")
private String title;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate productionYear;
private String descryption;
@NotEmpty (message="Cant be empty")
private String link}



    public Iterable<Film> findAll () {
    return this.filmRepository.findAll();
}

When I try to display it on the site, nothing related to the spring happens. I can’t see films in html.

@RestController

@RequestMapping(“/api/film”)
public class FilmApi {

private static final Logger logger = LoggerFactory.getLogger(FilmApi.class);
private final FilmManager filmManager;


@Autowired
public FilmApi(final FilmManager filmManager) {
    this.filmManager = filmManager;

}

@GetMapping(value = "/lista",params = {"!sort", "!page", "!size"} )
public ResponseEntity<Iterable<Film>> getAll(){
    logger.warn("Lista filmów");
    return ResponseEntity.ok(this.filmManager.findAll());
}


@GetMapping
public ResponseEntity<List<Film>>  findFilm(@Param("title") String title){
    return ResponseEntity.ok(this.filmManager.findFilm(title));
}

@PostMapping
public ResponseEntity<Film> addFilm(@RequestBody Film film){
    this.filmManager.save(film);
    return ResponseEntity.noContent().build();
}

@PutMapping("/{id}")
public ResponseEntity<?> updateFilm(@PathVariable Long id,@RequestBody Film film){
    if (!this.filmManager.exist(id)){
        return  ResponseEntity.notFound().build();
    }
    film.setId(id);
    this.filmManager.save(film);
    return ResponseEntity.noContent().build();
}

@DeleteMapping
public void deleteFilm(@RequestParam Long index){
    if (!this.filmManager.exist(index)){
          ResponseEntity.notFound().build();
    }
    this.filmManager.deleteById(index);
    ResponseEntity.noContent().build();
}

}

Ok I added api controller. When I enter localhostapi:8080/api/film/lista, all data is displayed If you need anything else, I will add it soon.

Comments

Comment posted by tour

welcome to stackoverflow. Take a

Comment posted by Awros

Still nothing appears

Comment posted by Hemant

Did you debug and see if

Comment posted by Awros

This is probably a problem. If I go to localhost:8080/films it shows me everything from the database, but this method show nothing. I would be grateful if you could direct me where I could make a mistake because I am a beginner in Spring.

Comment posted by Hemant

You must have an api

Comment posted by Awros

Ok, I added this as answer

By

Leave a Reply

Your email address will not be published. Required fields are marked *