Spring MVC的几个注解

1、@RestController与@Controller。

这两个注解用于声明WEB控制器,那么区别在哪儿呢?

我们知道使用Spring MVC,一个方法对应一个url,然后相应处理后返回对应的视图,但有时候不会返回视图,返回的是JSON数据,那么我们需要在方法上加上@ResponseBody

像下面这样:

@RequestMapping("/checkUser")
@ResponseBody
public Object checkUser(String username) {
	//code
	return new User();
}
而使用@RestController相当于在类上加了@Controller和@ResponseBody,这样会在每个方法上都加上@ResponseBody。如果想要在@RestController标识的类上返回视图,使用ModelAndView返回。


2、@RequestParam和@RequestBody

@RequestMapping("/test1/testRequestParam")
public String requestParam(@RequestParam("id") Integer id) {
	System.out.println(id);
	return "test1/requestParam";
}
加上@RequestParam 那么访问这个url就必须带上id参数。可设置required为false关闭参数是否必须。

否则会抛出org.springframework.web.bind.MissingServletRequestParameterException: Required Integer parameter 'id' is not present异常

而@RequestBody

@RequestMapping(value = "/test1/testRequestBody")
public String requestBody(@RequestBody String name) {
	System.out.println("test2:" + name);
	return "test1/requestBody";
}
如果直接访问链接,不带任何参数会报:

org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String org.yamikaze.hello.TestController1.requestBody(java.lang.String),可以通过required=false关闭必须需要请求体。
如果带上name参数也是这个异常。

因为HTTP请求主要分为GET和POST请求,GET请求没有请求体,POST请求有。@RequestBody需要是POST请求才能访问。即就算没有在方法上声明method=RequestMethod.POST,那么带上@RequestBody也只有POST请求才能访问。

以下几种方式为GET请求:

1、直接在浏览器地址栏输入链接。

2、点击页面上的非表单提交按钮链接。

3、表单method属性不设置或者设置为get。

4、使用Socket编程时使用GET请求行。

5、使用JS直接改变当前图片,页面的链接。

6、使用ajax时参数设置为get,jquery的get方法。
以下几种方式为POST请求:

1、表单提交method属性设置为POST

2、使用Socket编程时POST请求行。

3、使用ajax时参数设置为POST或者JQuery的POST方法。

主要GET和POST的情况如上所示。


3、@PathVariable注解

Spring MVC支持REST风格的访问,即将参数包含在链接路径中,如下所示:

@RequestMapping("/test2/info/{id}")
public String testRest(@PathVariable Integer id) {	
	return "test2/info";
}
如果在浏览器输入localhost:8080/test2/info是不会进入这个方法的,带上参数即可localhost:8080/test2/info/2。同样可以通过设置required=false关闭,但好像无效。

4、@Validated和BindingResult

使用Spring MVC等前端框架带来的一个好处是,不是手动使用request的getParameter去获取参数,Spring MVC会自动映射到方法参数中去,例如第二点的@RequestParam。请求参数多时也可以使用对象作为方法参数,Spring MVC会将相应的属性映射到对象中,但用户传的参数可能不符合要求,这时候可以使用@Validated验证参数,验证框架使用Hibernate-validator。在pom文件中加入这段代码引入依赖,或者将相应依赖jar拷贝到lib文件夹下并加入classpath。


	org.hibernate
	hibernate-validator
	5.2.4.Final
然后在属性get方法上使用@NotEmpty、@NotBlank、@Length等属性注解并填上相应提示信息。然后在Controller中这样编写:
@RequestMapping("/testBindingResult")
public String testBindingResult(@Validated User user, BindingResult br) {
	if(br.hasErrors()) {
		//如果验证有错
		//code
	}
	//验证没有错误
	return "br";
}
注意:使用了@Validated之后 下一个参数要紧跟着BindingResult。
5、Model或者Map

开发项目时经常会遇到在JSP、ftl与Controller层传递参数或者信息。使用servlet时可以使用域对象的setAttribute方法存储和getAttribute方法获取。在Spring MVC中,可以在参数上面加上Model或者一个HashMap来传输。例如:

@RequestMapping("/value2")
public String value2(String username, Model model) {
       model.addAttribute("username", username);
       // 由于存取值都是使用key——value,
       // 这个方法只有一个参数,默认是使用username的类型来当作key。
       // 在使用自定义的对象时非常有用 addAttribute(new User())-->addAtt("user",new User())
       model.addAttribute(username);
       return "value2";
}
然后就可以在视图层愉快的使用EL表达式获取数据了。使用Map操作时一致的,只需要将参数Model替换为Map。


你可能感兴趣的:(java)