@RequestMapping Example in Spring MVC Framework

@RequestMapping Example in Spring

In this example, We will show you about, @RequestMapping Example in Spring MVC Framework.

Mapping URL using @RequestMapping

package com.dineshkrish.spring;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Example1 {

	@RequestMapping(value = "login")
	public String login(HttpServletRequest request, HttpServletResponse response)  {
		
		String result = "";
		
		// do something
		
		return result;
	}
}

Creating POST or GET request using @RequestMapping

package com.dineshkrish.spring;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

@Controller
public class Example1 {

	@RequestMapping(value = "authenticate", method = RequestMethod.GET /* RequestMethod.POST */ )
	public String authenticate(HttpServletRequest request, HttpServletResponse response)  {
		
		String result = "";
		
		// do something
		
		return result;
	}
}

Mapping Multiple URL using @RequestMapping

package com.dineshkrish.spring;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

@Controller
public class Example1 {

	@RequestMapping(value = {"login", "autenticate", "authenticate"} , method = RequestMethod.POST )
	public String verifyUser(HttpServletRequest request, HttpServletResponse response)  {
		
		String result = "";
		
		// do something
		
		return result;
	}
}

References

1. @RequestMapping JavaDoc

Hello, folks, I am a founder of dineshkrish.com. I love Java and Open source technologies, If you find my tutorials are useful, please consider making donations to these charities.