0

I have a spring boot basic application

package com.meenakshi.fileupload;

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



@SpringBootApplication
public class Example {



    public static void main(String[] args) throws Exception {
        SpringApplication.run(Example.class, args);
    }

}

My controller is

package com.meenakshi.fileupload.controller;

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



@Controller
public class IndexController {

    @RequestMapping("/")
    public String index() {
        System.out.print("REDIRECTED BY MEENAKSHI");

        return "index";
    }

}

My index.html is a basic html file in src/main/resources/public folder

My pom.xml is

<?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.example</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

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



    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <!-- Additional lines to be added here... -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <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>


    </dependencies>


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

</project>

When I do localhost:8080 I expect to redirect to index.html but I am getting Whitelabel Error Page

When I add thymeleaf dependency and add xmlns:th="http://www.thymeleaf.org" in .html it works

How to resolve it? Also what is the default view resolver in Spring Boot? Do I compulsorily need to use thymeleaf?

Also, do I need to add something to application.properties

1 Answer 1

1

There are couple of ways you can expose your index.html.

The way I like the most is doing the following:

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

}

It will automatically grab your index.html and create a default view controller to serve it.

Another way is doing like you did but returning ModelAndView like the following but it suit a Thymeleaf/JSP more then a SPA:

@Controller
@RequestMapping("/")
public class DefaultController {

    @GetMapping("/")
    public ModelAndView index() {
        IndexModel indexModel = new IndexModel();
        return new ModelAndView("index", "index", indexModel);
    }

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

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.