简体   繁体   English

Spring引导映射静态html

[英]Spring boot mapping static html

I want to create spring boot web application.我想创建 Spring Boot Web 应用程序。

I have two static html files: one.html, two.html.我有两个静态 html 文件:one.html、two.html。

I want to map them as follows我想将它们映射如下

localhost:8080/one
localhost:8080/two

without using template engines (Thymeleaf).不使用模板引擎(Thymeleaf)。

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).我已经尝试了很多方法来做到这一点,但我有 404 错误或 500 错误(循环视图路径 [one.html]: 将调度回当前处理程序 URL)。

OneController.java is: OneController.java 是:

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

Project structure is项目结构是

在此处输入图片说明

Please update your WebMvcConfig and include UrlBasedViewResolver and /static resource handler.请更新您的 WebMvcConfig 并包含 UrlBasedViewResolver 和 /static 资源处理程序。 Mine WebConfig class looks as follows:我的 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;
    }

}

I have checked it and seems working.我已经检查过了,似乎工作正常。

Maciej's answer is based on browser's redirect. Maciej 的回答基于浏览器的重定向。 My solution returns static without browser interaction.我的解决方案在没有浏览器交互的情况下返回静态。

In my case I want to map all sub paths to the same file but keep the browser path as the original requested, at same time I use thymeleaf then I don't want to override it's resolver.在我的情况下,我想将所有子路径映射到同一个文件,但将浏览器路径保留为原始请求,同时我使用 thymeleaf 然后我不想覆盖它的解析器。

@Controller
public class Controller {

    @Value("${:classpath:/hawtio-static/index.html}")
    private Resource index;

    @GetMapping(value = {"/jmx/*", "/jvm/*"}, produces = MediaType.TEXT_HTML_VALUE)
    @ResponseBody
    public ResponseEntity actions() throws IOException {
        return ResponseEntity.ok(new InputStreamResource(index.getInputStream()));
    }
}

Obs.观察。 every hit will read the data from the index.html file, it will not be cached每次命中都会从 index.html 文件中读取数据,不会被缓存

If you don't care about additional browser redirect you can use this:如果你不关心额外的浏览器重定向,你可以使用这个:

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

I am new to spring along with thymeleaf and spent an hour trying to figure this out.我是春天和百里香的新手,花了一个小时试图解决这个问题。

In your "application.properties" add在您的“application.properties”中添加

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM