标签:
Spring从2.5版本开始在编程中引入注解,用户可以使用@RequestMapping, @RequestParam, @ModelAttribute等等这样类似的注解。到目前为止,Spring的版本虽然
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 = "/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 ... }
标签:
原文地址:http://www.cnblogs.com/ZhuRenWang/p/4691462.html