春季版本: 4.1.6,Thymeleaf版本: 3.0.0
本质上,在某些场景中,我希望使用Thymeleaf以外的东西来呈现HTML。所以如果我的控制器是这样的:
@RequestMapping(value={"/","/index"}, method={RequestMethod.GET, RequestMethod.POST})
public String renderIndexForCompanyUser(Model model){
model.addAttribute("someKey", "someValue");
return "index";
}我想要一个拦截器,或者类似的,可以做到的:
//pseudocode
public void intercept(Response response){
boolean someCondition = ...
if(someCondition){
renderHTMLWithoutThymeleaf(response.getTemplateName(), response.getModel());
// Thymeleaf rendering gets skipped in this scenario
}
else {
// render via Thymeleaf as normal
}
}这样的事有可能吗?我正在HandlerInterceptor接口上找到文档,但我不知道如何在某些场景中跳过Thymeleaf
发布于 2022-07-19 11:40:29
视图名称的解释由ViewResoler处理。Spring默认创建其中一些视图,而其他视图则通过项目依赖项添加到应用程序上下文中。Thymeleaf的情况是这样的--它自动配置一个ThymeleafViewResolver,它实际上具有一些有用的属性来控制它将呈现哪些视图:
spring.thymeleaf.view-names -逗号-可以解析的视图名称(允许的模式)列表。
spring.thymeleaf.excluded-view-names -逗号分隔的视图名称列表(允许的模式)应该被排除在解析之外。
spring.thymeleaf.prefix -前缀,用于在构建URL时查看名称,例如classpath:/templates/
spring.thymeleaf.suffix -在构建.html时被追加到查看名称的后缀,例如.html
配置了将由Thymeleaf专门处理的视图之后,您可以根据控制器中的条件返回那些视图或由另一个视图解析器处理的视图。
https://stackoverflow.com/questions/73028751
复制相似问题