2

I have created a Spring Boot application with Intellij. It creates a sample web application with a controller and an empty html page.

But problem is this sample application returns "Whitelabel Error Page" when I go to "http://localhost:8080/info/aa". I think It cannot find info.html file. Is there any way to fix this?

Here is sample controller;

@Controller
@RequestMapping("/info")
public class InfoController {

    @RequestMapping("/aa")
    public String getServerInfo(Map<String, Object> model){

        model.put("message", "server info");
        return "info";

    }

}

My project directory;

enter image description here

EDIT: My html file info.html;

<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html lang="en">
<head>

</head>
<body>

</body>

</html>

EDIT 2: I update my html directory to templates/info.html

enter image description here

DemoApplication class;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

And application.properties file is empty.

6
  • Please, post also the code from DemoApplication class Commented Apr 27, 2018 at 8:06
  • Also, what is the exception that you are getting? Spring should log the stacktrace if you get Whitelabel Error Page in your browser. Commented Apr 27, 2018 at 8:10
  • @SergiiBishyr I am getting "There was an unexpected error (type=Not Found, status=404). No message available" Commented Apr 27, 2018 at 8:11
  • When you start the application, can you see in the log the mapping for 'info/aa'? Should be something like this Mapped "{[/info/aa]}" onto public java.lang.String your.package.InfoController. getServerInfo(java.util.Map<String, Object> model) Commented Apr 27, 2018 at 8:14
  • @SergiiBishyr Yes there is a log "Mapped "{[/info/aa]}" onto public java.lang.String com.example.demo.controller.InfoController.getServerInfo(java.util.Map<java.lang.String, java.lang.Object>)" Commented Apr 27, 2018 at 8:19

3 Answers 3

2

If your info.html is actually a template file then it obviously shouldn't be in a static content, it should be in a templates folder, as its not static.

Here is an example: https://spring.io/guides/gs/serving-web-content/

Under static folder you have all static content, which wouldn't change when you serve it, for example, images, css, js scripts and so on.

Under templates folder you store templates from which spring + its template ending(for example thymeleaf) will generate actual html, using your model, which will be served to a client.

Also if you have a subfolder in templates like templates/info/info.html then you should return info/info as a template name from your controller, not just info.

However if you are using jsp then its something different, and you shouldn't use template folder, you should create webapp/WEB-INF/jsp/ folders and put it there and you should name your template info.jsp then, instead of info.html

Example here: https://www.mkyong.com/spring-boot/spring-boot-hello-world-example-jsp/

Sign up to request clarification or add additional context in comments.

7 Comments

I put info.html file into templates folder and I am still getting same error page
@hellzone what is your path to the file? if its templates/info/info.html then in controller you should retur "info/info.html" instead of "info.html"
Its templates/info.html and returning "info" from controller as above.
@hellzone sorry my mistake, should have returned "info/info" without .html if it still doesn't work, can you please update your question with your new structure
@hellzone I updated my answer regarding that you are using jsp
|
1

You don't have to put your info.html into static/info folder inside your resources, instead put it into templates. The @RequestMapping on top of your InfoController class does not tell where to find the requested html files, it is just a mapping for your request.

If you want to have separates folder for different html pages you can have the info folder inside of your templates folder, but you still have to refer is in your controller as info/info:

@RequestMapping("/aa")
public String getServerInfo(Map<String, Object> model){

    model.put("message", "server info");
    return "info/info";

}

2 Comments

I put it into templates folder and I am still getting same error page.
@hellzone Could you update your project directory screenshot with the new structure?
0

If you want to use JSP (like I see you want) first of all move the html files to the folder:

/src/java/webapp/

and change the extension to .jsp. Then you will have your view file in:

/src/java/webapp/info/info.jsp

Secondly you have to add configuration for view resolving:

@Configuration
@EnableWebMvc
public class WebConfiguration {
    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/templates/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

And in the controller rember to return path of the file - you have configured head to .../templates and you have to return info/info:

@RequestMapping("/info")
public String info() {
    return "info/info";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.