UrlMappingCtl.java
package in.co.sunrays.ctl;
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;
/**
*
* An example that maps URLs with controller class and its methods. A controller
* class or its individual methods are mapped with URL using @RequestMapping
* annotation.
*
* Mapping can be bound with HTTP POST and GET methods.
*
* @author SUNRAYS Technologies
* @version 1.0
* @Copyright (c) SUNRAYS Technologies
*/
@Controller
// URL /UrlMapping is mapped with entire class
@RequestMapping(value = "/UrlMapping")
public class UrlMappingCtl {
/**
* HTTP GET method is bound with display() method. URL to call this method
* will be http://localhost:8080/STMavenSpringMVC/UrlMapping is called from
* browser.
*
* @return View Name
*/
@RequestMapping(method = RequestMethod.GET)
public String display() {
System.out.println("I am in display method");
return "UrlMappingView";
}
/**
* HTTP POST method is bound with submit() method. This method will be
* called when data is submitted to action="/UrlMapping" from a HTML FORM using
* POST method.
*
* <form action="/UrlMapping" method="POST" >...</form>
*
* @return View Name
*/
@RequestMapping(method = RequestMethod.POST)
public String submit() {
System.out.println("I am in submit method");
return "UrlMappingView";
}
/**
*
* Method is bound with relative url "/search", that maps method with
* "/UrlMapping/search" URL.
*
* HTTP POST and GET both methods are bound to search() method.
*
*
* @return View Name
*/
@RequestMapping(value = "/search", method = { RequestMethod.POST,
RequestMethod.GET })
public String search() {
System.out
.println("I am in search method, it handles GET and POST methods.");
return "TestSearchView";
}
/**
* Example method to receive request parameter using @RequestParam
* annotation.
*
* Using @RequestParam annotation you can receive as many parameters you
* want.
*
* Use http://localhost:8080/STMavenSpringMVC/TestUrl/param
*
* @return View Name
*/
@RequestMapping(value = "/param", method = RequestMethod.GET)
public String requestParam(@RequestParam String message) {
System.out.println("Request Parameter : " + message);
return "UrlMappingView";
}
}