Spring Boot映射静态HTML [英] Spring boot mapping static html

查看:280
本文介绍了Spring Boot映射静态HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建Spring Boot Web应用程序.

I want to create spring boot web application.

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

I have two static html files: one.html, two.html.

我想按如下方式映射它们

I want to map them as follows

localhost:8080/one
localhost:8080/two

不使用模板引擎(Thymeleaf).

without using template engines (Thymeleaf).

该怎么做?我尝试了很多方法来执行此操作,但是我遇到404错误或500错误(循环视图路径[one.html]:将分派回当前的处理程序URL).

How to do that? I have tried many ways to do that, but I have 404 error or 500 error (Circular view path [one.html]: would dispatch back to the current handler URL).

OneController.java是:

OneController.java is:

@Controller
public class OneController {
    @RequestMapping("/one")
    public String one() {
        return "static/one.html";
    }
}

项目结构为

推荐答案

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

Please update your WebMvcConfig and include UrlBasedViewResolver and /static resource handler. Mine WebConfig class looks as follows:

@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;
    }

}

我已经检查了它,并且似乎可以正常工作.

I have checked it and seems working.

Maciej的答案基于浏览器的重定向.我的解决方案在没有浏览器交互的情况下返回静态.

Maciej's answer is based on browser's redirect. My solution returns static without browser interaction.

这篇关于Spring Boot映射静态HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆