Example 1 :
@RequestMapping(value="call31" ,params="name", method=RequestMethod.GET)
public String method1(@RequestParam("name") String name) {
return "page1";
}
Example URL :
http://localhost:8888/SpringMVC/spring/call31?name=Ram
==========================================
Example 2 :
@RequestMapping(value="call32" ,params={"name","no"}, method=RequestMethod.GET)
public String method2(@RequestParam("name") String name,@RequestParam("no") String no) {
return "page1";
}
Example URL :
http://localhost:8888/SpringMVC/spring/call32?name=Ram&no=25
================================================
Example 3 : With POST method
@RequestMapping(value="call33Submit" ,params="sub", method=RequestMethod.POST)
public String method4(@RequestParam("sub") String name) {
return "page3Result";
}
This is POST method , so to call this method we need to add extra parameter as name="sub" for button or sumbit button , then only we can call this method
JSP :
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<body>
<h1>From page3.jsp</h1>
<form:form action="./call33Submit" method="POST">
<input type="submit" name="sub" value="Submit"/>
</form:form>
</body>
</html>
================================================
Full Example :
web.xml :
<servlet>
<servlet-name>spring</servlet-name>
<!-- By default it will search for spring-servlet.xml file -->
<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>
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>
Controller :
package com.ram;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class RequestParamExample {
//http://localhost:8888/SpringMVC/spring/call31?name=Ram
@RequestMapping(value="call31" ,params="name", method=RequestMethod.GET)
public String method1(@RequestParam("name") String name) {
return "page1";
}
//http://localhost:8888/SpringMVC/spring/call32?name=Ram&no=25
@RequestMapping(value="call32" ,params={"name","no"}, method=RequestMethod.GET)
public String method2(@RequestParam("name") String name,@RequestParam("no") String no) {
return "page1";
}
@RequestMapping(value="call33" , method=RequestMethod.GET)
public String method3() {
return "page3";
}
//to call this method add extra parameter as name="sub" for button or sumbit button then only this method will call
@RequestMapping(value="call33Submit" ,params="sub", method=RequestMethod.POST)
public String method4(@RequestParam("sub") String name) {
System.out.println("name : "+name);
return "page3Result";
}
}
page1.jsp :
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<body>
<h1>From page1.jsp</h1>
</body>
</html>
page3.jsp :
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<body>
<h1>From page3.jsp</h1>
<form:form action="./call33Submit" method="POST">
<input type="submit" name="sub" value="Submit"/>
</form:form>
</body>
</html>
page3Result.jsp :
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<body>
<h1>From page3Result.jsp</h1>
</body>
</html>