Thursday, 1 October 2015

@RequestParam Annotation Example


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>




10 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. 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
    Data Science training in Chennai | Data science training in bangalore
    Data science training in pune| Data science online training
    Python training in Kalyan nagar

    ReplyDelete
  3. 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.
    python training in tambaram | python training in annanagar | python training in jayanagar

    ReplyDelete
  4. 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.
    java training in jayanagar | java training in electronic city

    java training in chennai | java training in USA

    ReplyDelete
  5. 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. 
    Devops Training courses
    Devops Training in Bangalore
    Best Devops Training in pune
    Devops interview questions and answers

    ReplyDelete
  6. 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.
    AWS 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

    ReplyDelete
  7. 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..

    hardware 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

    ReplyDelete