org.ucll.ip.spring_ip_project.controller.PlayerController.java Source code

Java tutorial

Introduction

Here is the source code for org.ucll.ip.spring_ip_project.controller.PlayerController.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 org.ucll.ip.spring_ip_project.controller;

import domain.MaintenanceSystem;
import domain.Player;
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 = "/player")
public class PlayerController {

    @Autowired
    private MaintenanceSystem system;

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView getPlayers() {
        return new ModelAndView("players", "players", system.getPlayers());
    }

    @RequestMapping(method = RequestMethod.POST, value = "edit")
    public String editPlayer(@ModelAttribute("player") Player player, BindingResult result) {
        if (result.hasErrors()) {
            return "editPlayerForm";
        }
        system.updatePlayer(player);
        return "redirect:/player.htm";
    }

    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public ModelAndView getPlayerForm() {
        return new ModelAndView("playerForm", "player", new Player());
    }

    @RequestMapping(method = RequestMethod.POST, value = "save")
    public String savePlayer(@ModelAttribute("player") Player player, BindingResult result) {
        if (result.hasErrors()) {
            return "playerForm";
        }
        system.addPlayer(player);
        return "redirect:/player.htm";
    }

    @RequestMapping(value = "/{name}/update", method = RequestMethod.GET)
    public ModelAndView getEditForm(@PathVariable String name) {
        return new ModelAndView("editPlayerForm", "player", system.getPlayer(name));
    }

    @RequestMapping(value = "/{name}/delete", method = RequestMethod.GET)
    public ModelAndView deleteConfirmation(@PathVariable String name) {
        return new ModelAndView("deletePlayerConfirm", "player", system.getPlayer(name));
    }

    @RequestMapping(value = "/{name}/delete", method = RequestMethod.POST)
    public String deletePlayer(@PathVariable String name, HttpServletRequest request) {
        if (request.getParameter("submit").equals("Sure") || request.getParameter("submit").equals("Zeker")) {
            system.removePlayer(name);
            return "redirect:/player.htm";
        }
        return "redirect:/player.htm";
    }
}