1、一些简单的常用注解的含义
@RequestMapping注解为控制器指定可以处理哪些 URL 请求
@Resource和@Autowired 都是做bean的注入时使用
@RequestBody该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上 @ResponseBody该注解用于将Controller的方法返回的对象,通过适HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区
@ModelAttribute 在方法定义上使Spring MVC在调用目标处理方法前,会先逐个调用在方法级上标注了, 的方法 在方法的入参前使用
@ModelAttribute 注解:可以从隐含对象中获取隐含的模型数据中获取对象,再将请求参数–绑定到对象中,再传入入参将方法入参对象添加到模型中
@RequestParam在处理方法入参处使用 可以把请求参 数传递给请求方法
@PathVariable 绑定 URL 占位符到入参
@ExceptionHandler 注解到方法上,出现异常时会执行该方法
@ControllerAdvice 使一个Contoller成为全局的异常处理类,类中用
@ExceptionHandler方法注解的方法可以处理所有Controller发生的异常
2、RESTFul风格的SringMVC
后台Controller层
@Controller
@RequestMapping("/rest")
public class RestController {
@RequestMapping(value="/user/{id}",method=RequestMethod.GET)
public String get(@PathVariable("id") Integer id){
System.out.println("get"+id);
return "/hello";
}
}
前端form表单发送put和delete请求
<form action="rest/user/1" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="put">
</form>
<form action="rest/user/1" method="get">
<input type="submit" value="get">
</form>
<form action="rest/user/1" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="delete">
</form>