亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java Spring Boot:如何將我的應用程序根目錄(“ /”)映射到index.html?

Java Spring Boot:如何將我的應用程序根目錄(“ /”)映射到index.html?

繁花不似錦 2019-11-08 14:21:18
我是Java和Spring的新手。如何將我的應用程序根目錄映射http://localhost:8080/到靜態目錄index.html?如果我導航到http://localhost:8080/index.html它的作品很好。我的應用程序結構為:rs我的config\WebConfig.java樣子是這樣的:@Configuration@EnableWebMvc@ComponentScanpublic class WebConfig extends WebMvcConfigurerAdapter {    @Override    public void addResourceHandlers(ResourceHandlerRegistry registry) {        registry.addResourceHandler("/**").addResourceLocations("/");        }}我嘗試添加,registry.addResourceHandler("/").addResourceLocations("/index.html");但是失敗。
查看完整描述

3 回答

?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

Dave Syer的答案的示例:


import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


@Configuration

public class MyWebMvcConfig {


    @Bean

    public WebMvcConfigurerAdapter forwardToIndex() {

        return new WebMvcConfigurerAdapter() {

            @Override

            public void addViewControllers(ViewControllerRegistry registry) {

                // forward requests to /admin and /user to their index.html

                registry.addViewController("/admin").setViewName(

                        "forward:/admin/index.html");

                registry.addViewController("/user").setViewName(

                        "forward:/user/index.html");

            }

        };

    }


}


查看完整回答
反對 回復 2019-11-08
?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

首先在資源下創建公用文件夾,然后創建index.html文件。使用WebMvcConfigurer而不是WebMvcConfigurerAdapter。


import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration

public class WebAppConfig implements WebMvcConfigurer {


    @Override

    public void addViewControllers(ViewControllerRegistry registry) {

        registry.addViewController("/").setViewName("forward:/index.html");

    }


}


查看完整回答
反對 回復 2019-11-08
  • 3 回答
  • 0 關注
  • 1296 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號