controller.LightController.java Source code

Java tutorial

Introduction

Here is the source code for controller.LightController.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 controller;

import domain.ColourService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author Jeroen-Lenovo
 */
@Controller
public class LightController {

    @Autowired
    private ColourService colourService;

    @RequestMapping(value = { "light" }, method = RequestMethod.GET)
    public String showHomePage(Model model) {
        model.addAttribute("lamp", new Lamp());
        try {
            model.addAttribute("listLamps", colourService.getAvailableLamps());
        } catch (NullPointerException exception) {
            return "error";
        }
        return "light";
    }

    @RequestMapping(value = { "light" }, method = RequestMethod.POST)
    public String onSubmit(@ModelAttribute Lamp lamp, Model model) {

        colourService.setColour(lamp.getLampId(), lamp.getRood(), lamp.getGeel(), lamp.getBlauw());

        return "light";

    }

}