弹簧控制器Thymeleaf 404

时间:2019-10-29 19:14:40

标签: spring spring-boot spring-mvc

我正在开发一个带有百里香的简单servlet,它可以通过按下按钮来更改文本。

问题是,当我在Spring工具套件4.4中将其作为Spring应用程序运行时,它可以工作,但是当将其编译为servlet并将其上传到tomcat服务器时,会出现此错误: 404 error

代码在这里:

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>

1 个答案:

答案 0 :(得分:2)

请发布您的完整请求网址。如果使用war文件将其部署在Tomcat中,则需要将上下文根添加到url。

默认为:

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