【问题标题】:spring-boot-thymeleaf-starter - "Circular view path[landing],Check your ViewResolver setup! errorspring-boot-thymeleaf-starter -“圆形视图路径[着陆],检查您的 ViewResolver 设置!错误
【发布时间】:2019-05-21 22:20:31
【问题描述】:

我已经使用 spring-security-saml2 实现了 saml-adfs 服务提供者。 SAML-ADFS 身份验证正常进行。身份验证后,我试图将其重定向到登录页面,该页面几乎没有变量,例如根据登录的用户信息动态填充的 UserID。对于 html 页面渲染,我使用的是 spring-boot-started-thymeleaf lib。我浏览了各种文章并完成了以下配置。我所有的 html 文件都存在于 src/main/resources/templates 中。

我收到“圆形视图路径[着陆],请检查您的 ViewResolver 设置!错误。

If I change it to a static html page, which is present in src/main/resources/static folder, then it is loading the content.

Please guide me how I can resolve this issue. I have dependency of spring-boot-starter-web and spring-boot-starter-thymeleaf in my build.gradle



landing.html page

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Landing!</title>
    </head>
    <body>
        <h1>You are logged as <span th:text="${username}">null</span>!</h1>
        <p>
            <a th:href="@{/saml/logout}">Global Logout</a><br/>
            <a th:href="@{/saml/logout?local=true}">Local Logout</a>
        </p>
    </body>
</html>

@Controller
public class LandingController {

    @RequestMapping("/landing")`enter code here`
    public String landing(@CurrentUser User user, Model model) {
        model.addAttribute("username",  user.getUsername());
        return "landing";
    }

【问题讨论】:

标签: spring-boot thymeleaf spring-security-saml2


【解决方案1】:

首先检查您的视图解析器是否配置正确。尝试配置您的 thymeleaf 视图解析器,如下所示:

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public ClassLoaderTemplateResolver templateResolver() {

        var templateResolver = new ClassLoaderTemplateResolver();

        templateResolver.setPrefix("templates/");
        templateResolver.setCacheable(false);
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setCharacterEncoding("UTF-8");

        return templateResolver;
    }

   @Bean
   public SpringTemplateEngine templateEngine() {
       SpringTemplateEngine templateEngine = new SpringTemplateEngine();
       templateEngine.setTemplateResolver(templateResolver());
       templateEngine.setTemplateEngineMessageSource(messageSource());
       return templateEngine;
   }

   @Bean
   public ViewResolver viewResolver() {
       var viewResolver = new ThymeleafViewResolver();   
       viewResolver.setTemplateEngine(templateEngine());
       viewResolver.setCharacterEncoding("UTF-8");
       return viewResolver;
    }
}

这是基本配置,您可以根据您的项目结构在此配置中更改模板的位置等。

当您提供此配置时,Thymeleafview 解析器将处理解析 uri 以访问应该解决您的问题的 html。

【讨论】:

  • 感谢@Ananthapadmanabhan,它成功了。我的印象是 springboot 默认会处理这些 bean,但似乎情况并非如此。
  • 乐于助人?
猜你喜欢
  • 2017-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-25
  • 2016-08-10
  • 2014-09-10
  • 2017-04-09
  • 2014-05-09
相关资源
最近更新 更多