SpringMVC中部分注解及其要求的xml配置

mac2024-03-20  23

@RequestMapping注解

   概念:

1.SpringMVC使用@RequestMapping注解为控制器指定某些URL请求

2.可以在控制器类定义,也可以在具体的方法上定义(一般情况下这两个一起使用便于区分)

3.作用:前端控制器拦截请求后,通过控制器上的@RequestMapping的映射信息,找到对应的方法,执行对应的逻辑

样例:

package com.thekingqj.spring; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; @Controller//helloController/modelAndView @RequestMapping(value="/helloController") public class HelloController { @RequestMapping(value="/hello",params= {"id"},method=RequestMethod.GET)//请求的位置,跟servlet意思一样 public String say(@RequestParam("id")Integer id) { System.out.println(id); return "hello"; } @RequestMapping(value="/ant/{id}",method= RequestMethod.GET)//请求的位置,跟servlet意思一样 public String testAnt(@PathVariable("id") String id) { System.out.println("id="+id); return "hello"; } @RequestMapping(value="/ant1",method= RequestMethod.GET,params= {"id"})// public String testAnt1(@PathVariable("id") Integer id) { System.out.println("id="+id); return "hello"; } @RequestMapping(value="/user/{id}",method= RequestMethod.DELETE)// public String testdel() { System.out.println("删除"); return "hello"; } @RequestMapping(value="/user",method= RequestMethod.POST)// public String testPost(@RequestParam(value="name") String name) { System.out.println("新建"+name); return "hello"; } @RequestMapping(value="/cookieId")// public String testCookieId(@CookieValue("JSESSIONID") String name) { System.out.println("获取cookieid"+name); return "hello"; } @RequestMapping(value="/modelAndView") public ModelAndView testModelAndView() { ModelAndView m=new ModelAndView(); m.addObject("username","theking"); m.setViewName("hello"); return m; } @RequestMapping(value="/testMap") public String testMap(Map<String,Object> map) { System.out.println(map.getClass().getName()); map.put("username", "saber"); return "hello"; } @RequestMapping(value="/testModel") public String testModel( Model model) { model.addAttribute("username", "saber0"); return "hello"; } @RequestMapping(value="/testRedirect") public String testRedirect( Model model) { System.out.println("111"); return "redirect:/helloController/testModel"; } }

@RequestMapping中的属性:

      value【重点】:请求 的URL路径

      method【重点】:请求方法 get、post、delete

      params【了解】:URL请求中必须要携带的参数

这几个联合使用可以让请求的映射更加的细化

   params 和 headers支持简单的表达式:

                param1: 表示请求必须包含名为 param1 的请求参数

                !param1: 表示请求不能包含名为 param1 的请求参数

                param1 != value1: 表示请求包含名为 param1 的请求参数,但其值不能为 value1

                {"param1=value1", "param2"}: 请求必须包含名为 param1 和param2 的两个请求参数,且 param1 参数的值必须为 value1

 

RequestMapping映射请求占位符PathVariable注解

   再说这个之前,有一个东西要铺垫(REST路径格式,关于Rest这里只是使用它的路径方式)

REST:即 Representational State Transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便,所以正得到越来越多网站的采用

通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:

URL 中的 {xxx} 占位符可以通过 @PathVariable("xxx") 绑定到操作方法的入参中。

带占位符的URL 是 Spring3.0 新增的功能,该功能在 SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义

 

什么是占位符??/order/${id}

示例:

/order/1   HTTP GET :得到 id = 1 的 order           gerOrder?id=1

/order/1   HTTP DELETE:删除 id = 1的 order         deleteOrder?id=1

/order     HTTP PUT:更新order   

/order      HTTP POST:新增 order 

后台获取:

@RequestMapping(value="/order/{id}",method= RequestMethod.GET)//请求的位置,跟servlet意思一样 public String testAnt(@PathVariable("id") String id) { System.out.println("id="+id); return "hello"; }

REST扩展

关于REST这里多讲一点关于后台@RequestMapping映射中请求方法参数(get、put、delete、post)问题,由于前台的请求只能存在两种形式(get和post)但是REST要求:get(获取数据)、post(添加数据)、delete(删除数据)、put(更新数据),这样会带来一个问题怎样区分这些请求?(状态转换)

      Spring3.0后添加了一个HiddenHttpMethodFilter过滤器使得浏览器发送的get或post请求转化为对应的四种请求的其中一种。

 但是有特定的要求,在发送的form表单里面必须要有特定的input标签,不过他是隐藏的

<form action="helloController/user/1" method="post"> <input type="hidden" name="_method" value="delete"> <input type="submit" value="删除"> </form>

 配置HiddenHttpMethodFilter过滤器(注意是在web.xml中配置呦)

<!-- 支持REST风格的过滤器:可以将POST请求转换为PUT或DELETE请求 --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

处理请求数据的@RequestParam注解

    1)在处理方法入参处使用 @RequestParam 可以把请求参数传递给请求方法

    2)value:参数名

    3)required:是否必须。默认为 true, 表示请求参数中必须包含对应的参数,若不存在,将抛出异常

    4)defaultValue: 默认值,当没有传递参数时使用该值

样例:

@RequestMapping(value="/user",method= RequestMethod.POST)// public String testPost(@RequestParam(value="name") String name) { System.out.println("新建"+name); return "hello"; } <form action="helloController/user" method="post"> <input type="hidden" name="_method" value="post"> <input type="text" name="name" > <input type="submit" value="新建"> </form>

@RequestParam和@PathVariable的区别:

        @pathVariable的请求路径:"user/1"     后台获取方式:value="/order/{id}"  @PathVariable("id") String id

        @RequestParam的请求路径:"user" 参数的传递方式:?id=1&name=13     

            后台获取方式:value="/user" @RequestParam(value="name") String name

最新回复(0)