How To Pass Multiple Parameters To Spring MVC Controller

This example shows how to pass the multiple parameters to the Spring controller. In our previous article, I have explained how to use the @PathVariable to pass the parameters to the controller. This example shows a simple example on how to pass multiple parameters using the @PathVariable.

1. Spring Controller

HelloController.java

package javabeat.net.spring.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloController {
	@RequestMapping(value = "/welcome/{userId}/{userName}/", method = RequestMethod.GET)
	public String printWelcome(@PathVariable("userId") String userId,
			@PathVariable("userName") String userName, ModelMap model,
			HttpServletRequest request) {
		System.out.println("User Id : " + userId);
		System.out.println("User Name : " + userName);
		model.addAttribute("msg", userId);
		return "hello";
	}

}

2. Spring Configuration

spring-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:websocket="http://www.springframework.org/schema/websocket"
    xmlns:context="http://www.springframework.org/schema/context"
    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/websocket


http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd">

	<context:component-scan base-package="javabeat.net.spring.controller" />
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/jsp/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
</beans>

3. Web.xml

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>Spring MVC 4.0 Web Application</display-name>
	<servlet>
		<servlet-name>spring-dispatcher</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>spring-dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

4. Demo

You can run the above example by accessing the URL localhost:8080/SpringExamples/welcome/krishnas/Krishna/. Now the values “krishnas” and “krishna” will be passed to the controller.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*