Spring 3 MVC嵌套RequestMapping

Phư*_*yễn 5 java spring spring-mvc

从Spring官方文档来看,Spring 3 MVC看起来是支持嵌套请求映射. http://static.springsource.org/spring/docs/3.0.0.RELEASE/spring-framework-reference/pdf/spring-framework-reference.pdf 在第448页,他们提到:

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

(我已经删除了一些可读性代码)在这种情况下,他们声称请求/appoinments/new将调用该getNewForm方法.但是,它不能与我的本地Google App Engine服务器一起使用(尽管GAE服务器可以正常使用非嵌套的映射).我创建一个示例控制器,如下所示:

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是请求/basic.do/hello始终导致404错误.

不知道有什么不对吗?我正在使用注释驱动模式,*.do请求由spring处理DispatchServlet.

fly*_*ire 14

试试这个

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

并尝试使用basic/hello.do网址

原因是/basic.do/hello您的调度程序servlet不会处理它,因为它不是以.do结尾的URL

BTW,.html扩展名比.do,恕我直言

  • 没有任何扩展比.html更好 (9认同)
  • 实际上"没有扩展名"比.html更好;) (4认同)
  • 而你夸张:) (3认同)