spring mvc注解总结-@controller

为什么我们要使用注解呢?
实现controller层不可以实现多个响应请求,使用@controll可以达到这个目的

spring使用扫描机制来找到应用程序中所有局域注解的控制器类。
为了达到这个当让要告知其扫面路径:

<context:component-scan base-package=""/>

requestmapping将uri映射到方法上
value:设置相应路径
method:前端的请求方式
如果只有一个路径响应可以使用:

@RequestMapping("/路径")
@RequestMapping(value ="/"." /a")

使用路径变量的方法:

RequestMapping("/路径/{userId}/{orderId                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }")
public void getById(@PathVariable Long id,Model model){
//  调用该方式的时候,URL请求中的id值将被复制到路径变量中,并且可以在方法中使用
}

也可以使用@ModelAttribute注解的方法返回一个对象或者一个void类型。如果返回一个对象,则返回对象会自动以键值队的方式存放在model中;原因则是:在每次调用请求方法之前,spring会有限调用带@modelatrribute注解的方法


//只有一个对象要返回的写法
@ModelAttribute
public entity method(@RequestParam String id){
//  随后会自行加入到model中
    return entity.get(id);
}
//没有对象要返回的写法
public entity method(@ModelAttribute Entity entity,Model ){
//  必须添加一个model类型的参数,随后会自行加入到model中
    your handle access;

}
//有多个对象要返回的时候
public entity method(@ModelAttribute Entity entity,@ModelAtrribute Entity_01,Model model){
    your handle access;
}

猜你喜欢

转载自blog.csdn.net/wangzhonglinqwe/article/details/79976166