Reputation: 187
I have two controllers in my Application; one is userController
, where I have add, delete and update methods; the other one is studentController
, where I also have add, delete and update methods.
All the mappings are same in my methods using @RequestMapping
annotation in both controllers. I have one confusion: if we are passing the same action from the JSP, then how will the Dispatcher find the corresponding controller? If anybody could describe this using example will be appreciated.
Upvotes: 16
Views: 36193
Reputation: 627
We can have any number of controllers, the URL mapping will decide which controller to call..
Please refer here for detailed Spring MVC multiple Controller example
Upvotes: 0
Reputation: 9649
You have to set a @RequestMapping
annotation at the class level the value of that annotation will be the prefix of all requests coming to that controller,
for example:
you can have a user controller
@Controller
@RequestMapping("user")
public class UserController {
@RequestMapping("edit")
public ModelAndView edit(@RequestParam(value = "id", required = false) Long id, Map<String, Object> model) {
...
}
}
and a student controller
@Controller
@RequestMapping("student")
public class StudentController {
@RequestMapping("edit")
public ModelAndView edit(@RequestParam(value = "id", required = false) Long id, Map<String, Object> model) {
...
}
}
Both controller have the same method, with same request mapping but you can access them via following uris:
yourserver/user/edit
yourserver/student/edit
hth
Upvotes: 37