I recently started learning Spring from the Thymeleaf template engine for displaying the data. However, I have been facing some problems that I can not solve:
Star Wars Movie Controller
package example.starwars.controllers;
import example.starwars.command.StarWarsMovieCommand;
import example.starwars.services.StarWarsMoviesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Slf4j
@Controller
public class StarWarsMovieController {
private StarWarsMoviesService starWarsMoviesService;
@Autowired
public StarWarsMovieController(StarWarsMoviesService starWarsMoviesService) {
this.starWarsMoviesService = starWarsMoviesService;
}
@RequestMapping({"/", "", "index"})
public String getIndexPage(Model model) {
model.addAttribute("movies", starWarsMoviesService.getStarWarsMovies());
return "index";
}
@GetMapping
@RequestMapping("/movie/{id}/show")
public String showById(@PathVariable String id, Model model){
log.debug("Getting Episode " + id + " page!");
model.addAttribute("movie", starWarsMoviesService.getStarWarsMovie(Long.valueOf(id)));
return "movie/show";
}
@GetMapping
@RequestMapping("/movie/new")
public String newMovie(Model model){
model.addAttribute("movie", new StarWarsMovieCommand());
return "movie/movieform";
}
@GetMapping
@RequestMapping("{movieName}/{id}/edit")
public String updateMovie(@PathVariable String id, @PathVariable String movieName, Model model) {
log.debug("Editing " + movieName);
model.addAttribute("movie", starWarsMoviesService.findCommandById(Long.valueOf(id)));
return "movie/movieform";
}
@GetMapping
@RequestMapping("/movie/{id}/delete")
public String deleteStarWarsMovie(@PathVariable String id){
log.debug("Deleting Episode " + id);
starWarsMoviesService.deleteStarWarsMovie(Long.valueOf(id));
return "redirect:/";
}
@PostMapping
@RequestMapping("movie")
public String saveOrUpdate(@ModelAttribute StarWarsMovieCommand starWarsMovieCommand) {
StarWarsMovieCommand savedCommand = starWarsMoviesService.saveStarWarsMovieCommand(starWarsMovieCommand);
return "redirect:/movie/" + savedCommand.getId() + "/show";
}
}
Star Wars Movie Model
package example.starwars.models;
import lombok.*;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Data
@Entity
public class StarWarsMovie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String movieName;
@Column(columnDefinition = "DATE")
private Date releaseDate;
private Long boxOffice;
@ManyToOne
private Director director;
@ManyToMany
@JoinTable(name = "starwars_starships",
joinColumns = @JoinColumn(name = "movie_id"),
inverseJoinColumns = @JoinColumn(name = "starship_id"))
private List<Starship> starships = new ArrayList<>();
@ManyToMany
@JoinTable(name = "starwars_characters",
joinColumns = @JoinColumn(name = "movie_id"),
inverseJoinColumns = @JoinColumn(name = "character_id"))
private List<Characters> characters = new ArrayList<>();
}
movieForm
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Star Wars Movie Form</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container-fluid" style="margin-top: 20px">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<form th:object="${movie}" th:action="@{/movie/}">
<input type="hidden" th:field="*{id}"/>
<div class="pannel-group">
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title">Star Wars Movie Information</h1>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-6 form-group">
<label>Movie Name:</label>
<input type="text" class="form-control" th:field="*{movieName}"/>
</div>
<div class="col-md-6 form-group">
<label>Release Date:</label>
<input type="date" class="form-control" th:field="*{releaseDate}"/>
</div>
</div>
<div class="row">
<div class="col-md-3 form-group">
<label>Box Office:</label>
<input type="number" class="form-control" th:field="*{boxOffice}"/>
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
I also put the complete project in Github .