Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3 MVC Nesting RequestMapping

From Spring Official Document, Spring 3 MVC look to be support nesting Request Mapping. http://static.springsource.org/spring/docs/3.0.0.RELEASE/spring-framework-reference/pdf/spring-framework-reference.pdf In page 448, they mentioned:

@Controller
@RequestMapping("/appointments")
public class AppointmentsController {
//...
    @RequestMapping(value="/new", method = RequestMethod.GET)
    public AppointmentForm getNewForm() {
        return new AppointmentForm();
    }
//...
}

(I have eliminated some code for readability) In such case, they claimed that a request to /appoinments/new will invoke the getNewForm method. However, it doesn't work with my local Google App Engine server (though GAE server works just fine with mapping that are not nested). I create an example controller like below:

@Controller
@RequestMapping("/basic.do")
public class HelloWorldController {
    @RequestMapping(value="/hello", method=RequestMethod.GET)
    public ModelAndView helloWorld() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("basic/helloWorld");
        mav.addObject("message", "Hello World From Phuong!");
        return mav;
    }
}

but a request to /basic.do/hello always results in 404 error.

Wonder if anything wrong there? I'm using annotation-driven mode with *.do request handled by spring DispatchServlet.

like image 988
Phương Nguyễn Avatar asked Feb 10 '10 13:02

Phương Nguyễn


People also ask

What is the use of @RequestMapping in Spring MVC?

One of the most important annotations in spring is the @RequestMapping Annotation which is used to map HTTP requests to handler methods of MVC and REST controllers. In Spring MVC applications, the DispatcherServlet (Front Controller) is responsible for routing incoming HTTP requests to handler methods of controllers.

What is @RequestMapping annotation in Spring MVC?

RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods. Today we will look into various usage of this annotation with example and other annotations @PathVariable and @RequestParam .

Is @RequestMapping mandatory at class level?

A @RequestMapping on the class level is not required. Without it, all paths are simply absolute, and not relative. This means if you specify the class level annotations, the URL shall be relative, it shall be http://localhost:8080/users/user (URL to Handler mapping) and likewise.

What is @RequestMapping and GetMapping?

@RequestMapping is used at the class level while @GetMapping is used to connect the methods. This is also an important Spring MVC interview question to knowing how and when to use both RequestMapping and GetMapping is crucial for Java developers.


1 Answers

try this

@Controller
@RequestMapping("/basic")
public class HelloWorldController {
    @RequestMapping(value="/hello.do", method=RequestMethod.GET)
    public ModelAndView helloWorld() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("basic/helloWorld");
        mav.addObject("message", "Hello World From Phuong!");
        return mav;
    }
}

and try with the basic/hello.do url

The reason is that /basic.do/hello is not going to be handled by your dispatcher servlet as it is not an URL that ends in .do

BTW, .html extensions are nicer than .do, IMHO

like image 100
flybywire Avatar answered Oct 14 '22 09:10

flybywire