About Spring ViewResolver

ViewResolver is very important component of Spring MVC. Without proper understanding, it is difficult to analyze MVC components, especially when taking over other’s source code. The following explanations is based on Spring Framework 4.3.18.

What is ViewResolver? (org.springframework.web.servlet.ViewResolver)

  • ViewResolver is the translator which converts view name into View object
  • View is the component which renders response to the client
  • By defining ViewResolver, you can decide the way a response is shown. (ex. html, json, binary)
  • ViewResolver is an interface with one method
View resolveViewName(String viewName, Locale locale);
  • View name is usually defined by Controller (which is annotated with @Controller)

How to define view name?

View name is defined by Controller method’s return value.

    • String : String return value is handled as view name
    • ModelAndView : org.springframework.web.servlet.ModelAndView can contain view name (by constructor or setViewName())
    • example (which returns String as view name)
import java.util.Date;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);

		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

		String formattedDate = dateFormat.format(date);

		model.addAttribute("serverTime", formattedDate );

		return "home";
	}

}

How to define ViewResolver?

  • ViewResolver should be defined to decide the way a view name is interpreted
  • ViewResolver is defined in servlet context xml (ex. servlet-context.xml)
  • Multiple ViewResolver can be defined. In this case, view name is resolved in sequence based on “order” property
  • example (which shows that BeanNameViewResolver is resolved the first and InternalResourceViewResolver the second)
    servlet-context.xml
  • If no ViewResolver is defined, the default is InternalResourceViewResolver (it’s defined at DispatcherServlet.properties in spring-webmvc.jar)

Commonly used ViewResolver

InternalResourceViewResolver

  • org.springframework.web.servlet.view.InternalResourceViewResolver
  • It interpretes view name as jsp or servlet
  • It’s default ViewResolver
  • Internal JstlView handles matching jsp or servlet (org.springframework.web.servlet.view.JstlView)
  • Matching jsp or servlet is calculated with “prefix” and “suffix” property
  • Most commonly used to return jsp or servlet

BeanNameViewResolver

  • org.springframework.web.servlet.view.BeanNameViewResolver
  • It interpretes view name as bean name
  • Matching bean must be declared
  • Matching bean could implement View or extend AbstractView
  • It is used when customizing output is needed (ex. JSON response)

2 thoughts on “About Spring ViewResolver

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.