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>
This comment has been removed by the author.
ReplyDeleteThank you for this post. Thats all I are able to say. You most absolutely have built this blog website into something speciel. You clearly know what you are working on, youve insured so many corners.thanks
ReplyDeleteData Science training in Chennai | Data science training in bangalore
Data science training in pune| Data science online training
Python training in Kalyan nagar
After reading your post I understood that last week was with full of surprises and happiness for you. Congratz! Even though the website is work related, you can update small events in your life and share your happiness with us too.
ReplyDeletepython training in tambaram | python training in annanagar | python training in jayanagar
After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
ReplyDeletejava training in jayanagar | java training in electronic city
java training in chennai | java training in USA
Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up.
ReplyDeleteDevops Training courses
Devops Training in Bangalore
Best Devops Training in pune
Devops interview questions and answers
Greetings. I know this is somewhat off-topic, but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.
ReplyDeleteAWS Interview Questions And Answers
AWS Tutorial |Learn Amazon Web Services Tutorials |AWS Tutorial For Beginners
AWS Online Training | Online AWS Certification Course - Gangboard
AWS Training in Toronto| Amazon Web Services Training in Toronto, Canada
This looks absolutely perfect.i like it a lot. Android Training in Chennai | Certification | Mobile App Development Training Online | Android Training in Bangalore | Certification | Mobile App Development Training Online | Android Training in Hyderabad | Certification | Mobile App Development Training Online | Android Training in Coimbatore | Certification | Mobile App Development Training Online | Android Training in Online | Certification | Mobile App Development Training Online
ReplyDeleteThe strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
ReplyDeletejava training in chennai
java training in annanagar
aws training in chennai
aws training in annanagar
python training in chennai
python training in annanagar
selenium training in chennai
selenium training in annanagar
Thanks, Learned a lot of new things from your post! Good creation and HATS OFF to the creativity of your mind.
ReplyDeletedata science training in chennai
data science training in tambaram
android training in chennai
android training in tambaram
devops training in chennai
devops training in tambaram
artificial intelligence training in chennai
artificial intelligence training in tambaram
Thank you for this post. Thats all I are able to say. You most absolutely have built this blog website into something speciel. You clearly know what you are working on, youve insured so many corners.thanks..
ReplyDeletehardware and networking training in chennai
hardware and networking training in omr
xamarin training in chennai
xamarin training in omr
ios training in chennai
ios training in omr
iot training in chennai
iot training in omr