【问题标题】:Calling default web page of application automatically in Spring Boot在 Spring Boot 中自动调用应用程序的默认网页
【发布时间】:2016-07-11 23:09:07
【问题描述】:

我是一个 Spring Boot 新手,有一件事让我很困扰:如果我有一个像这样的简单 Spring Boot 应用程序:

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);
    }

}

我可以使用 URL 从浏览器调用应用程序的默认网页:

http://localhost:8080/greeting

返回由控制器指定的名为greeting.html 的页面:

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

有没有办法让 Spring Boot 在浏览器中自动打开greeting.html?项目启动时,我可以告诉 Spring boot 我希望它运行哪个控制器方法吗?

【问题讨论】:

  • 这更像是一个 IDE 配置问题而不是 Spring 启动问题,你应该用你正在使用的 IDE 标记它
  • 您是否知道相关的 NetBeans 设置?
  • 你问的是如何设置欢迎页面? “在浏览器中自动打开greeting.html”不是服务器端框架会做的事情。
  • 是的,这是真的,但如果我运行其他 Web 应用程序并在 web.xml 中指定欢迎页面,我知道该页面将加载。所以我想在这里做同样的事情。
  • 感谢受访者。我明天会回到这个。

标签: java html spring-boot


【解决方案1】:

不确定是否可以为这种情况设置设置,但可以做的是使用重定向。

只需映射"/" 并重定向到"/greeting"

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

但是,如果您希望浏览器在启动应用程序后打开任何页面,您应该查找您的 IDE 是否可以这样做。

【讨论】:

    【解决方案2】:

    只需将您的问候页面映射到根目录:

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

    然后当您拨打http://localhost:8080/ 时,您将被重定向到作为默认页面的问候页面。

    【讨论】:

      猜你喜欢
      • 2015-05-02
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      • 2019-06-07
      • 2015-06-06
      • 2017-01-02
      • 2016-01-21
      • 2018-07-28
      相关资源
      最近更新 更多