BeanNameViewResolver resolves a view name by looking up a bean with the same name in the current application context. The matching bean must implement the View interface so that DispatcherServlet can call it to render the response.
If no bean with that name exists, or the bean is not a View, the resolver returns null and the next ViewResolver in the chain gets a chance to resolve the view name. By default, its order is Ordered.LOWEST_PRECEDENCE, so it is consulted last unless we change that with setOrder().
Example
A custom view
package com.logicbig.example;
import org.springframework.web.servlet.View;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;
public class MyCustomView implements View {
@Override
public String getContentType() {
return "text/html";
}
@Override
public void render(Map<String, ?> model,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
response.setContentType(getContentType());
PrintWriter writer = response.getWriter();
writer.println("This is my custom dummy view.<br/>");
writer.println("<h3>Model attributes</h3>");
for (Map.Entry<String, ?> entry : model.entrySet()) {
writer.printf("%s = %s<br/>", entry.getKey(), entry.getValue());
}
}
}
Registering BeanNameViewResolver and the view as beans
The name of the view bean (simpleView) is what our controller will return as the view name.
package com.logicbig.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.BeanNameViewResolver;
@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig {
@Bean
public ViewResolver viewResolver() {
return new BeanNameViewResolver();
}
@Bean(name = "simpleView")
public View myCustomView() {
return new MyCustomView();
}
}
Alternative way to register BeanNameViewResolver
As an alternative to the above configuration, we can enable BeanNameViewResolver by implementing the configureViewResolvers() method of WebMvcConfigurer. The old WebMvcConfigurerAdapter class was removed in Spring Framework 6.0; since WebMvcConfigurer now provides default methods, an adapter class is no longer needed:
@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig implements WebMvcConfigurer {
@Override
public void configureViewResolvers(
ViewResolverRegistry registry) {
registry.beanName();
}
@Bean("simpleView")
public View myCustomView() {
return new MyCustomView();
}
}
Writing a controller
The controller returns our bean name as the view name.
package com.logicbig.example;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.time.LocalTime;
@Controller
@RequestMapping("/")
public class MyController {
@RequestMapping
public String handleRequest (Model model) {
model.addAttribute("msg",
"A message from the controller");
model.addAttribute("time", LocalTime.now());
return "simpleView";
}
}
Here is a diagram that illustrates how BeanNameViewResolver resolves a view by matching the controller's returned view name to a bean in the application context.
To try examples, run embedded Jetty (configured in pom.xml of example project below):
mvn jetty:run
Output
 $ curl -s "http://localhost:8080" This is my custom dummy view.<br/> <h3>Model attributes</h3> msg = A message from the controller<br/>time = 07:27:26.263<br/>org.springframework.validation.BindingResult.time = org.springframework.validation.BeanPropertyBindingResult: 0 errors<br/>
Example ProjectDependencies and Technologies Used: - spring-webmvc 7.0.6 (Spring Web MVC)
Version Compatibility: 3.2.9.RELEASE - 7.0.6 Version compatibilities of spring-webmvc with this example: Versions in green have been tested.
- jakarta.servlet-api 6.1.0 (Jakarta Servlet API documentation)
- JDK 25
- Maven 3.9.11
|