@PathVariable
Annotation
·
It will Read the URL data
§
ex :
goggle.com/INDIA/Satya
§
ex :
goggle.com/PAKISTHAN/ShahidAfridi
§
format : goggle.com/ {countryName}/{userName}
·
If We want to read the URL data in our java
class we can use @PathVarible at method level
·
We can read each parameter data by using à @PathVariable("countryName")
String cn
·
We can read all paramrers data by Map à @PathVariable Map<String,String>
pathVars
Example:
Web.xml
<?xml version="1.0"
encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>9FirstSpringMVCProject</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-dispatcher-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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="hellocontroller"
/>
<mvc:annotation-driven/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
HelloController.java
package hellocontroller;
import java.util.Map;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.PathVariable;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping("/welcomeMap/{countryName}/{userName}")
public ModelAndView helloMap(@PathVariable
Map<String,String> pathVars) {
String name = pathVars.get("userName");
String country = pathVars.get("countryName");
ModelAndView model = new ModelAndView("HelloPage");
model.addObject("msg","hello "+name+ " You are from
"+country);
return model;
}
@RequestMapping("/welcomeString/{countryName}/{userName}")
public ModelAndView helloString(@PathVariable("countryName") String cn, @PathVariable("userName") String un){
ModelAndView model = new ModelAndView("HelloPage");
model.addObject("msg", "COUNTRY NAME
: "+cn+"
=======>USERNAME : "+un);
return model;
}
}
HelloPage.jsp
<html>
<body>
<h1>First Spring MVC Application Demo</h1>
<h2>${msg}</h2>
</body>
</html>