Java tutorial
/* * 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 org.ucll.ip.spring_ip_project.controller; import domain.MaintenanceSystem; import domain.Tank; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; 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 org.springframework.web.servlet.ModelAndView; /** * * @author wouteradriaens */ @Controller @RequestMapping(value = "/tank") public class TankController { @Autowired private MaintenanceSystem system; @RequestMapping(method = { RequestMethod.POST }, value = "/overview") public ModelAndView getTanks(HttpServletRequest req) { String name = req.getParameter("player"); req.setAttribute("player", name); ModelAndView view = new ModelAndView("tanks", "tanks", system.getTanksOfPlayer(name)); view.addObject("games", system.getTotalGamesPlayed(name)); return view; } @RequestMapping(method = RequestMethod.POST, value = "/new") public ModelAndView getNewForm(HttpServletRequest request) { ModelAndView view = new ModelAndView("tankForm", "tank", new Tank()); String name = request.getParameter("player"); request.setAttribute("player", name); view.addObject("name", system.getPlayer(name)); return view; } @RequestMapping(method = RequestMethod.POST, value = "/save") public String saveTank(@ModelAttribute("tank") Tank tank, BindingResult result, HttpServletRequest req) { if (result.hasErrors()) { String name = req.getParameter("player"); req.setAttribute("player", name); return "tankForm"; } String name = req.getParameter("player"); system.addTank(tank); system.getPlayer(name).addTankToPlayer(tank); return "redirect:/player.htm"; } @RequestMapping(value = "/{id}/update", method = RequestMethod.GET) public ModelAndView getEditForm(@PathVariable Long id) { return new ModelAndView("editTankForm", "tank", system.getTank(id)); } @RequestMapping(method = RequestMethod.POST, value = "/edit") public String update(@ModelAttribute("tank") Tank tank, BindingResult result) { if (result.hasErrors()) { return "editTankForm"; } system.updateTank(tank); return "redirect:/player.htm"; } @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET) public ModelAndView getDeleteForm(@PathVariable long id) { return new ModelAndView("deleteTankConfirm", "tank", system.getTank(id)); } @RequestMapping(method = RequestMethod.POST, value = "/{id}/delete") public String delete(@PathVariable long id, HttpServletRequest request) { if (request.getParameter("submit").equals("Sure") || request.getParameter("submit").equals("Zeker")) { system.removeTank(id); return "redirect:/player.htm"; } return "redirect:/player.htm"; } }