安卓 java 映射目录_Java Spring Boot:如何将我的应用程序根目录(“/”)映射到index.html?...

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

我是Java和Spring的新手。如何将我的应用程序根registry.addResourceHandler("/").addResourceLocations("/index.html");映射到静态index.html?如果我导航到http://localhost:8080/index.html它的工作正常。

我的app结构是:

bMEaw.png

我的registry.addResourceHandler("/").addResourceLocations("/index.html");看起来像这样:

@Configuration

@EnableWebMvc

@ComponentScan

public class WebConfig extends WebMvcConfigurerAdapter {

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("/**").addResourceLocations("/");

}

}

我试图添加registry.addResourceHandler("/").addResourceLocations("/index.html");但它失败了。

7个解决方案

128 votes

如果您没有使用@EnableWebMvc注释,它可以开箱即用。 当你这样做时,你可以在WebMvcAutoConfiguration中关闭Spring Boot为你做的所有事情。你可以删除那个注释,或者你可以添加回你关闭的视图控制器:

@Override

public void addViewControllers(ViewControllerRegistry registry) {

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

}

Dave Syer answered 2019-07-09T13:34:12Z

40 votes

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

}

};

}

}

justin answered 2019-07-09T13:34:39Z

14 votes

如果它是一个Spring启动应用程序。

Spring Boot会自动检测public / static / webapp文件夹中的index.html。 如果您已写入任何控制器@Requestmapping("/")它将覆盖默认功能,除非您输入localhost:8080/index.html,否则它不会显示index.html

Krish answered 2019-07-09T13:35:23Z

5 votes

@Configuration

@EnableWebMvc

public class WebAppConfig extends WebMvcConfigurerAdapter {

@Override

public void addViewControllers(ViewControllerRegistry registry) {

registry.addRedirectViewController("/", "index.html");

}

}

Rodrigo Ribeiro answered 2019-07-09T13:35:51Z

2 votes

index.html文件应该位于以下位置 -     src / resources / public / index.html或   SRC/资源/静态/ index.html的如果两个位置都已定义,则首先发生的是index.html将从该目录调用。

源代码看起来像 -

package com.bluestone.pms.app.boot;

import org.springframework.boot.Banner;

import org.springframework.boot.Banner;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.boot.web.support.SpringBootServletInitializer;

import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication

@EnableAutoConfiguration

@ComponentScan(basePackages = {"com.your.pkg"})

public class BootApplication extends SpringBootServletInitializer {

/**

* @param args Arguments

*/

public static void main(String[] args) {

SpringApplication application = new SpringApplication(BootApplication.class);

/* Setting Boot banner off default value is true */

application.setBannerMode(Banner.Mode.OFF);

application.run(args);

}

/**

* @param builder a builder for the application context

* @return the application builder

* @see SpringApplicationBuilder

*/

@Override

protected SpringApplicationBuilder configure(SpringApplicationBuilder

builder) {

return super.configure(builder);

}

}

Pravind Kumar answered 2019-07-09T13:36:40Z

2 votes

在servername:15800内部,我总是将网页放在index.html或spring.mvc.view.suffix或views等文件夹中,并将其放在src/main/resources目录中,如application.properties所示。

JgkZg.png

这是我的servername:15800:

server.port=15800

spring.mvc.view.prefix=/public/

spring.mvc.view.suffix=.html

spring.datasource.url=jdbc:mysql://localhost:3306/hibernatedb

spring.datasource.username=root

spring.datasource.password=password

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

spring.jpa.hibernate.ddl-auto = update

spring.jpa.properties.hibernate.format_sql = true

logging.level.org.hibernate.SQL=DEBUG

logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

一旦你把像servername:15800这样的网址和Spring Boot收到的这个请求占用了Servlet调度程序,它将完全搜索到index.html,这个名称将在spring.mvc.view.suffix的情况下敏感,如html,jsp,htm等。

希望它能帮到很多人。

ArifMustafa answered 2019-07-09T13:37:39Z

2 votes

更新:2019年1月

首先在资源下创建公用文件夹并创建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");

}

}

Sampath T answered 2019-07-09T13:38:19Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值