Friday 23 November 2018

Spring web mvc: Map request parameters to a map object

This is continuation to my previous post. In this post, I am going to show you, how can you map request parameters to a 'java.util.Map' object.

@RequestMapping(value = "welcome", method = RequestMethod.POST)
public ModelAndView getWelcomeMessage(@RequestParam("userName") String name, @RequestParam("age") int userAge) {
         .....
         .....
}

See the above snippet, it maps the request parameter userName to the method parameter name and age to the method parameter userAge.

What if your request has 10 parameters, will you write a method with 10 arguments. What if your request has dynamic parameters. In those cases, we can map request parameters to a map like below.

@RequestMapping(value = "welcome", method = RequestMethod.POST)
public ModelAndView getWelcomeMessage(@RequestParam Map<String, String> requestParamMap) {

}

Find the below working application.

HelloWorldController.java
package com.sample.myApp.controllers;

import java.util.Map;

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;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("greeting")
public class HelloWorldController {

 @RequestMapping(value = "welcome", method = RequestMethod.POST)
 public ModelAndView getWelcomeMessage(@RequestParam Map<String, String> requestParamMap) {

  String name = requestParamMap.get("userName");
  int age = Integer.parseInt(requestParamMap.get("age"));
  String msg = String.format("Hi %s, you are %d years old", name, age);

  ModelAndView modelAndView = new ModelAndView("hello");
  modelAndView.addObject("message", msg);
  return modelAndView;
 }

}


Create hello.jsp file under WEB-INF/jsp folder.

hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World Spring Web MVC</title>
</head>
<body>
 <h2>${message}</h2>
</body>
</html>


Create web.xml, HelloWorld-servlet.xml files under WEB-INF folder.

web.xml
<web-app id="WebApp_ID" version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <display-name>Spring MVC Hello WorldApplication</display-name>

 <servlet>
  <servlet-name>HelloWorld</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>HelloWorld</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

</web-app>


HelloWorld-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"
 xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:component-scan
  base-package="com.sample.myApp" />

 <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>

</beans>

Create index.jsp file under webapp folder.

index.jsp

<html>
<head>
<title>User Information Page</title>
</head>
<div id="login_form">
 <form method="post" action="/springdemo/greeting/welcome"
  id="f1">
  <table>
   <tr>
    <td class="f1_label">User Name :</td>
    <td><input type="text" name="userName" value="" /></td>
   </tr>
   <tr>
    <td class="f1_label">Age :</td>
    <td><input type="text" name="age" value="" /></td>
   </tr>
   <tr>
    <td><input type="submit" name="submit" value="submit"
     style="font-size: 18px;" /></td>
   </tr>
  </table>
 </form>
</div>
</html>


Project structure looks like below.
Run the application on server. You can able to see below form.


Give the User Name as ‘krishna’ and age as 29 hit the submit button. You can able to see below kind of output.




Previous                                                 Next                                                 Home

No comments:

Post a Comment