0

I have this in com.tuke.doto.Controllers / RootController.java:

package com.tuke.doto.Controllers;

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

@Controller
public class RootController {

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

and I have frontpage.html in main/resources/static (I also tried to move it into main/resources/templates

When I do request on localhost:8080 I see this:

HTTP Status [404] – [Not Found]

Type Status Report

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/8.5.15

and in my console on jetbrains intellij I see this:

javax.servlet.ServletException: Circular view path [error]: would dispatch back to the current handler URL [/error] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

I've also included server.error.whitelabel.enabled=false into main/resources/application.properties

Where is the problem? Isn't that the simplest example so why it doesn't work?

EDIT maybe I should install some dependency to make it work... this is how my build.gradle looks right now:

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-websocket')

    compile("org.springframework.boot:spring-boot-devtools")

    testCompile('org.springframework.boot:spring-boot-starter-test')
}
4
  • suppose you follow this example and see what you have missed in your configurations. Commented Jul 22, 2017 at 1:47
  • I am using gradle @RajithPemabandu so I can't follow your Maven example Commented Jul 22, 2017 at 1:51
  • Hope this would be the helpful example Commented Jul 22, 2017 at 1:54
  • I had same problem, and server.error.whitelabel.enabled=false did not work. But what worked was @EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class}) on the @SpringBootApplication class. Maybe this is because I use Spring Boot 2. This solution works when there is another error that is the problem, but since Spring MVC does not have an error page mapped, it fails with the irrelevant error message. Commented Jan 10, 2019 at 15:20

6 Answers 6

4

IF you are just using static html, you may not need a template library like thyme leaf. Simple return ModelAndViewObject. Place your html in src/main/resources/static

example

@GetMapping("/")
public ModelAndView index() {
    return new ModelAndView("frontpage.html");
}
Sign up to request clarification or add additional context in comments.

Comments

3

There are a few assumptions we have to make with this question. Do you mean src/main/templates? We also have to assume you are using Thymeleaf? Is that dependency in your gradle path?

compile 'org.springframework.boot:spring-boot-starter-thymeleaf'

7 Comments

Yes src/main/resources/static or src/main/resources/templates - none of these works... and I've tried to include thymeleaf as gradle dependency but I still get this error: Circular view path [error]: would dispatch back to the current handler URL [/error] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.) btw I get it twice
Are you using Thymeleaf in the way I proposed? If so, placing the file in src/main/resources/static will never work, because you should only place static content in that directory (JavaScript, Images, etc.). Thymeleaf will resolve files by name in directory src/main/resources/templates assuming in you are even using Thymeleaf.
Yes I am using it exactly as you proposed. Ok I've moved frontpage.html to templates but I still get the same error
Is there anything in frontpage.html that would cause an error? Is it static or does it use layouts or tags? Also what happens if you place error.html into src/main/resources/templates?
it's static.. for now it just says This is an index page... I tried to put error.html in there but still the same error
|
0

Try to adding the following to your configuration class

    @Bean
    public EmbeddedServletContainerFactory factory(){
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
        factory.setContextPath("/myapp");
        return factory;
    }

and the following to application.config

spring.resources.chain.strategy.fixed.enabled=true
spring.resources.chain.strategy.fixed.paths=/**

put .html file inside src/main/resources/templates

And then try to access localhost:8080/myapp/frontpage

Comments

0

If you are trying to simply run an html file, do ensure that the project structure is the following for a Spring Boot app

enter image description here

After following this structure, you will be able to access index.html via launching localhost:8080

Comments

0

For CRA React, below 2 methods can be used

  • Copy contents from CRA build folder to either target/classes/static or target/classes/public

This will automatically map index.html with MVC. You can see this during startup of spring-boot application as Adding welcome page: class path resource [public/index.html] Incase this Method fails, alternatively use below

  • create a ModelAndView object as below
@RequestMapping("/")
public ModelAndView frontend(){
  return new ModelAndView("frontend.html")
}

Comments

0

As I understand your query,please include in controller class

import org.springframework.web.bind.annotation.RequestMapping;

100% will work

1 Comment

Tha sample code in the question already contains that import statement so how does your answer solve the problem?

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.