spring-boot-thymeleaf-starter - “圆形视图路径[着陆],检查您的 ViewResolver 设置!错误
spring-boot-thymeleaf-starter - "Circular view path[landing],Check your ViewResolver setup! error
我已经使用 spring-security-saml2 实现了 saml-adfs 服务提供者。 SAML-ADFS 身份验证正确进行。身份验证后,我试图将其重定向到登录页面,该页面有一些变量,例如根据登录的用户信息动态填充的 UserID。对于 html 页面渲染,我使用 spring-boot-started-thymeleaf 库。我浏览了各种文章并完成了以下配置。我所有的 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";
}
首先检查您的视图解析器是否已配置 correctly.Try 配置您的 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 解析器将处理解析用于访问 html 的 uri,这应该可以解决您的问题。
我已经使用 spring-security-saml2 实现了 saml-adfs 服务提供者。 SAML-ADFS 身份验证正确进行。身份验证后,我试图将其重定向到登录页面,该页面有一些变量,例如根据登录的用户信息动态填充的 UserID。对于 html 页面渲染,我使用 spring-boot-started-thymeleaf 库。我浏览了各种文章并完成了以下配置。我所有的 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";
}
首先检查您的视图解析器是否已配置 correctly.Try 配置您的 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 解析器将处理解析用于访问 html 的 uri,这应该可以解决您的问题。