Skip to content

Allow UrlBasedViewResolver to delegate if file not found [SPR-3661] #8342

@spring-projects-issues

Description

@spring-projects-issues

Peter Backlund opened SPR-3661 and commented

ViewResolvers that inherit UrlBasedViewResolver, such as FreemarkerVR and VelocityVR, don't have the ability to return null if a template file is not found. For example, FreemarkerView will run checkTemplate() on app context initialization, which will throw an IOException if the template file is not found. This exception will propagate past the view resover chaining in DispatcherServlet instead of sending the signal to try the next available view resolver.

My proposal is that the UrlBasedViewResolver.loadView() method catches whatever exception is thrown when the template file is missing (currently ApplicationContextException, might be a good idea to narrow down the scope by using a dedicated exception type), and instead return null (possibly with a flag to tune the behaviour).

Something like this:

protected View loadView(String viewName, Locale locale) throws Exception {
	AbstractUrlBasedView view = buildView(viewName);
	try {
              return (View) getApplicationContext().getAutowireCapableBeanFactory().initializeBean(view, viewName);
            } catch (TemplateNotFoundException e) {
              if (throwExceptionIfNotFound) {
                 throw e;
              } else {
                 return null;
              }
            }
}

Affects: 2.0.5

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions