spring-boot-thymeleaf-starter-“圆形视图路径[着陆],请检查您的ViewResolver设置!错误

时间:2019-05-21 22:20:32

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

我已经使用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";
    }

1 个答案:

答案 0 :(得分:0)

首先检查您的视图解析器配置是否正确。尝试像这样配置百里香视图解析器:

@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的问题,这将解决您的问题。