Java – Spring 3 MVC Nesting RequestMapping

javaspringspring-mvc

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.

Best Answer

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

Related Topic