14

I tried to run spring boot application, that will return me the HTML static file on the static folder, the problem was: every time I load the page: 127.0.0.1 I get the String "bakara" and not the HTML file bakara.html. and when I load 127.0.0.1/bakara.html I get the bakara.html file

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>il.mda.ks</groupId>
<artifactId>mdaForm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mdaForm</name>
<description>Demo project for Spring Boot</description>


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>


<dependencies>
    <!-- This is a web application -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Tomcat embedded container -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- JSTL for JSP -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <!-- Need this to compile JSP -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Need this to compile JSP, tomcat-embed-jasper version is not working, 
        no idea why -->
    <dependency>
        <groupId>org.eclipse.jdt.core.compiler</groupId>
        <artifactId>ecj</artifactId>
        <version>4.6.1</version>
        <scope>provided</scope>
    </dependency>

    <!-- Optional, test for static content, bootstrap CSS -->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.7</version>
    </dependency>

</dependencies>

application.properties:

#spring.mvc.view.prefix=/static/
#spring.mvc.view.suffix=.html

spring.mvc.view.prefix=/static
spring.mvc.view.suffix=.html

HomeController.java:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class HomeController {

    @RequestMapping("/")
    @ResponseBody
    public String welcome() {
        return "bakara";
    }
}

project structure:

|── src
│   ├── main
│   │   ├── java
│   │   │   └── il
│   │   │       └── mda
│   │   │           └── ks
│   │   │               └── mdaForm
│   │   │                   ├── BakaraController.java
│   │   │                   ├── HomeController.java
│   │   │                   └── MdaFormApplication.java
│   │   └── resources
│   │       ├── application.properties
│   │       ├── static
│   │       │   ├── assets
│   │       │   ├── bakara.html
│   │       │   ├── succeed.html
│   │       │   └── TokenDenied.html
│   │       └── templates

4 Answers 4

34

@Controller VS @RestController

  • @Controller is used to mark classes as Spring MVC Controller.
  • @RestController is a convenience annotation that does nothing more than adding the @Controller and @ResponseBody annotations.

So in your case just removing the @ResponseBody annotation from the welcome() method in HomeController.java, should be enough to get the desired output.

Also have a look at this Spring Guide displaying how to Serve Web Content with Spring MVC

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

Comments

5

By Default Spring Boot Looks For your html templates in templates folder static folder is for your other files like css and js .Try moving your html files in src/main/resources/templates folder and remove @ResponseBody from your controller method and remove this from your application properties spring.mvc.view.prefix=/static. I hope it will work.

Comments

5

When you use annotation @ResponseBody, you actually tell spring to not try to find a view with the returned name. If you want the html, just remove the annotation from the controller method.

1 Comment

I think you configured your application.properties with the spring.mvc.view.prefix=/static. Have you tried to access the page via 127.0.0.1/static/bakara.html?
2

I had this same problem. There was no way to return an html just by returning a String with the name of the html file. For me, it only worked using ModelAndView, in your case it would look like this:

@RequestMapping("/")
public ModelAndView welcome() {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("bakara");
    return modelAndView;
}

Some people say that problems like this occurs because the jdk version. I use jdk14.

1 Comment

use @Controller but not @RestController

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.