com.swcguild.addressbookmvc.controller.HomeController.java Source code

Java tutorial

Introduction

Here is the source code for com.swcguild.addressbookmvc.controller.HomeController.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.addressbookmvc.controller;
//
//import com.swcguild.contactlistmvc.dao.ContactDao;
//import com.swcguild.contactlistmvc.dto.Contact;
//import java.util.List;
//import javax.inject.Inject;
//import org.springframework.http.HttpStatus;
//import org.springframework.stereotype.Controller;
//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 HomeController {
//    
//    @RequestMapping(value={"/", "/home"}, method=RequestMethod.GET)
//    public String displayHomePage() {
//        return "home";
//    }
//    
//}

/*
 * 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.addressbookmvc.controller;

import com.swcguild.contactlistmvc.dao.ContactDao;
import com.swcguild.contactlistmvc.dto.Contact;
import java.util.List;
import javax.inject.Inject;
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 HomeController {

    //we are injecting a DAO into a controller; we already declared this bean in spring-persistence.xml
    private ContactDao dao;

    @Inject //another way is: @Autowired
    public HomeController(ContactDao dao) {
        this.dao = dao;
    }
    //this is the end of the injecction

    @RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET)
    public String displayHomePage() {
        return "home";
    }

    //adding REST approach; no logical view; pure data transfer
    //I think that's what we are doing here
    @RequestMapping(value = "/contact", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    //indicates that the content gets sent to client
    @ResponseBody
    public Contact createContact(@RequestBody Contact contact) {
        dao.addContact(contact);
        return contact;
    }

    @RequestMapping(value = "/contacts", method = RequestMethod.GET)
    @ResponseBody
    public List<Contact> getAllContacts() {
        return dao.getAllContacts();
    }

    @RequestMapping(value = "/contact/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Contact getContact(@PathVariable("id") int id) {
        return dao.getContactById(id);
    }

    @RequestMapping(value = "/contact/{id}", method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void putContact(@PathVariable("id") int id, @RequestBody Contact contact) {
        contact.setContactId(id);
        dao.updateContact(contact);
    }

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

}