DEV Community

V I N O T H
V I N O T H

Posted on

background color change using spring boot mini project

1) File --> New --> Spring Starter Project
2) Name --> Calculator
3) Select Maven as Type
4) Click Next
5) Add Dependencies: DevTools, Web, Thymeleaf
6) Click Next, Finish
7) Go to src/main/java --> Create a package called com.example.demo.controller
8) Now, right click the newly created package.

package com.example.demo.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class Color_controller {

    @GetMapping("/")
    public String change_color(@RequestParam(defaultValue = "blue") String colors, Model model)
    {
        model.addAttribute("selectedColor", colors);
        return "color"; 
    }

}
Enter fullscreen mode Exit fullscreen mode
<!DOCTYPE html>
<html xmlns:th="http://www.thymelear.org">
    <head>
        <title> Color Picker </title>
    </head>
<body th:style="'background-color:' + ${selectedColor}">
        <form action="/" method="get">
            <input type="color" th:value='${selectedColor}' name="colors">
            <input type="submit" value="Change Color">
        </form>

    </body>
</html>
Enter fullscreen mode Exit fullscreen mode

OUTPUT

Image description

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.