SpringMVC 注解(待补充)

来源:互联网 发布:linux 多进程通信 编辑:程序博客网 时间:2024/05/17 04:07

@ResponseBody

作用: 

      该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区,即:直接写入HTTP response body中,和response.getWriter().print(xxx)效果类似。

使用时机:

      返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;


@PathVariable是用来对指定请求的URL路径里面的变量 

eg: Java代码 

  1. @RequestMapping(value = "form/{id}/apply", method = {RequestMethod.PUT, RequestMethod.POST})  
@RequestMapping(value="/updateView/{id}")
    public ModelAndView updateView(@PathVariable(value = "id") String id){
        ModelAndView mav = new ModelAndView("demo/personUpdate");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("id", id);
        Person person = personService.findPersonById(map);
        mav.addObject("person", person);
        return mav;
    }

 {id}在这个请求的URL里就是个变量,可以使用@PathVariable来获取

 @PathVariable和@RequestParam的区别就在于:@RequestParam用来获得静态的URL请求参数;@PathVariable用来获得动态的URL请求入参


http://localhost:8080/Springmvc/user/page.do?pageSize=3&pageNow=2 

你可以把这地址分开理解,其中问号前半部分:http://localhost:8080/Springmvc/user/page.do 这个就是路径,是你的请求url,而如果这个路径上有数据匹配,用的就是@PathVariable  如 
@RequestMapping(value="/page{pageNo}.do") 
public String page(@PathVariable int pageNo){} 
【注意:看下这地方的@RequestMapping的地址,对照我上面说的路径】 

问号的后面部分就是请求参数部分,是要向请求路径提交的参数信息,用的就是@RequestParam ,对于这种参数,如果你要用的话,代码应该如下: 
@RequestMapping(value="/page.do") 
public String page(@RequestParam int pageSize,@RequestParam  int pageNow){} 

【注意:这个代码的RequestMapping地址只需要/page.do就行】 


@Controller

 

    Controller控制器是通过服务接口定义的提供访问应用程序的一种行为,它解释用户的输入,将其转换成一个模型然后将试图呈献给用户。Spring MVC 使用 @Controller 定义控制器,它还允许自动检测定义在类路径下的组件并自动注册。如想自动检测生效,需在XML头文件下引入 spring-context:

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>

@RequestMapping

 

    我们可以 @RequestMapping 注解将类似 “/favsoft”这样的URL映射到整个类或特定的处理方法上。一般来说,类级别的注解映射特定的请求路径到表单控制器上,而方法级别的注解只是映射为一个特定的HTTP方法请求(“GET”,“POST”等)或HTTP请求参数。

 

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";
    }
     
}

 

    @RequestMapping 既可以作用在类级别,也可以作用在方法级别。当它定义在类级别时,标明该控制器处理所有的请求都被映射到 /favsoft 路径下。@RequestMapping中可以使用 method 属性标记其所接受的方法类型,如果不指定方法类型的话,可以使用 HTTP GET/POST 方法请求数据,但是一旦指定方法类型,就只能使用该类型获取数据。

 

    @RequestMapping 可以使用 @Validated与BindingResult联合验证输入的参数,在验证通过和失败的情况下,分别返回不同的视图。



原创粉丝点击