com.swcguild.bluraymvc.controller.DisplayController.java Source code

Java tutorial

Introduction

Here is the source code for com.swcguild.bluraymvc.controller.DisplayController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.swcguild.bluraymvc.controller;

import com.swcguild.bluraymvc.dao.BluRayDao;
import com.swcguild.bluraymvc.dto.Movie;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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.ResponseStatus;

/**
 *
 * @author Christopher Becker <beckerc@umich.edu>
 */

@Controller
public class DisplayController {

    //used here because the @RequestMappings refer to dao
    //we are injecting a DAO into a controller; this bean was declared in spring-persistence.xml
    //BluRayDao refers to the name of the DAO interface
    private BluRayDao dao;

    //from one blog post @Inject and @Autowired are identical; daughter annotations the same?
    //treatment of ambiguous beans the same?
    //I don't know if @Named is used with @Autowired or not
    @Inject //another way is: @Autowired; I think this matches the bean def in spring-persistence.xml
    @Named(value = "movieDao")
    //this is a constructor injection
    public DisplayController(BluRayDao dao) {
        this.dao = dao;
    }

    //view component "display", endpoint "/display", and method do not have to match.
    @RequestMapping(value = "/display", method = RequestMethod.GET)
    public String displayDisplayPage() {
        return "display";
    }
    //These method names below are idiosyncratic. They refer back to the methods in the DAO by name

    //I think we are adding REST approach; no logical view; pure data transfer
    @RequestMapping(value = "/movie", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    //indicates that the content gets sent to client; not returninng a jsp destination but an object
    //that's what @ResponseBody indicates
    @ResponseBody
    public Movie createMovie(@RequestBody Movie movie) {
        dao.createMovie(movie);
        return movie;
    }

    @RequestMapping(value = "/movies", method = RequestMethod.GET)
    @ResponseBody
    public List<Movie> getAllMovies() {
        return dao.getAllMovies();
    }

    @RequestMapping(value = "/movie/{id}", method = RequestMethod.GET)
    //@ResponseBody causes Spring to look at data returned from RequestMapping and try to return it in appropriate format; not clear; it invokes Jackson here? But I don't think there is a JSON request here
    @ResponseBody
    public Movie getMovie(@PathVariable("id") int id) {
        return dao.getMovieById(id);
    }

    @RequestMapping(value = "/movie/{id}", method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void putMovie(@PathVariable("id") int id, @RequestBody Movie movie) {
        movie.setMovieId(id);
        dao.updateMovie(movie);
    }

    @RequestMapping(value = "/movie/{id}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deleteMovie(@PathVariable("id") int id) {
        dao.removeMovie(id);
    }

}