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) { FavUser favUser = favUserService.findFavUser(); model.addAttribute( ; } |
@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 ... }