1

I try to load the index.html using Spring. But when I load it in localhost instead of getting my HTML page I just get a string that says index.

My Resource structure

I try to load the index with the IndexController

    @Controller
public class IndexController {
    @RequestMapping("/")
    @ResponseBody
    public String welcome() {
        return "index";
    }
}
ceng
  • 128
  • 3
  • 17
TomasB
  • 47
  • 1
  • 9

3 Answers3

1

The issue is with the @ResponseBody annotation on the method. It tells Spring that the return value of the method should be serialized and sent to the client, rather than Spring treating the string as a view name.

ThisIsNoZaku
  • 2,213
  • 2
  • 26
  • 37
  • I removed the @ResponseBody and now got: There was an unexpected error (type=Internal Server Error, status=500). Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers error – TomasB Apr 06 '20 at 17:15
1

You have to make 2 changes.

  1. Remove @ResponseBody as it will send response in body instead of a view page.

  2. There is a typo in folder name there. Rename your folder name with templates instead of templetes.

Alien
  • 15,141
  • 6
  • 37
  • 57
-1

Try with RedirectView :

@Controller
public class IndexController {
    @RequestMapping("/")
    @ResponseBody
    public RedirectView welcome() {
        //@FIXME load url from propertie
        return new RedirectView("http://localhost/index.html");
    }
}
  • I tried the above and now when I connect to localhost it redirects to http://localhost/index.html and I get an Unable to connect from the browser. – TomasB Apr 06 '20 at 17:09
  • replace localhost/index.html with your working index, i dont know your port and the frontpage tecnology. – Alejandro Venegas Apr 06 '20 at 17:20