Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Spring web mvc: Map request parameters

@RequestParam is used to bound the request parameter to the method argument.

@RequestMapping("welcome")
public Modelandview getWelcomeMessage(@RequestParam("userName") String name, @RequestParam("age") int userAge) {

}

If you hit the url, ‘{APPLICATION_URL}?userName=krishna&age=28’, then username is mapped to the method parameter name and age is mapped to the method parameter userAge.

Find the below working application.

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

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
@RequestMapping("greeting")
public class HelloWorldController {

@RequestMapping("welcome")
public ModelAndView getWelcomeMessage(@RequestParam("userName") String name, @RequestParam("age") int userAge) {

String msg = String.format("Hi %s, you are %d years old", name, userAge);

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"%>



http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
Hello World Spring Web MVC


${message}




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

web.xml
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">

Spring MVC Hello WorldApplication


HelloWorld
org.springframework.web.servlet.DispatcherServlet
1



HelloWorld
/





HelloWorld-servlet.xml
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">


base-package="com.sample.myApp" />


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





Create index.jsp file under webapp folder.


index.jsp


Hello World!




Project structure looks like below.


Run the application on server and hit below url.
http://localhost:8080/springdemo/greeting/welcome?userName=krishna&age=28





Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Spring web mvc: Map request parameters

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×