Spring从2.5版本开始在编程中引入注解,用户可以使用@RequestMapping, @RequestParam, @ModelAttribute等等这样类似的注解。到目前为止,Spring的版本虽然发生了很大的变化,但注解的特性却是一直延续下来,并不断扩展,让广大的开发人员的双手变的更轻松起来,这都离不开Annotation的强大作用,今天我们就一起来看看Spring MVC 4中常用的那些注解吧。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?><
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:p
=
"http://www.springframework.org/schema/p"
xmlns:context
=
"http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<
context:component-scan
base-package
=
"org.springframework.samples.petclinic.web"
/>
<!-- ... -->
</
beans
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
@Controller
@RequestMapping
(
"/favsoft"
)
public
class
AnnotationController {
@RequestMapping
(method=RequestMethod.GET)
public
String get(){
return
""
;
}
@RequestMapping
(value=
"/getName"
, method = RequestMethod.GET)
public
String getName(String userName) {
return
userName;
}
@RequestMapping
(value=
"/{day}"
, method=RequestMethod.GET)
public
String getDay(Date day){
DateFormat df =
new
SimpleDateFormat(
"yyyy-MM-dd"
);
return
df.format(day);
}
@RequestMapping
(value=
"/addUser"
, method=RequestMethod.GET)
public
String addFavUser(
@Validated
FavUser favUser,BindingResult result){
if
(result.hasErrors()){
return
"favUser"
;
}
//favUserService.addFavUser(favUser);
return
"redirect:/favlist"
;
}
@RequestMapping
(
"/test"
)
@ResponseBody
public
String test(){
return
"aa"
;
}
}
|
String findOwner( String , Model model) { |
@RequestMapping(value="/owners/{ownerId}/pets/{petId}", method=RequestMethod.GET)public String findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
Owner owner = ownerService.findOwner(ownerId);
Pet pet = owner.getPet(petId);
model.addAttribute("pet", pet); return "displayPet";
}
@RequestMapping(value = "/something", method = RequestMethod.PUT)public void handle(@RequestBody String body, Writer writer) throws IOException {
writer.write(body);
}
@RequestMapping(value = "/something", method = RequestMethod.PUT)@ResponseBodypublic String helloWorld() { return "Hello World";
}
@RequestMapping("/something")public ResponseEntity<String> handle(HttpEntity<byte[]> requestEntity) throws UnsupportedEncodingException {
String requestHeader = requestEntity.getHeaders().getFirst("MyRequestHeader")); byte[] requestBody = requestEntity.getBody(); // do something with request header and body
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue"); return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);
}
@ModelAttribute
public Account addAccount(@RequestParam String number) {
return accountManager.findAccount(number);
}
@ModelAttribute
public void populateModel(@RequestParam String number, Model model) {
model.addAttribute(accountManager.findAccount(number));
// add more ...
}