14

I read the following documentation from spring.io and it said By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath however when I put my index.html file under /resources the string index is just rendered. Currently index.html is under webapp and I am using AngularJS.

directory

MvcConfiguration

@Configuration
public class MvcConfig {

    @Bean
    InternalResourceViewResolver viewResolver(){

        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/webapp/");
        resolver.setSuffix(".html");

        return resolver;
    }

}

Restful Service for index page

@RestController
public class IndexController {

    @RequestMapping("/")
    public String index(){
        System.out.println("Looking in the index controller.........");
        return "index";
    }

}

ON my IDE console I do see Looking in the index controller...... printed from IndexController and under network in the Chrome development tools I only see localhost 200.

index.html

<body>

  <!--[if lt IE 7]>
      <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
  <![endif]-->

  <div ng-view></div>

  <div>Angular seed app: v<span app-version></span></div>
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
Mike3355
  • 11,305
  • 24
  • 96
  • 184

2 Answers2

36

Spring Boot docs also says:

Do not use the src/main/webapp directory if your application will be packaged as a jar. Although this directory is a common standard, it will only work with war packaging and it will be silently ignored by most build tools if you generate a jar.

Spring Boot is very opinionated and works best when you do not try to resist defaults. I don't see any reason having your files placed in /src/main/webapp. Just use /src/main/resources/static for your front-end assets. That is most common place.

It will serve these static files from root URI automatically, without need to create any root level Controller. In fact your IndexController would prevent static front-end files to be served from root URI. There is no need to create Controller for static files at all.

Also view resolver is not needed for your app. Your app is just REST API consumed by single page angular application. So your HTML templating is on client. View resolvers are needed if you are doing server side HTML templating (e.g. with Thymeleaf or JSP). So remove that piece also.

Vik David
  • 3,640
  • 4
  • 21
  • 29
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • Yes it said do not put the index page in webapps IF you are putting it in a jar. I made a dummy index.html file put it in `/src/main/resources/static` and the string `index` was still rendered. I do need a RestAPI because I am retrieving information from the `@Service`. – Mike3355 Mar 13 '16 at 13:34
  • because your IndexController is mapped to "/". When you want to serve static HTML files from your root resource, you shouldn't have any Controller mapped to "/". – luboskrnac Mar 13 '16 at 13:40
  • I'm really confused actually when I create a new springboot project I don't have this folder webapp I only have static....where I should place my JSP files ? – Osama Al-Banna Feb 15 '17 at 11:58
  • @luboskrnac would the static folder also be the right place to put dynamically generated web pages? – Arya Oct 24 '17 at 15:35
  • Can you elaborate more on "dynamically generated web pages"? – luboskrnac Oct 24 '17 at 17:57
4
@RestController
public class IndexController {

    @RequestMapping("/")
    public String index(){
        System.out.println("Looking in the index controller.........");
        return "index";
    }

}

Problem is here, you are using @RestController, so in this case, if you write "return 'index';" spring boot covers it as just string answer. You need use @Controller annotation instead.