Announcement Announcement Module
Collapse
No announcement yet.
What are the differences in these 2 simple controllers? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • What are the differences in these 2 simple controllers?

    Hello to all,

    I am studing the Spring MVC showcase example downloaded from the STS dashboard.

    I have a basic question for you:

    In my home.jsp view I have two links, something like that:

    Code:
    		<ul>
    			<li>
    				<a id="simpleLink" class="textLink" href="<c:url value="/simple" />">GET /simple</a>
    			</li>
    
    			<li>
    				<a id="simpleRevisited" class="textLink" href="<c:url value="/simple/revisited" />">GET /simple/revisited</a>
    			</li>
    So, in this example the first one is handled by a controller class named SimpleController and the second one is handled by a controller class named SimpleControllerRevisited

    This is the code of SimpleController class:

    Code:
    package org.springframework.samples.mvc.simple;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class SimpleController {
    
    	@RequestMapping("/simple")
    	public @ResponseBody String simple() {
    		return "Hello world!";
    	}
    	
    }
    As you can see the simple() method is annoted by @RequestMapping("/simple") annotation so it mean that this method handles request towards "/simple" folder. This method return a String to the caller and this string is encapsulated into the BODY section of the HTTP Response...ok it is very simple !!

    Now this is the code of SimpleControllerRevisited

    Code:
    package org.springframework.samples.mvc.simple;
    
    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.ResponseBody;
    
    @Controller
    public class SimpleControllerRevisited {
    
    	@RequestMapping(value="/simple/revisited", method=RequestMethod.GET, headers="Accept=text/plain")
    	public @ResponseBody String simple() {
    		return "Hello world revisited!";
    	}
    
    }
    It is very similar to the previus SimpleController() method. It handles request towards "/simple/revisited" folder and return a string into the BODY of the HTTP Response

    what is the difference between these two methods?

    When I do:
    method=RequestMethod.GET I'm specifying that the HTTP Request type is a GET...but is it not the default HTTP Request? Also in the other method is a GET although it is not explicitly specified? Or what?

    What do exactly the: headers="Accept=text/plain" parameter of the @RequestMapping annotation?

    thank you very much
    Andrea
    Last edited by AndreaNobili; Nov 16th, 2012, 03:08 PM.

  • #2
    I believe in the /simple example it will handle any request to /simple - get/post/etc.

    The /simple/revisited will only handle get request. It will not handle a POST.

    Comment

    Working...
    X