=> Based on this Annotation only Request will come to any method in the Controller.
=> We can mention that , the method is supporting either POST request or GET request
=> We can mention the value of the url
(Ex : http://ip:port/projName/..../value)
For each method we have to differentiate the value
=> We can append variables with URL as Path Variables
=> We can append variables with URL as Request Parameters
Annotation
@RequestMapping
import org.springframework.web.bind.annotation.RequestMapping;
How to specify the method is only supports either POST / GET ?
@RequestMapping(value="/callController1", method=RequestMethod.GET)
@RequestMapping(value="/callController1", method=RequestMethod.POST)
@RequestMapping(value="/callController1", method={RequestMethod.GET,RequestMethod.POST})
[It will support both GET and POST Requests]
@PathVariable Example
@RequestMapping(value="/callController2/{name}",method=RequestMethod.GET)
public ModelAndView method2(@PathVariable("name") String person) {
// Write the Code whatever you want
}
Ex URL : http://localhost:8888/SpringMVC/spring/callController2/Ram
For More Path variables
@RequestMapping(value="/callController2/{name}/one/{salary}",method=RequestMethod.GET)
Ex URL : http://localhost:8888/SpringMVC/spring/callController2/Ram/one/50000
@RequestParam with Params attribute
@RequestMapping(value="/callController3",params="id",method=RequestMethod.GET)
public ModelAndView method3(@RequestParam("id") String uid) {
// Code
}
Ex URL : http://localhost:8888/SpringMVC/spring/callController3?id=25
For Multiple Request Parameters
@RequestMapping(value="/callController5",params={"id","name"},method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView method5(@RequestParam("id") String uid,@RequestParam("name") String name) {
// Code
}
Ex URL : http://localhost:8888/SpringMVC/spring/callController5?id=25&name=Ram
Example :
package com.ram;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class SpringMvcController {
//http://localhost:8888/SpringMVC/spring/springController
//By default it will support GET method
@RequestMapping(value="/springController")
public String method() {
return "returnPage";
}
//http://localhost:8888/SpringMVC/spring/callController1
@RequestMapping(value="/callController1",method=RequestMethod.GET)
public String method1(ModelMap model) {
model.addAttribute("key1", "Hi Ram");
return "returnPage1";
}
//http://localhost:8888/SpringMVC/spring/callController2/Ram
@RequestMapping(value="/callController2/{name}",method=RequestMethod.GET)
public ModelAndView method2(@PathVariable("name") String person) {
ModelAndView model = new ModelAndView();
model.setViewName("returnPage2");
model.addObject("msg1", person);
model.addObject("msg2", "This is Message2");
return model;
}
//http://localhost:8888/SpringMVC/spring/callController3?id=25
//It will suppoert Both POST and GET
@RequestMapping(value="/callController3",params="id",method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView method3(@RequestParam("id") String uid) {
ModelAndView model = new ModelAndView();
model.setViewName("returnPage3");
model.addObject("msg1", uid);
return model;
}
//http://localhost:8888/SpringMVC/spring/callController4
@RequestMapping(value="/callController4",method=RequestMethod.GET)
public ModelAndView method4() {
ModelAndView model = new ModelAndView("returnPage4");
model.addObject("msg5", "Hi Ram");
return model;
}
//http://localhost:8888/SpringMVC/spring/callController5?id=25&name=Ram
//It will suppoert Both POST and GET
@RequestMapping(value="/callController5",params={"id","name"},method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView method5(@RequestParam("id") String uid,@RequestParam("name") String name) {
ModelAndView model = new ModelAndView();
model.setViewName("returnPage5");
model.addObject("id", uid);
model.addObject("name", name);
return model;
}
}
=> We can mention that , the method is supporting either POST request or GET request
=> We can mention the value of the url
(Ex : http://ip:port/projName/..../value)
For each method we have to differentiate the value
=> We can append variables with URL as Path Variables
=> We can append variables with URL as Request Parameters
Annotation
@RequestMapping
import org.springframework.web.bind.annotation.RequestMapping;
How to specify the method is only supports either POST / GET ?
@RequestMapping(value="/callController1", method=RequestMethod.GET)
@RequestMapping(value="/callController1", method=RequestMethod.POST)
@RequestMapping(value="/callController1", method={RequestMethod.GET,RequestMethod.POST})
[It will support both GET and POST Requests]
@PathVariable Example
@RequestMapping(value="/callController2/{name}",method=RequestMethod.GET)
public ModelAndView method2(@PathVariable("name") String person) {
// Write the Code whatever you want
}
Ex URL : http://localhost:8888/SpringMVC/spring/callController2/Ram
For More Path variables
@RequestMapping(value="/callController2/{name}/one/{salary}",method=RequestMethod.GET)
Ex URL : http://localhost:8888/SpringMVC/spring/callController2/Ram/one/50000
@RequestParam with Params attribute
@RequestMapping(value="/callController3",params="id",method=RequestMethod.GET)
public ModelAndView method3(@RequestParam("id") String uid) {
// Code
}
Ex URL : http://localhost:8888/SpringMVC/spring/callController3?id=25
For Multiple Request Parameters
@RequestMapping(value="/callController5",params={"id","name"},method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView method5(@RequestParam("id") String uid,@RequestParam("name") String name) {
// Code
}
Ex URL : http://localhost:8888/SpringMVC/spring/callController5?id=25&name=Ram
Example :
package com.ram;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class SpringMvcController {
//http://localhost:8888/SpringMVC/spring/springController
//By default it will support GET method
@RequestMapping(value="/springController")
public String method() {
return "returnPage";
}
//http://localhost:8888/SpringMVC/spring/callController1
@RequestMapping(value="/callController1",method=RequestMethod.GET)
public String method1(ModelMap model) {
model.addAttribute("key1", "Hi Ram");
return "returnPage1";
}
//http://localhost:8888/SpringMVC/spring/callController2/Ram
@RequestMapping(value="/callController2/{name}",method=RequestMethod.GET)
public ModelAndView method2(@PathVariable("name") String person) {
ModelAndView model = new ModelAndView();
model.setViewName("returnPage2");
model.addObject("msg1", person);
model.addObject("msg2", "This is Message2");
return model;
}
//http://localhost:8888/SpringMVC/spring/callController3?id=25
//It will suppoert Both POST and GET
@RequestMapping(value="/callController3",params="id",method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView method3(@RequestParam("id") String uid) {
ModelAndView model = new ModelAndView();
model.setViewName("returnPage3");
model.addObject("msg1", uid);
return model;
}
//http://localhost:8888/SpringMVC/spring/callController4
@RequestMapping(value="/callController4",method=RequestMethod.GET)
public ModelAndView method4() {
ModelAndView model = new ModelAndView("returnPage4");
model.addObject("msg5", "Hi Ram");
return model;
}
//http://localhost:8888/SpringMVC/spring/callController5?id=25&name=Ram
//It will suppoert Both POST and GET
@RequestMapping(value="/callController5",params={"id","name"},method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView method5(@RequestParam("id") String uid,@RequestParam("name") String name) {
ModelAndView model = new ModelAndView();
model.setViewName("returnPage5");
model.addObject("id", uid);
model.addObject("name", name);
return model;
}
}
spring-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.ram" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Web.xml
<web-app> <display-name>Spring MVC</display-name> <welcome-file-list> <welcome-file>home.jsp</welcome-file> </welcome-file-list>
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/spring/*</url-pattern> </servlet-mapping> </web-app>
returnPage.jsp
<html> <body> <h1>Spring MVC</h1> </body> </html>
returnPage1.jsp
<html> <body> <h1>Spring MVC</h1> <h2>${key1}</h2> </body> </html>
returnPage2.jsp
<html> <body> <h1>Spring MVC</h1> <h2>${msg1}</h2> <h2>${msg2}</h2> </body> </html>
returnPage3.jsp
<html> <body> <h1>Spring MVC</h1> <h2>${msg1}</h2> </body> </html>
returnPage4.jsp
<html> <body> <h1>Spring MVC</h1> <h2>${msg5}</h2> </body> </html>
returnPage5.jsp
<html> <body> <h1>Spring MVC</h1> <h2>Id : ${id}</h2> <h2>Name : ${name}</h2> </body> </html>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.ram" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Web.xml
<web-app> <display-name>Spring MVC</display-name> <welcome-file-list> <welcome-file>home.jsp</welcome-file> </welcome-file-list>
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/spring/*</url-pattern> </servlet-mapping> </web-app>
returnPage.jsp
<html> <body> <h1>Spring MVC</h1> </body> </html>
returnPage1.jsp
<html> <body> <h1>Spring MVC</h1> <h2>${key1}</h2> </body> </html>
returnPage2.jsp
<html> <body> <h1>Spring MVC</h1> <h2>${msg1}</h2> <h2>${msg2}</h2> </body> </html>
returnPage3.jsp
<html> <body> <h1>Spring MVC</h1> <h2>${msg1}</h2> </body> </html>
returnPage4.jsp
<html> <body> <h1>Spring MVC</h1> <h2>${msg5}</h2> </body> </html>
returnPage5.jsp
<html> <body> <h1>Spring MVC</h1> <h2>Id : ${id}</h2> <h2>Name : ${name}</h2> </body> </html>