0% found this document useful (0 votes)
29 views

Spring Sending Data From Controller To UI

ModelAndView is used to send data from a controller to a UI page in Spring. It allows adding data using addObject() with a key, which can then be retrieved in the UI using that same key. For example, primitive values, objects, and collections can be added to the ModelAndView and accessed in JSP using expression language or scriptlets. JSTL tags are also useful for iterating over collections from the ModelAndView.

Uploaded by

ni3ma ni3ma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Spring Sending Data From Controller To UI

ModelAndView is used to send data from a controller to a UI page in Spring. It allows adding data using addObject() with a key, which can then be retrieved in the UI using that same key. For example, primitive values, objects, and collections can be added to the ModelAndView and accessed in JSP using expression language or scriptlets. JSTL tags are also useful for iterating over collections from the ModelAndView.

Uploaded by

ni3ma ni3ma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Spring Sending Data From Controller to UI:-

ModelAndView is a common Object to send data controller and UI page. ModelAndView


had a method addObject(String key,Object value), is used to add data to MAV Object. Using
this same key we have to retrieve data at UI level.

Ex:

Sending Primitive Values at Controller:

package com.app;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {

@RequestMapping("/welcome")
public ModelAndView welcome(){
ModelAndView mav=new ModelAndView("welcome");
mav.addObject("empId", 100);
mav.addObject("empName", "AJ");
mav.addObject("empSal", 250.36);
return mav;
}

JSP Code to Read Data:


Welcome.jsp

<h1>Data is :</h1>

<b>${empId},${empName},${empSal}</b>

<%=req.getAttribute(“empId”)%>

Sending Object Data at Controller:

package com.app;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {

@RequestMapping("/welcome")
public ModelAndView welcome(){
ModelAndView mav=new ModelAndView("welcome");
Employee emp=new Employee();
emp.setEmpId(10);
emp.setEmpName("AJ");
emp.setEmpSal(12.35);
mav.addObject("empObj", emp);
return mav;
}

Reading data at JSP page:-

<b> ${empObj.empId},${empObj.empName},${empObj.empSal}</b>

Or

<% Employee emp=req.getAttribute(“empObj”);

Out.print(emp.getEmpId());

%>

Sending Collection Data from Controller:

package com.app;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {

@RequestMapping("/welcome")
public ModelAndView welcome(){
ModelAndView mav=new ModelAndView("welcome");
Employee emp=new Employee();
emp.setEmpId(10);
emp.setEmpName("AJ");
emp.setEmpSal(12.35);

Employee emp2=new Employee();


emp2.setEmpId(10);
emp2.setEmpName("AJ");
emp2.setEmpSal(12.35);

Employee emp3=new Employee();


emp3.setEmpId(10);
emp3.setEmpName("AJ");
emp3.setEmpSal(12.35);

List<Employee> empListObj=new ArrayList<Employee>();


empListObj.add(emp);
empListObj.add(emp2);
empListObj.add(emp3);
mav.addObject("empListObj", empListObj);
return mav;
}

Reading Data At JSP Level:

<c:forEach items="${empListObj}" var="emp">


<c:out value="${emp.empId}"/>,<c:out value="${emp.empName}"/>,<c:out
value="${emp.empSal}"/><br/>
</c:forEach>

or

<%
List empList=request.getAttribute("empListObj");
Iterator iterator= empList.iterator();
while(iterator.hasNext()){
Employee emp=(Employee)iterator.next();
out.println(emp);
}
%>

For JSTL code:

Add below line in JSP:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Download and Add JSTL JAR in lib folder:

Location : http://central.maven.org/maven2/javax/servlet/jstl/1.2/jstl-1.2.jar

You might also like