如何在Spring MVC中映射多个控制器 [英] How to map Multiple controllers in Spring MVC

查看:125
本文介绍了如何在Spring MVC中映射多个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有两个控制器;一个是userController,其中有添加,删除和更新方法;另一个是studentController,我也有添加,删除和更新方法.

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.

在我的方法中,两个控制器中都使用@RequestMapping批注,所有映射都是相同的.我有一个困惑:如果我们要从JSP传递相同的动作,那么分派器将如何找到相应的控制器?如果有人可以使用示例进行描述,将不胜感激.

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.

推荐答案

您必须在类级别设置一个@RequestMapping批注,该批注的值将成为所有发送给该控制器的请求的前缀,
例如:

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) {
        ...
    }
}

和一名学生负责人

@Controller
@RequestMapping("student")
public class StudentController {

    @RequestMapping("edit")
    public ModelAndView edit(@RequestParam(value = "id", required = false) Long id, Map<String, Object> model) {
        ...
    }
}

两个控制器具有相同的方法,具有相同的请求映射,但是您可以通过以下uri访问它们:

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

这篇关于如何在Spring MVC中映射多个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆