如何将我的应用程序根
http://localhost:8080/
映射到静态index.html
?
如果我导航到 http://localhost:8080/index.html
它的工作正常。
我的应用程序结构是:
我的
config\WebConfig.java
看起来像这样:
@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/");
}
}
我尝试添加
registry.addResourceHandler("/").addResourceLocations("/index.html");
但失败了。
如果您没有使用
@EnableWebMvc
注释,它会开箱即用。当您这样做时,您会关闭 Spring Boot 在 WebMvcAutoConfiguration
中为您所做的所有事情。您可以删除该注释,或者可以添加回您关闭的视图控制器(在您的WebMvcConfigurer
中):
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}
使用较新的 Spring Boot 版本(3.2.3 或更高版本),您仍然可以执行上述操作,但 Spring MVC 开发人员建议使用另一种替代方法,即使用
@Bean
类型的 RouterFunction
。例如:
static import org.springframework.web.servlet.function.RequestPredicates.path;
...
@Bean
RouterFunction<ServerResponse> spaRouter() {
ClassPathResource index = new ClassPathResource("static/index.html");
return route().resource(path("/"), index).build();;
}
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");
}
};
}
}
如果是Spring boot App。
Spring Boot 自动检测 public/static/webapp 文件夹中的index.html。如果您编写了任何控制器
@Requestmapping("/")
,它将覆盖默认功能,并且不会显示index.html
,除非您键入localhost:8080/index.html
如果您使用最新的
spring-boot 2.1.6.RELEASE
和简单的 @RestController
注释,那么您不需要执行任何操作,只需将您的 index.html
文件添加到 resources/static
文件夹下即可:
project
├── src
├── main
└── resources
└── static
└── index.html
然后点击应用程序的根 URL http://localhost:8080。
上面的解决方案可以在 Spring 和 Tomcat 中开箱即用,并且您对根
/
的 HTTP 请求会自动映射到 index.html
文件。但如果您使用了 @EnableWebMvc
注释,那么您就关闭了 Spring Boot 为您所做的功能。在这种情况下,您有两个选择:
(1)删除该注释
(2) 或者您可以添加回您关闭的视图控制器
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}
}
希望对大家有帮助。
@Configuration
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "index.html");
}
}
更新:2019 年 1 月
首先在resources下创建public文件夹,并创建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");
}
}
您可以添加一个 RedirectViewController,例如:
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "/index.html");
}
}
在
Spring Boot
内,我总是将网页放在 public
或 webapps
或 views
等文件夹中,并将其放在 src/main/resources
目录中,正如您在 application.properties
中看到的那样。
这是我的
application.properties
:
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
这样的 url,并且 Spring Boot 收到的请求占用了 Servlet 调度程序,它将精确搜索 index.html
,并且该名称将区分大小写,与 spring.mvc.view.suffix
一样,它可以是 html、jsp、htm 等.
希望对大家有帮助。
我也有同样的问题。 Spring Boot 知道静态 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);
}
}