Sending data from controller to view

Most dynamic web pages are composed of template and data. In Spring MVC, data is sent usually from controller to view.

How to send data (from controller’s point)

Usually data is created in controller. The following is some ways to send data. (Not exhaustive explanation. More on RequestMapping handler method)

  • To declare Model as parameter (org.springframework.ui.model)

If Model is declared as a paremter, it is injected by Spring Framework.

import java.text.DateFormat;
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;

@Controller
public class HomeController {

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

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		String formattedDate = dateFormat.format(date);
		model.addAttribute("serverTime", formattedDate );
		return "home";
	}

}
  • To use ModelAndView as method return (org.springframework.web.servlet.ModelAndView)

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController2 {

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView home(Locale locale) {
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		ModelAndView mav = new ModelAndView();
		ModelMap mMap = mav.getModelMap();
		String formattedDate = dateFormat.format(date);
		mMap.addAttribute("serverTime", formattedDate);
		mav.setViewName("home");
		return mav;
	}
}

How to retrieve data (from view’s point)

The way to retrieve data is defferent depending on which View is chosen. (ViewResolver decides View object. More on ViewResolver)

  • JstlView (When InternalResourceViewResolver is used)

When InternalResourceViewResolver is used, internal JstlView is chosen. Inside JstlView, Model object’s all attributes are set as HttpServletRequest’s attribute.

home.jsp

“serverTime” attribute is converted as HttpServletRequest’s attribute. Therefore, common ${attrName} works.

  • Custom View (When BeanNameViewResolver is used)

When you build custom view, it must implement View or extend AbstractView. View interface has following method.


void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception;

Model is injected by Spring Framework. Therefore data can be accessed by Model.asMap().get()

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 )

Google photo

You are commenting using your Google 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.