0

I am having some issues with a controller in spring boot. Which should return an index.html file but what it doing is, returning the string "index".

Here are the things you want to look for,

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>com.earworm</groupId>
    <artifactId>earworm</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project> 

The controller named LoginController.java

package com.earwormproject.earwormcontrollers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    @ResponseBody
    public String createLoginForm(){
        return "index";
    }

    @RequestMapping(value = "/profile", method = RequestMethod.GET)
    @ResponseBody
    public String addDataToLoginForm() {
        return "profile";
    }
}

Here is the main class,

package com.earwormproject;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

The file structure,

enter image description here

Earworm is the main class.So far I know controller checks for the html templates in the src/main/resources/templates, I have put the index.html in templates, did not work.

When I am going to http://localhost:8080/index it is showing me the string index instead of the index.html page. Like this,

enter image description here

I have gone through almost all the similar questions here in SO, tried all those but did not work in this case. Some clue will be of great help. Thank you.

4
  • What's your viewresolver configuration? Commented Jul 14, 2017 at 21:33
  • 2
    "@ResponseBody public String createLoginForm(){ return "index"; }" It can not return anything else but that Commented Jul 15, 2017 at 1:37
  • What's in your index.html? Maybe one word: "index"? :-) Commented Jul 17, 2017 at 18:06
  • Also, why is there no application.properties file in your resources folder? Have you deleted it? Is it a valid Spring Boot application? How did you create it? Commented Jul 17, 2017 at 18:13

5 Answers 5

1

Remove response body from you controller and try again please.

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

1 Comment

I tried too, but it throws an exception saying that "NumberFormatException" for input string "index".
1

Set the content type in the header, delete the annotation @ResponseBody and change the "index" to "index.html"

 @RequestMapping(value = "/index", method = RequestMethod.GET)
 public String createLoginForm(HttpServletResponse response){
   response.setHeader("Content-Type","text/html");
    return "index";
}

Also the classpath of the static resources(your files from templates) must be in the next locations: /static or /public or /resources or /META-INF/resources because only this locations the spring know to bind by default

3 Comments

There is no need to add .html. It is the logical view, which has to be resolved by view resolver.
Without .html will return the string...with Spring MVC maybe there is no need to add the html suffix
Thanks for your answer, but I didn`t get the thing of static resources being in the next locations. As far I know it looks in the src/main/resources/templates by default. Confused a bit, :-)
1

I met the same question,but my problem is JDK9. When I try to use JDK1.8, everything become fine.

Comments

1

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:

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

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

Comments

0

There are three steps to solve this
1. Remove @RequestBody annotation as we are returning a HTML file.
2. Add a dependency from maven repository corresponding to the tomcat server you are using.
https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper
3. Close all html tags. Thymleaf expects the html file to be a valid xml file.

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.