add_person.jsp

<body>
  <form name="frmTest" id="frmTest" method="post" action="/Test2/Control1/displayPerson">
    <input type="text" name="Name" />I Agree
	<select name="Location">
  	  <option value="Teynampet">Teynampet</option>
	  <option value="TNagar">TNagar</option>
	  <option value="Adyar">Adyar</option>
	</select>
    <input type="checkbox" name="cboAgree" value="I Agree"/>I Agree		
    <input type="submit" name="btnSubmit" value="Submit"/>		
  </form>
</body>

Person.java

public class Person 
{
	private String Name;
	private String Location;	
	
	public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}
	public String getLocation() {
		return Location;
	}
	public void setLocation(String location) {
		Location = location;
	}

        @Override
	public String toString() 
        {
	  return "Person [Name=" + Name + ", Location=" + Location + "]";
	}
}

TestController .java

@Controller
@RequestMapping("/Control1")
public class TestController 
{
	@RequestMapping(value="/displayPerson", method=RequestMethod.POST)
	public String TestMe4(@ModelAttribute Person person)
	{
		System.out.println(person); 
		return "add_person";
	}
}

Output

Person [Name=Mac, Location=TNagar]

TestController.java

@RequestMapping(value="/Control2/{personId}")		
public String TestMe(@PathVariable("personId") Long personIdVal)
{
  System.out.println(personIdVal);
  return "hello";
}

Input

http://localhost:8089/Test2/Control1/Control2/152

Output

152

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";
}

TestController.java

@RequestMapping(value="/Control2", method=RequestMethod.POST)
public String TestMe2(Model model)
{
  Person objPerson = new Person();
  objPerson.setName("Mugil");
  objPerson.setAddress("Sample Address");
  objPerson.setAge("27");

  model.addAttribute("objPerson", objPerson);
  return "display";
}

display.jsp

Person Name : ${objPerson.name}
Person Location : ${objPerson.location}
Person Age : ${objPerson.age}
Person Address : ${objPerson.address}
public class Person 
{
 private String Name;
 private String Location;
 private String Age;
 private String Address;

 public String getName() {
	return Name;
 }
 public void setName(String name) {
	Name = name;
 }
 public String getLocation() {
	return Location;
 }
 public void setLocation(String location) {
	Location = location;
 }

 .
 .
 .
}

How to Design Spring MVC for View Page

  1. Have a isSuccess attribute in the bean
  2. Use when and choose to decide whether to form element or form value based on the value set in the isSuccess
  3. When isSuccess is set to true the show the form Value or show the form element else display the form element
  4. When adding a new values in through the form the bean would be set to isSuccess true
  5. Next time when the page gets loaded the form values filled before the click of the submit will be displayed a s form values.

How to access the bean values where the bean has sub bean

class Student
{
 String Name;
 Address houseAddress;
 .
 .
}

For accessing values in Student bean the form elements should be

path=Name value="${student.Name}"

For accessing values in Student Address bean the form elements should be

path=Address.houseAddress value="${Student.Address.StreetName}" 

hello.java

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="frmTest" id="frmTest" method="post">		
	<select name="cboArea">
		<option value="Teynampet">Teynampet</option>
		<option value="TNagar">TNagar</option>
		<option value="Adyar">Adyar</option>
	</select>		
	<input type="checkbox" name="cboAgree" value="I Agree"/>I Agree		
	<input type="submit" name="btnSubmit" value="Submit"/>		
</form>
</body>
</html>

TestController.java

package com.mugil.controls;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/Control1")
public class TestController 
{	
	@RequestMapping("/Control2")		
	public String TestMe()
	{
		return "hello";
	}

	@RequestMapping(value="/Control2", method=RequestMethod.POST)
	public String TestMe2()
	{
		System.out.println("Hi there");
		return "hello";
	}

	@RequestMapping(value="/Control2", method=RequestMethod.POST, params={"cboArea=TNagar", "cboAgree"})
	public String TestMe3()
	{
		System.out.println("I am in TNagar");
		return "hello";
	}
}

Simple Controller Example
TestController.java

package com.mugil.controls;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/Control1")
public class TestController 
{	
  @RequestMapping("/Control2")	
  public String TestMe()
  {
    return "hello";
  }
}

hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
   Hi Dear!
</body>
</html>

dispatcher-servlet.xml

 <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>

HomeController.java

package com.mugil.controls;

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

@Controller
public class HomeController {	
	@RequestMapping("/home")	
	public String testMethod()
	{
		return "home";
	}
}

style.css

h1
{
  color : red;
}

Creating a Preconfigured MVC Project
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Processes application requests -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>
  1. DispatcherServlet is the first servlet which gets called when you run Preconfigured MVC Project
  2. servlet-context.xml in init param of DispatcherServlet get run for other initialization of internal beans
  3. Apart from servlet-context.xml the other xml file that gets called is root-context.xml in a separate context

Test.java

package com.mugil.spring;

import java.util.Locale;
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 Test
{	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String testMethod(Locale locale, Model model) 
	{	
		model.addAttribute("testMsg", "Hi There" );		
		return "home";
	}
}

home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
	<title>Home</title>
</head>
<body>
<h1>
	${testMsg}  
</h1>
</body>
</html>

Output(http://localhost:8089/spring/)

Hi There

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<annotation-driven />
	<resources mapping="/resources/**" location="/resources/" />
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>

	<context:component-scan base-package="com.mugil.spring" />
</beans:beans>
  1. context:component-scan base-package defines the base location in which the controller to be found
  2. beans:property name=”prefix” defines the folder in which jsp should be found
  3. beans:property name=”suffix” defines the valid prefix
  4. Based on the return type of testMethod method the jsp page name is recognized. In our case it is home.jsp
  1. The Way aspect function calls are made are through proxy classes internally
  2. Internally the Spring framework creates proxy classed and calls to the code generated as per the xml are run in proxy class methods before the actual class are called
  3. In the below example in DrawingApp.java I try to create a object for the class circle by invoking factoryService getBean Method
  4. This method returns a Object of class ShapeServiceProxy with the custom methods for xml code added

ShapeService.java

package com.mugil.shapes;

public class ShapeService {
	private Circle objCircle;
	private Triangle objTriangle;
		
	public Circle getObjCircle() {
		return objCircle;
	}
	public void setObjCircle(Circle objCircle) {
		this.objCircle = objCircle;
	}
	public Triangle getObjTriangle() {
		return objTriangle;
	}
	public void setObjTriangle(Triangle objTriangle) {
		this.objTriangle = objTriangle;
	}	
}

ShapeServiceProxy.java

package com.mugil.shapes;

public class ShapeServiceProxy extends ShapeService {
	
	public Circle getObjCircle() {
		new LoggingAspect().getLogMessage();
		return super.getObjCircle();
	}
}

DrawingApp.java

package com.mugil.shapes;

public class DrawingApp
 {
	public static void main(String[] args) 
        {
		FactoryService objFactSer = new FactoryService();
		ShapeService objSS = (ShapeService)objFactSer.getBean("ShapeService");
		objSS.getObjCircle();
	}
}

FactoryService.java

package com.mugil.shapes;

public class FactoryService {
	
	public Object getBean(String className)
	{
		if(className.equals("Circle"))
			return new Circle();
		else if(className.equals("Triangle"))
			return new Triangle();
		else if(className.equals("DrawingApp"))
			return new DrawingApp();
		else if(className.equals("ShapeService"))
			return new ShapeServiceProxy();
			
		return null;
	}
}

spring.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

	<aop:aspectj-autoproxy />
        <bean id="customerBo" class="CustomerBoImpl" />
	<bean name="objTriangle" class="com.mugil.shapes.Triangle">
         	<property name="name" value="Triangle Name"></property>
        </bean>
        <bean name="objCircle" class="com.mugil.shapes.Circle">
    	        <property name="name" value="Circle Name"></property>
        </bean>
        <bean id="shapeService" class="com.mugil.shapes.ShapeService" autowire="byName"/>    	
        <bean name="loggingAspect" class="com.mugil.shapes.LoggingAspect"/>
          <aop:config>
    	     <aop:aspect id="loggingAspect" ref="loggingAspect">
    		<aop:pointcut expression="execution(* com.mugil.shapes.*.get*())" id="allGetters"/>
    		<aop:around method="LoggingAdvice2" pointcut-ref="allGetters"/>
    	     </aop:aspect>
          </aop:config>
</beans>

spring.xml

<aop:aspect id="loggingAspect" ref="loggingAspect">

equivalent to

@Aspect
public class LoggingAspect {

spring.xml

<aop:pointcut expression="execution(* com.mugil.shapes.*.get*())" id="allGetters"/>

equivalent to

@Pointcut("execution(* com.mugil.shapes.*.get*())")
public void allGetters()
{	 
}

spring.xml

<aop:around method="LoggingAdvice2" pointcut-ref="allGetters"/>

equivalent to

@Around("allGetters()")
public void LoggingAdvice2(ProceedingJoinPoint pjp)
{
.
.

<aop:pointcut expression="execution(* com.mugil.shapes.*.get*())" id="allGetters"/>
<aop:around method="LoggingAdvice2" pointcut-ref="allGetters"/>

could be replaced using pointCut

<aop:around method="LoggingAdvice2" pointcut="execution(* com.mugil.shapes.*.get*())"/>
Posted in AOP.