繁体   English   中英

白标签错误页面:Spring 引导应用程序与 Eclipse

[英]Whitelabel Error Page : Spring Boot Application with Eclipse

我正在尝试在 eclipse 中使用 spring 引导和 java 构建应用程序。

到目前为止我已经尝试过:

主页控制器.class

@Controller
@ComponentScan(basePackages = {"project.tool.frontend"})
public class HomeController {

    @GetMapping("/home")     
    public String home(){
        System.out.println("open page");
        return "home";
    }
}

主页.html

<!DOCTYPE HTML>
<html>
<head>
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>  
<form action="/home" method="get">
        First name: <input type="text" name="fname"><br> Last
        name: <input type="text" name="lname"><br> <input
            type="submit" value="Submit">
    </form> 
</body>
</html>

应用.class

@SpringBootApplication
public class Application{

public static void main(String agrs[])
{
     SpringApplication.run(Application.class);
}
}

当我运行此应用程序时,我收到“Whitelabel ErrorPage”错误。 我还在我的 application.properties 中启用了 whitelabel 错误页面。

还有一个问题:当我在我的 HomeController 中使用@GetMapping("/home")并在我的 html 文件中使用<form action="/home"时,它将通过"/home" get method进行映射,对吗?

谢谢你们。

  1. 作为 static 资源,您的 home.html 应该位于 resources/public/home.html(或 /static/)。 它在那里吗?
  2. 您的应用程序应告知您的 controller 位于哪个 package 中,以便获取

添加 ComponentScan 有帮助吗?

@SpringBootApplication
@ComponentScan(basePackages = { "com.acme.controllers" })
public class Application{
}

如果您还没有配置任何视图解析器,请尝试返回“/views/home.html”,而不仅仅是“home”。 将“/views/home.html”更改为文件的路径。

我建议使用 Thymeleaf 作为您的视图解析器。 您可以将 home.html 放在 src/main/resources 文件夹中进行识别。

当您使用.html 扩展时,我建议您使用 thymeleaf 作为视图解析器。 使用 thymeleaf HTML:

<form th:action="@{/home}" th:method="get"
        ...
        ...
        //Rest of your code goes here.
</form>

使用@ComponentScan 注解:

@SpringBootApplication
@ComponentScan(basePackages = { "<full package name where controller exists>" })
public class Application{

    public static void main(String agrs[]){
         SpringApplication.run(Application.class);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM