简体   繁体   中英

Calling default web page of application automatically in Spring Boot

I am a Spring Boot newbie and one thing bothers me: if I have a simple Spring Boot application like this:

package hello;

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

@SpringBootApplication
public class Application {

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

}

I can call the default web page of the application from a browser by using URL:

http://localhost:8080/greeting

To return a page called greeting.html which is specified by the controller:

@RequestMapping("/greeting")
    public String greeting() Model model) {            
        return "greeting";
    } 

Is there a way to have Spring Boot automatically open greeting.html in the browser? Can I tell Spring boot which controller method I want it to run when the project starts?

Not sure if there is a setting you can set for this case but what you can do is using redirection.

Just map "/" and redirect to "/greeting" :

@RequestMapping("/")
public String index(Model model) {            
    return "redirect: greeting";
} 

But if you want that the browsers open any page after lunching the application you should lookup if your IDE can do that.

Just map your greeting page to the root:

@RequestMapping("/")
public String greeting() Model model) {            
    return "greeting";
} 

Then when you call http://localhost:8080/ you will be redirected to the greeting page as a default page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM