RequestMapping for controllers for different servlets
I have a problem with my spring controller/request mapping struture. In web.xml I have defined 2 dispatcher servlets, that map the following request paths:
- Servlet: /pathA/*
- Servlet /pathB/*
All my controllers are defined in the package com.myproject.controllers, so both controllers serving for paths under /pathA/* and /pathB/*. I am doing a component-scan in both of my servlets. How do I need to set the ReuestMapping annotations for the following Controller:
@Controller
public class MyController {
// /pathA
public void action1() {
}
开发者_开发问答
// /pathA/action2
public void action2() {
}
}
I really get confused here, i have tried so many different things, I hope you can help me!
Sincerely, Heinrich
If you actually need to use several DispatcherServlet
, perhaps the best approach is to place their controllers into separate packages and limit component scan of each servlet to its own package.
If you can't do it for some reason, you can configure your servlets as follows:
<bean class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name = "alwaysUseFullPath" value = "true" />
</bean>
In this case you can use servlet paths in @RequestMapping
, as stas showed.
try smt like this
@RequestMapping(method = RequestMethod.GET, value = "/pathA")
@RequestMapping(method = RequestMethod.GET, value = "/pathA/bla-bla")
if it wouldn't help there is some variant with urlrewriters.
精彩评论