【问题标题】:Return HTML page from Spring controller从 Spring 控制器返回 HTML 页面
【发布时间】:2018-11-14 09:35:00
【问题描述】:

我将 Spring Boot 应用程序设置为 REST api。我现在还希望能够为客户端提供简单的 HTML 页面,而无需在任何模板引擎(如 Thymeleaf)上使用。我希望通过使用 WebSecurityConfigurerAdapter 来访问 HTML 页面,使其受到 Spring Security 设置的相同安全约束,该约束已经存在于我的应用程序中。


我尝试过的是Controller

@Controller
public class HtmlPageController {
    @RequestMapping(value = "/some/path/test", method = RequestMethod.GET)
    public String getTestPage() {
        return "test.html";
    }
}

并将test.html 文件放入/resources/test.html/webapp/WEB-INF/test.html

每次我尝试访问localhost:8080/some/path/test 的页面时,都会返回404

我该如何进行这项工作?

【问题讨论】:

  • 试试return "test";
  • 如果我这样做,服务器会抛出 Circular view path [test]: would dispatch back to the current handler URL [/some/path/test] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

标签: java spring spring-mvc spring-boot


【解决方案1】:

好吧,显然 Spring Boot 支持这一点,无需任何额外的配置或控制器。

我所要做的就是将 HTML 文件放在正确的目录 /resources/static/some/path/test.html 中,并且可以通过 localhost:8080/some/path/test.html 访问它。

在尝试更改提供文件的目录时,我没有成功。 It seems that 提供一个单独的 @EnableWebMvc(需要配置资源处理程序)会破坏 Spring Boot 配置。但我可以接受使用默认的/static 目录。

【讨论】:

    【解决方案2】:

    存在提供静态资源的 Spring MVC 机制。

    在配置类中,覆盖此方法:

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
            .addResourceHandler("some/path/*.html")
            .addResourceLocations("/static/");
    }
    

    并将您的 html 文件放在 src/main/webapp/static/ 文件夹中。

    如果您请求some/path/test.html(注意.html),它将返回位于static 文件夹中的test.html 文件。

    您显然可以使用不同的文件夹或更复杂的目录结构。

    这样您就不必创建控制器。请注意,您的配置类应该实现WebMvcConfigurer

    【讨论】:

      【解决方案3】:

      您的 html、js 和 css 文件应位于 src/main/resources/static 目录下。以及您的退货声明,您可以尝试删除 .html。

      @RestController
      public class HtmlPageController {
          @GetMapping("/some/path/test")
          public String getTestPage() {
              return "test";
          }
      }
      

      【讨论】:

      • 如果我这样做,服务器会抛出 Circular view path [test]: would dispatch back to the current handler URL [/some/path/test] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
      • 将@Controller替换为@RestController并添加thymeleaf依赖。
      • 这个问题明确指出我不想使用任何像 Thymeleaf 这样的模板引擎。
      • @RestContoller 可以。无需添加任何 thymeleaf 依赖
      • 不,不会的。 @RestController 与使用@ResponseBody 注释方法相同,然后将在响应正文中返回字符串“test”。
      【解决方案4】:

      tutotrial example如何在Spring MVC配置中定义html视图

      @Bean
      public InternalResourceViewResolver htmlViewResolver() {
          InternalResourceViewResolver bean = new InternalResourceViewResolver();
          bean.setPrefix("/WEB-INF/html/");
          bean.setSuffix(".html");
          bean.setOrder(2);
          return bean;
      }
      
      • setOrder 设置为 2,因为它还包括示例中的 JSP 支持

      另外你需要改成不带.html后缀的返回

      return "test.html";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-07
        • 2016-12-06
        • 2021-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-21
        • 2016-12-09
        相关资源
        最近更新 更多