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>
4
  • So what exactly you want to do here ? had you initialize your app on index page ( ng-app ) ? Commented Mar 13, 2016 at 9:26
  • Try this resolver.setPrefix("/"); since src/main/webapp is already in your classpath. Commented Mar 13, 2016 at 12:31
  • @SanjayRawat, did not work... Commented Mar 13, 2016 at 13:38
  • @ojuskulkarni the app is configured correctly. index.html is not even being rendered. If there is an AngularJS issue I'll address that once my index page is being rendered Commented Mar 13, 2016 at 13:47

2 Answers 2

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.

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

5 Comments

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.
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 "/".
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 ?
@luboskrnac would the static folder also be the right place to put dynamically generated web pages?
Can you elaborate more on "dynamically generated web pages"?
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.

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.