Spring MVC 注解类【Spring MVC 学习笔记 四】

前言

      使用Spring MVC注解首先要在Spring MVC的Ioc容器配置文件中添加下列代码,已启用Spring MVC注解。

    <mvc:annotation-driven></mvc:annotation>

      如果无法识别mvc标签,则需在beans标签下加入下列内容:

    xmlns:mvc="http://www.springframework.org/schema/mvc"

@RequestMapping注解类

      1.功能:映射请求地址,请求方式,请求参数,请求类型。

      2.映射请求地址:value="uri" 

value是@RequstMapping的默认属性。或者使用:path="uri"

例子:

    @RequestMapping(value="/delete")
    public String delete(int empNo) throws Exception{
            ..
            ..
            ..
        return "index";
    }

省略格式:

    @RequestMapping("/delete")
    public String delete(int empNo) throws Exception{
            ..
            ..
            ..
        return "index";
    }

如果有其他属性,value不能省略。

      3.确定请求方式: method={请求方式,请求方式}

@RequestMapping(value="/add",method={RequestMethod.POST,RequestMethod.GET})

@RequestMapping(value="/add",method=RequestMethod.POST)

    @RequestMapping(value="/add",method=RequestMethod.POST)
    public String add(Employee employee) throws Exception{
            ..
            ..
            ..
        return "index";
    }

      4.确定请求类型: consumes={"请求数据格式"}

    //增加员工的后处理控制方法
    @RequestMapping(value="/add", consumes="application/json",  method={RequestMethod.POST,RequestMethod.GET})
    public String add(Employee employee) throws Exception{              
            ..   
            ..
            ..
        return "add";
    }

      5.确定响应类型: produces={"响应类型","响应类型"}

    //增加员工的后处理控制方法
    @RequestMapping(value="/add", consumes="application/json", produces="application/json", method={RequestMethod.POST,RequestMethod.GET})
    public String add() throws Exception{
            ..
            ..
            ..
        return "add";
    }

@RequestParam注解类

      1.功能:确定请求参数

      2.位置:放置在控制方法参数的前面。

    public String toListByAll(@RequestParam(required=false,defaultValue="1") int page, Model model) throws Exception{
    }

      3.主要的属性:

value="参数名"

required=true|false, 确定参数是否必须提供, 默认是true.

DefaultValue="初始值"

      4.有@RequestParam参数的请求

参数和url使用?分割

http://localhost:8080/employee/tolistbyall.mvc?page=1

多个参数使用&连接

http://localhost:8080/neuoaweb//employee/tolistbyall.mvc?page=1&rows=10

Controller方法中定义的参数,没有@RequestParam修饰,默认是请求参数。

 

Path参数注解类@PathVariable

      1.URL的地址参数的格式: URI中使用{参数名}实现。

      2.使用@PathVariable接收;REST API编程的控制器使用较多。

    @RequestMapping(value="/tolistbyallpage{page}rows{rows}", method={RequestMethod.GET})
    public String toListByAll(@PathVariable("page") int page,@PathVariable int rows,Model model) throws Exception{
        ..
        ..
        ..
    }

 

@ModelAttribute注解类

      1.通常使用在返回JSP Viewd.

      2.在控制方法的参数上加@ModelAttribute,表达该参数自动成为Spring MVC Model对象的属性。

 

@RequestHeader注解类

      1.使用在控制方法的参数前面。

      2.功能:取得指定请求头

      3.语法:@RequestHeader("User-Agent") String browser

等价的JavaEE代码:

    String brower=request.getHeader("User-Agent");

@SessionAttribute注解类

      1.使用控制器的参数上。

      2.功能:直接取得Session中保存的数据。 session.setAttribute(“user”,”lhd”);

      3.getUser(@SessionAttribute(“user”) String user)

 

@CookieValue注解类

      1.使用控制器的方法的参数上。

      2.功能:取得指定的Cookie的值。

    @GetMapping("/demo")

    public void handle(@CookieValue("JSESSIONID") String cookie) {

        //...

    }

@ResponseBody注解类

      1.使用方法的前面。

      2.功能:指定控制器方法直接返回数据给客户端,不通过View解析器。一般用于返回JSON或XML。

      3.语法:

    @RequestMapping(value="/tolistbyalljson", method={RequestMethod.GET})
    @ResponseBody
    public List<EmployeeModel> toListByAllForResponse(@RequestParam(value="page", required=false, defaultValue="1") @ModelAttribute int page, @RequestParam(value="rows", required=false, defaultValue="10") int rows, @RequestHeader("User-Agent") String browser, @RequestHeader("Host") String host, Model model, HttpServletRequest request) throws Exception{
        List<EmployeeModel> list=new ArrayList<EmployeeModel>();
        //...
        return list;
    }

@RestController注解类

      1.使用在控制器的类上。

      2.@RestController=@Controller+@ResponseBody

 

@DateTimeFormat(pattern="yyyy-MM-dd")

      1.主要使用在Model类的Date类型的属性上。

      2.功能:自动将日期从特定格式的字符串类型转换成java.util.Data型。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值