Sho*_*ham 121 java spring spring-boot
我是Java和Spring的新手.如何将我的应用程序根映射http://localhost:8080/到静态index.html?如果我导航到http://localhost:8080/index.html它的工作正常.
我的app结构是:

我config\WebConfig.java看起来像这样:
@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/");
}
}
Run Code Online (Sandbox Code Playgroud)
我试图添加registry.addResourceHandler("/").addResourceLocations("/index.html");但它失败了.
Dav*_*yer 140
如果您没有使用@EnableWebMvc注释,它可以开箱即用.当你这样做时,你关掉Spring Boot为你做的所有事情WebMvcAutoConfiguration.您可以删除该注释,或者可以添加回您关闭的视图控制器:
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}
Run Code Online (Sandbox Code Playgroud)
jus*_*tin 43
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");
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
小智 18
如果它是一个Spring启动应用程序.
Spring Boot会自动检测public/static/webapp文件夹中的index.html.如果您已编写任何控制器@Requestmapping("/"),它将覆盖默认功能,index.html除非您键入,否则不会显示localhost:8080/index.html
sam*_*hlk 12
更新: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");
}
}
Run Code Online (Sandbox Code Playgroud)
zap*_*pee 10
如果您使用spring-boot 2.1.6.RELEASE带有简单@RestController注释的最新版本,那么您无需执行任何操作,只需将您的index.html文件添加到resources/static文件夹下:
project
??? src
??? main
??? resources
??? static
??? index.html
Run Code Online (Sandbox Code Playgroud)
然后点击 URL http://localhost:8080。希望对大家有所帮助。
@Configuration
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "index.html");
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
您可以添加一个 RedirectViewController,例如:
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "/index.html");
}
}
Run Code Online (Sandbox Code Playgroud)
在里面Spring Boot,我总是把网页放在像publicorwebapps或 or这样的文件夹中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
Run Code Online (Sandbox Code Playgroud)
一旦您输入类似 url 的 urlservername:15800并且 Spring Boot 接收到的这个请求占用了 Servlet 调度程序,它就会精确搜索index.html并且这个名称将区分大小写,因为spring.mvc.view.suffix它是 html、jsp、htm 等。
希望它能帮助很多人。
小智 5
我有同样的问题。Spring Boot 知道静态 html 文件所在的位置。
| 归档时间: |
|
| 查看次数: |
153867 次 |
| 最近记录: |