Add a view to show movies of a given user (or all movies if no user is specified).
This commit is contained in:
parent
b1dd0dd0bb
commit
5284966d4a
6
pom.xml
6
pom.xml
@ -37,6 +37,12 @@
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -1,11 +1,17 @@
|
||||
package com.camillepradel.movierecommender.controller;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.camillepradel.movierecommender.model.Genre;
|
||||
import com.camillepradel.movierecommender.model.Movie;
|
||||
|
||||
@Controller
|
||||
public class MainController {
|
||||
String message = "Welcome to Spring MVC!";
|
||||
@ -20,4 +26,26 @@ public class MainController {
|
||||
mv.addObject("name", name);
|
||||
return mv;
|
||||
}
|
||||
|
||||
@RequestMapping("/movies")
|
||||
public ModelAndView showMovies(
|
||||
@RequestParam(value = "user_id", required = false) Integer userId) {
|
||||
System.out.println("show Movies of user " + userId);
|
||||
|
||||
// TODO: write query to retrieve all movies from DB or all movies rated by user with id userId,
|
||||
// depending on whether or not a value was given for userId
|
||||
List<Movie> movies = new LinkedList<Movie>();
|
||||
Genre genre0 = new Genre(0, "genre0");
|
||||
Genre genre1 = new Genre(1, "genre1");
|
||||
Genre genre2 = new Genre(2, "genre2");
|
||||
movies.add(new Movie(0, "Titre 0", Arrays.asList(new Genre[] {genre0, genre1})));
|
||||
movies.add(new Movie(1, "Titre 1", Arrays.asList(new Genre[] {genre0, genre2})));
|
||||
movies.add(new Movie(2, "Titre 2", Arrays.asList(new Genre[] {genre1})));
|
||||
movies.add(new Movie(3, "Titre 3", Arrays.asList(new Genre[] {genre0, genre1, genre2})));
|
||||
|
||||
ModelAndView mv = new ModelAndView("movies");
|
||||
mv.addObject("userId", userId);
|
||||
mv.addObject("movies", movies);
|
||||
return mv;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,20 @@
|
||||
package com.camillepradel.movierecommender.model;
|
||||
|
||||
public class Genre {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Genre(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.camillepradel.movierecommender.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Movie {
|
||||
|
||||
private int id;
|
||||
private String title;
|
||||
private List<Genre> genres;
|
||||
|
||||
public Movie(int id, String title, List<Genre> genres) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.genres = genres;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return this.title;
|
||||
}
|
||||
|
||||
public List<Genre> getGenres() {
|
||||
return this.genres;
|
||||
}
|
||||
}
|
33
src/main/webapp/WEB-INF/views/movies.jsp
Normal file
33
src/main/webapp/WEB-INF/views/movies.jsp
Normal file
@ -0,0 +1,33 @@
|
||||
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Films</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>
|
||||
<c:choose>
|
||||
<c:when test="${userId==null}">
|
||||
Tous les films
|
||||
</c:when>
|
||||
<c:otherwise>
|
||||
Films de l'utilisateur ${userId}
|
||||
</c:otherwise>
|
||||
</c:choose>
|
||||
</h1>
|
||||
<ul>
|
||||
<c:forEach items="${movies}" var="movie">
|
||||
<li>
|
||||
${movie.title}
|
||||
<ul>
|
||||
<c:forEach items="${movie.genres}" var="genre">
|
||||
<li>
|
||||
${genre.name}
|
||||
</li>
|
||||
</c:forEach>
|
||||
</ul>
|
||||
</li>
|
||||
</c:forEach>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user