Module 16, 17: Adding Data to Model - dineshmadhup/Spring GitHub Wiki

This description is explained based on code in Module-16 and 17

In Module-15, we just read the form data using getParameter() method and display in the view page.

In this module, we are going to store the form data in model. The Model is a container for application data. In controller, you can put anything in the Model such as Strings, Objects, and information from database, etc. Your view page (jsp) can access the data from the Model using ${…}.

How to pass Model to Controller

Using HttpServletRequest request to read HTML form

ApplicationController.java

// need a controller method to process the HTML form @RequestMapping("/processForm") public String processForm() { return "application"; }

// new  a controller method to read form data and add data to the model
@RequestMapping("/processFormForAddDataToModel")
public String addDataToModel(HttpServletRequest request, Model model) {
	
	// read the request parameter from the html form
	String pName = request.getParameter("personName");
	
	String pAddress = request.getParameter("personAddress");
	
	String pEmail = request.getParameter("personEmail");
	
	String pPhone = request.getParameter("personPhone");
	
	//convert the data to all caps
	pName = pName.toUpperCase();
	pAddress = pAddress.toUpperCase();
	pEmail = pEmail.toUpperCase();
	pPhone = pPhone.toUpperCase();
	
	// create a message
	String result  = "Model Stored Data: \n" + pName + " | " + pAddress + " | " + pEmail + " | " + pPhone;
	
	// add message to the model
	model.addAttribute("message", result);
	
	return "application";
}

Notice that result data is stored in the model using model.addAttribute( ) method.

Steps we followed is:

  1. HttpServletRequest request is used.
  2. Form data is read using getParameter() method. String pAddress = request.getParameter("personAddress");
  3. Read data is added to Model using addAttribute() method. model.addAttribute("message", result);
  4. Return a view page(application.jsp)

application.jsp

Name: ${param.personName}

Address: ${param.personAddress}

Email: ${param.personEmail}

Phone: ${param.personPhone}

Using @RequestParam to read HTML form

// new a controller method to read form data and add data to the model @RequestMapping("/processFormForAddDataToModelVersionTwo") public String addDataToModel( @RequestParam("personName") String pName, @RequestParam("personAddress") String pAddress, @RequestParam("personEmail") String pEmail, @RequestParam("personPhone") String pPhone, Model model) {

		//convert the data to all caps
		pName = pName.toUpperCase();
		pAddress = pAddress.toUpperCase();
		pEmail = pEmail.toUpperCase();
		pPhone = pPhone.toUpperCase();
		


		// create a message
		String result  = "Model Stored Data: \n" + pName + " | " + pAddress + " | " + pEmail + " | " + pPhone;
		
		// add message to the model
		model.addAttribute("message", result);
		
		return "application";
	}

=> In this approach spring will read parameter from request: personName, personAddress, personEmail, personEmail, personPhone.

=> And bind it to the variable: pName, pAddress, pEmail, pPhone respectively.

No need to write these lines while using @ReuestParam:

String pName = request.getParameter("personName");

String pAddress = request.getParameter("personAddress");

String pEmail = request.getParameter("personEmail");

String pPhone = request.getParameter(“personPhone");

References:

  1. Binding request parameters to method parameters with @RequestParam: http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-ann-requestparam

  2. luv2code: http:luv2code.com

  3. javapapers: http://javapapers.com/spring/spring-annotation-based-controllers/