1. Overview

Every application should require its default landing page or we can say default index or welcome page. The user will land to default index page when the user types the only domain name. Spring boot provides very easy ways to default index page.

Solution 1: index.html page inside public

create an index.html page inside resources/public/index.html

Solution 2: index.html page inside static

create an index.html page inside resources/static/index.html

Solution 3: Using controller Mapping

Create a controller with@RequestMapping("/") which will return view name but for this requires any view configuration. Here is spring boot jsp example

package com.javadeveloperzone.controller;

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


/**
 * Created by Java Developer Zone on 19-07-2017.
 */
@Controller
public class IndexController {

    @RequestMapping("/")
    public String index() {
        return "Spring Boot Example";
    }
}

2. Example:

In this example, we have createdindex.html page inside resources/public/index.html page.

Spring boot index page example

Spring boot index page example

 

2.1 pom.xml

<?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>spring-boot-example</groupId>
    <artifactId>spring-boot-index-page</artifactId>
    <description>This is example of spring boot index page or default landing page</description>
    <version>1.0-SNAPSHOT</version>
    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.2 index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Spring boot index page example</title>
</head>
<body>
    This is spring boot index page available inside resources/static/index.html
</body>
</html>

2.3 SpringBootConfig

package com.javadeveloperzone;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * Created by JavaDeveloperZone on 19-07-2017.
 */

@SpringBootApplication
@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootConfig.class, args);            // it wil start application
    }
}

2.4 Output:

Now type,http://localhost:8080 It will land to default index or landing or welcome page.

 

Spring boot index page example - output

Spring boot index page example – output

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *