Thursday 1 October 2015

@Path Variable Annotation Examples

Example 1 : 

@RequestMapping(value="call21/{name}" , method=RequestMethod.GET)
public String method1(@PathVariable("name") String name) {

return "page1";
}

Example URL :

http://localhost:8888/SpringMVC/spring/call21/Ram

=====================================

Example 2 : 

@RequestMapping(value="call22/{name}/{no}" , method=RequestMethod.GET)
public String method2(@PathVariable("name") String name,@PathVariable("no") String no) {
return "page1";
}


Example URL : 

http://localhost:8888/SpringMVC/spring/call22/Ram/25

=======================================

Example 3 : 

@PathVariable with POST method : 

@RequestMapping(value="call2222/{name}/{no}" , method=RequestMethod.POST)
public String method3(@PathVariable("name") String name,@PathVariable("no") String no) {
return "page1";
}

This is POST method , so Passing extra Path variables from jsp as 

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<body>
<h1>From page8.jsp</h1>
<form:form action="./call2222/Ram/21" method="POST">
<input type="submit" value="Submit"/>
</form:form>
</body>
</html>

==============================================

Full Example :

Web.xml : 

<web-app>

<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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class PathVariableEx {


//http://localhost:8888/SpringMVC/spring/call21/Ram
@RequestMapping(value="call21/{name}" , method=RequestMethod.GET)
public String method1(@PathVariable("name") String name) {

return "page1";
}

//http://localhost:8888/SpringMVC/spring/call22/Ram/25
@RequestMapping(value="call22/{name}/{no}" , method=RequestMethod.GET)
public String method2(@PathVariable("name") String name,@PathVariable("no") String no) {

return "page1";
}

@RequestMapping(value="call222" , method=RequestMethod.GET)
public String method222() {

return "page8";
}

//use like this in jsp page <form:form action="./call2222/Ram/21" method="POST"> to call this url with Path varilable
@RequestMapping(value="call2222/{name}/{no}" , method=RequestMethod.POST)
public String method3(@PathVariable("name") String name,@PathVariable("no") String no) {

return "page1";
}



}

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>

page8.jsp

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<body>
<h1>From page8.jsp</h1>
<form:form action="./call2222/Ram/21" method="POST">
<input type="submit" value="Submit"/>
</form:form>
</body>

</html>

No comments:

Post a Comment