java Spring 3 MVC 嵌套 RequestMapping

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2237161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:13:40  来源:igfitidea点击:

Spring 3 MVC Nesting RequestMapping

javaspringspring-mvc

提问by Ph??ng Nguy?n

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.pdfIn page 448, they mentioned:

从 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();
    }
//...
}

(I have eliminated some code for readability) In such case, they claimed that a request to /appoinments/newwill invoke the getNewFormmethod. 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:

(为了可读性,我删除了一些代码)在这种情况下,他们声称请求/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;
    }
}

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

但是请求/basic.do/hello总是导致 404 错误。

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

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

回答by flybywire

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.dourl

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

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

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

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

顺便说一句,.html 扩展名比 .do 好,恕我直言