DAOService.java

public class DAOService 
{
	private List<Person> arrPersons = new ArrayList<Person>(); 
			
	public DAOService()
	{
		
		Person objPerson = new Person();
		objPerson.setName("Person 1");
		objPerson.setLocation("Teynampet");
		
		arrPersons.add(objPerson);
		
		Person objPerson2 = new Person();
		objPerson2.setName("Person 2");
		objPerson2.setLocation("TNagar");
		
		arrPersons.add(objPerson2);
	}

	public List getList()
	{
		return this.arrPersons;
	}
}

ListPerson.java

@Controller
@RequestMapping("/List")
public class ListPerson 
{	
	@Autowired
	private DAOService objDAOService;
	
	@RequestMapping("/PersonList")
	public String listPersons(Model model)
	{
		DAOService objDAOService = new DAOService();
		
		model.addAttribute("personList", this.objDAOService.getList());
		
		return "display_list";
	}
}

display_list.jsp

<title>Insert title here</title>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
</head>
<body>	
	<table>
		<c:forEach items="${personList}" var="objPerson"> 
			<tr>
				<td>${objPerson.name}</td>
				<td>${objPerson.location}</td>
			</tr>
		</c:forEach>
	</table>
</body>

applicationContext.xml

<bean name="DAOService" class="com.mugil.controls.DAOService"></bean>

pom.xml

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

Method Arguments in Controller

@Controller
@RequestMapping("/Control1")
public class TestController 
{	
	@RequestMapping("/Control2")		
	public String TestMe(HttpSession session, HttpServletRequest request)
	{
		request.setAttribute("name", "Mugil Vannan");
		session.setAttribute("Test", "TestValue");
		System.out.println("1");
		return "hello";
	}
	
	@RequestMapping(value="/Control2", method=RequestMethod.POST)
	public String TestMe2(HttpSession session, HttpServletRequest request)
	{
		System.out.println(session.getAttribute("Test"));
		System.out.println(request.getParameter("cboArea"));		
		return "display";
	}
}

Alternate of HttpServletRequest request

@RequestMapping(value="/Control2", method=RequestMethod.POST)
public String TestMe2(HttpSession session, @RequestParam("cboArea") String Area)
{
  System.out.println(session.getAttribute("Test"));
  System.out.println(Area);		
  return "display";
}

Comments are closed.