Joanmi
Joanmi

Reputation: 442

Spring Controller Thymeleaf 404

I am working on a simple servlet with thymeleaf that changes a text by pressing a button.

The problem is that when I run it as a Spring application in the Spring tool suite 4.4, it works, but when compiling it as a servlet and uploading it to a tomcat server, this error appears: 404 error

Here the code:

MainController.java

 package com.example.demo;

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

    @Controller
    public class MainController {


    @GetMapping("/saludo")
    public String saludo(Model model) {
    model.addAttribute("saludo", "bondia");
    return "index"; 
    }

    @GetMapping("/nosaludo")
    public String nosaludo(Model model) {
    model.addAttribute("saludo", "no te salud");
    return "index"; 
    }

    @GetMapping("/despido")
    public String despido(Model model) {
    model.addAttribute("saludo", "Adeu");
    return "index"; 
    }
    }

ServletInicializer.java

package com.example.demo;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(CreantServletApplication.class);
    }

}

index.html

<html xmlns:th="http://www.thymeleaf.org">
    <head>
    </head>
    <body>
            <form action="./saludo">
                <input type="submit" value="Saludar">
            </form>
            <form action="./nosaludo">
                <input type="submit" value="No saludar">
            </form>
            <form action="./despido">
                <input type="submit" value="Despedirse">
            </form>
            <h1 th:text="${saludo}">Text que es modificara</h1>
    </body>
</html>

Upvotes: 0

Views: 209

Answers (1)

Marc Stroebel
Marc Stroebel

Reputation: 2357

Please post your complete request url. If you deploy it in Tomcat using a war file you need to add context root to the url.

Defaults to:

http://<host><port>/<war-name-without-file-type>/saludo

Upvotes: 2

Related Questions