Skip to content

Instantly share code, notes, and snippets.

@luismoramedina
Created May 7, 2015 09:44
Show Gist options
  • Save luismoramedina/5cf47dc10703b33c234d to your computer and use it in GitHub Desktop.
Save luismoramedina/5cf47dc10703b33c234d to your computer and use it in GitHub Desktop.
crud + spring data + controller
package com.isb.phoenix.demo.hotels.controller;
import com.isb.phoenix.demo.hotels.data.entity.Hotel;
import com.isb.phoenix.demo.hotels.data.repository.HotelsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
/**
* @author luismoramedina
*/
@Controller
public class HotelController {
@Autowired
HotelsRepository hotelsRepository;
@RequestMapping("/hotels")
public String getHotels(ModelMap model) {
List<Hotel> all = hotelsRepository.findAll();
System.out.println("all = " + all);
model.addAttribute("hotels", all);
return "hotels";
}
@RequestMapping(value = "/new-hotel", method = RequestMethod.POST)
public String createHotel(@ModelAttribute Hotel hotel, ModelMap modelMap) {
System.out.println("hotel = " + hotel);
hotelsRepository.save(hotel);
modelMap.put("hotel", hotel);
return "hotel";
}
@RequestMapping(value = "/new-hotel", method = RequestMethod.GET)
public String createHotel() {
return "new-hotel";
}
@RequestMapping(value = "/hotel/{id}" )
public String getHotel(@PathVariable String id, ModelMap modelMap) {
modelMap.put("hotel", hotelsRepository.getOne(id));
return "hotel";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment