0

I am trying to build an application with spring boot and java in eclipse.

What I have tried so far:

HomeController.class

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

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

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>

Application.class

@SpringBootApplication
public class Application{

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

When I run this Application I get the "Whitelabel ErrorPage" Error. I also enabled the whitelabel errorpage in my application.properties .

And a question for unserstandig: When I use @GetMapping("/home") in my HomeController and the <form action="/home" in my html file it will be mapped through the "/home" get method right?

Thank you all.

flowers1234
  • 339
  • 2
  • 9
  • 21

4 Answers4

1
  1. As a static resource, your home.html should be at resources/public/home.html (or /static/). Is it there?
  2. Your Application should tell in which package your controller is located, so it gets picked up

Does adding a ComponentScan help?

@SpringBootApplication
@ComponentScan(basePackages = { "com.acme.controllers" })
public class Application{
}
Simon
  • 2,994
  • 3
  • 28
  • 37
  • thanks for your answer, but it is still not working – flowers1234 Nov 20 '19 at 12:36
  • 1. When the application is starting, do you see a log entry that Spring is picking up your controller? (something about mapping /home to HomeController). 2. Could you tell use the location of home.html relative to your project root? 3. What does your log-file / console output when calling /home? – Simon Nov 20 '19 at 12:49
  • 1. Yes I now what you mean, but no there is no mapping in the console. The html file is located in the /frontend/src/main/resources/templates/home.html – flowers1234 Nov 20 '19 at 17:58
  • edited my code.^^ It's working. Can you tell me how I can see the "mapped /home" in my console in eclipse? – flowers1234 Nov 20 '19 at 18:32
  • @flowers1234, I won't be at my PC until this evening. Does this here help? https://stackoverflow.com/questions/53327836/controller-mapping-are-not-logging-at-startup – Simon Nov 21 '19 at 07:49
  • got it ! so i just have to use the logger to get the messages in the console? – flowers1234 Nov 22 '19 at 09:42
  • Yes, setting up some sort of logging is a good idea. You can configure the logger to just write to the console (which is good for debugging, but not for production). If you google for "Spring" and "logback", you should find plenty of information. (And please don't forget to mark the correct answer, it'll help future users) – Simon Nov 22 '19 at 12:33
0

If you have not configured any view resolver, try returning "/views/home.html" instead of just "home". Change "/views/home.html" to whatever be the path of the file.

Varun Mukundhan
  • 260
  • 2
  • 10
0

I suggest using Thymeleaf as your viewresolver. You can place the home.html in src/main/resources folder to be recognized.

tbotalla
  • 119
  • 2
  • 10
0

As you are using .html extension then I suggest you to use the thymeleaf as a view resolver. Using thymeleaf HTML:

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

Use @ComponentScan annotation:

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

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