Spring version: 4.1.6, Thymeleaf version: 3.0.0
Essentially, in certain scenarios, I want to render HTML using something other than Thymeleaf. So if my controller looks like this:
@RequestMapping(value={"/","/index"}, method={RequestMethod.GET, RequestMethod.POST})
public String renderIndexForCompanyUser(Model model){
model.addAttribute("someKey", "someValue");
return "index";
}
I want an interceptor, or something similar, that can do:
//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
}
}
Is something like this possible? I'm finding documentation on the HandlerInterceptor interface, but I'm not sure how to get Spring to skip Thymeleaf in certain scenarios
CodePudding user response:
The view name interpretation is handled by ViewResoler
's. Spring creates some of them by default and other ones are added to the application context by project dependencies. This is the case with Thymeleaf - it autoconfigures a ThymeleafViewResolver
which in fact has some useful properties to control which views will be rendered by it:
spring.thymeleaf.view-names
- Comma-separated list of view names (patterns allowed) that can be resolved.
spring.thymeleaf.excluded-view-names
- Comma-separated list of view names (patterns allowed) that should be excluded from resolution.
spring.thymeleaf.prefix
- Prefix that gets prepended to view names when building a URL, e.g. classpath:/templates/
spring.thymeleaf.suffix
- Suffix that gets appended to view names when building a URL, e.g. .html
Having configured the views that will be handled specifically by Thymeleaf you can then return either those or the ones handled by another view resolver based on the condition in your controller.