【问题标题】:Spring boot mapping static htmlSpring Boot 映射静态 html
【发布时间】:2016-12-24 06:19:18
【问题描述】:

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

我有两个静态 html 文件:一个.html、两个.html。

我想将它们映射如下

localhost:8080/one
localhost:8080/two

不使用模板引擎(Thymeleaf)。

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

OneController.java 是:

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

项目结构是

【问题讨论】:

    标签: java spring-boot spring-mvc static-html


    【解决方案1】:

    我和百里香一起是春天的新手,花了一个小时试图弄清楚这一点。

    在你的“application.properties”中添加

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

    【讨论】:

      【解决方案2】:

      在我的情况下,我想将所有子路径映射到同一个文件,但将浏览器路径保持为原始请求的路径,同时我使用 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()));
          }
      }
      

      观察。每次点击都会从 index.html 文件中读取数据,不会被缓存

      【讨论】:

      • InputStreamResource 不是必需的。 ResponseEntity.ok(index) 工作正常。
      【解决方案3】:

      如果你不关心额外的浏览器重定向,你可以使用这个:

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

      【讨论】:

        【解决方案4】:

        请更新您的 WebMvcConfig 并包含 UrlBasedViewResolver 和 /static 资源处理程序。我的 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;
            }
        
        }
        

        我已经检查过了,似乎工作正常。

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

        【讨论】:

        • 由于 Spring 5 WebMvcConfigurerAdapter 被标记为 Deprecated,所以需要实现新的接口 WebMvcConfigurer !
        • 我认为问题在于这个解析器对于所有 spring 上下文都是全局的,我使用 thymeleaf 或 JSP 并想使用一个特定的控制器来提供静态文件呢?
        • 在实现 WebMvcConfigurer 时删除“super.addResourceHandlers(registry)”。查看链接 [stackoverflow.com/questions/42393211/…
        猜你喜欢
        • 2016-09-26
        • 1970-01-01
        • 2020-07-21
        • 2017-02-21
        • 2014-04-13
        • 1970-01-01
        • 2017-03-10
        • 1970-01-01
        • 2020-01-26
        相关资源
        最近更新 更多