@RequestParam annotations
· If we want to get the form data we use this
· You must take care about the names
· For getting form data one by one use à @RequestParam("sname") String name
· For getting form data all at once use à @RequestParam Map<String, String> map
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>AnnoEx</display-name>
<welcome-file-list>
<welcome-file>/WEB-INF/index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</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.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">
<!-- 1.handlerMapping Repalcement -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 2. Controolers Base Pkg -->
<context:component-scan base-package="controller"></context:component-scan>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
Index.jsp
<h1>Student Registartion Form</h1>
<form action="register">
Reg. No : <input type="text" name="regno"><br/>
Name : <input type="text" name="sname"><br/>
DOB : <input type="text" name="dob"><br/>
<button type="submit">REGISTER</button><br/>
</form>
|
StudentController.java
package controller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class StudentController {
@RequestMapping("/register")
public ModelAndView getData(@RequestParam("sname") String name,@RequestParam("regno") String regno,@RequestParam("dob") String dob ){
ModelAndView model = new ModelAndView("Hello");
String msg = "Name : "+name+" <br/> REG. No: "+regno+" <br/> DOB : "+dob;
model.addObject("msg", msg);
return model;
}
@RequestMapping("/registerMap")
public ModelAndView getData(@RequestParam Map<String, String> map){
ModelAndView model = new ModelAndView("Hello");
String name = map.get("sname");
String regno = map.get("regno");
String dob = map.get("dob");
String msg = "*********** MAP ************* <br/>Name : "+name+" <br/> REG. No: "+regno+" <br/> DOB : "+dob;
model.addObject("msg", msg);
return model;
}
}
Hello.jsp
<h1>${msg}</h1>
Output
Name : Satya
REG. No: 101
DOB : 25-JAN-1990
Output