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
.
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.
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 .
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.
@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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With