Spring启动映射静态html

meg*_*och 2 spring-mvc static-html spring-boot

我想创建spring boot web应用程序.

我有两个静态html文件:one.html,two.html.

我想将它们映射如下

localhost:8080/one
localhost:8080/two
Run Code Online (Sandbox Code Playgroud)

不使用模板引擎(Thymeleaf).

怎么做?我已经尝试了很多方法来做到这一点,但我有404错误或500错误(圆形视图路径[one.html]:将调度回当前处理程序URL).

OneController.java是:

@Controller
public class OneController {
    @RequestMapping("/one")
    public String one() {
        return "static/one.html";
    }
}
Run Code Online (Sandbox Code Playgroud)

项目结构是

在此输入图像描述

小智 6

请更新您的WebMvcConfig并包含UrlBasedViewResolver和/ static资源处理程序.Mine WebConfig类如下所示:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }

    @Bean
    public ViewResolver viewResolver() {
        UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
        viewResolver.setViewClass(InternalResourceView.class);
        return viewResolver;
    }

}
Run Code Online (Sandbox Code Playgroud)

我检查过它,看起来很有效.

Maciej的答案基于浏览器的重定向.我的解决方案在没有浏览器交互的情

  • 由于Spring 5 WebMvcConfigurerAdapter标记为已弃用,因此您需要实现新接口WebMvcConfigurer! (2认同)