打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
SpringMVC常用注解详解

常用注解

RequestParam

作用:把请求中指定名称的参数给控制器中的形参赋值。

属性:

  1. value:请求参数的名称
  2. required:请求参数中是否必须提供此参数,默认值为true表示必须提供。
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>常用注解的使用</title></head><body>    <a href="/anno/testRequestParam?name=hehe">RequestParam</a></body></html>
package com.example.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;@Controller@RequestMapping("/anno")public class AnnoController {    @RequestMapping("/testRequestParam")    public String testRequestParam(@RequestParam("name") String username){        System.out.println(username);        return "success";    }}

RequestBody

作用:用于获取请求体内容,直接使用得到的是key=value&key=value...结构的数据。get请求方式不适用。

属性:required,是否必须有请求体,默认是true,当取值为true时,get方式会报错,如果为false,get请求得到的是null。

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>常用注解的使用</title></head><body><form action="/anno/testRequestBody" method="post">    用户姓名:<input type="text" name="username"><br>    用户年龄:<input type="text" name="password"><br>    <input type="submit" value="提交"></form></body></html
package com.example.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;@Controller@RequestMapping("/anno")public class AnnoController {    @RequestMapping("/testRequestBody")    public String testRequestBody(@RequestBody String body){        System.out.println(body);        return "success";    }}

PathVariable

作用:用于绑定url中的占位符,例如:请求url中/delete/{id},这个{id}就是url的一个占位符。

属性:

  1. value:用于指定url中占位符的名称。
  2. required:是否必须提供占位符。

restful编程风格:描述了一个架构样式的网络系统,比如web应用程序。HTTP协议是一种无状态的协议,即所有的状态都保存在服务器端,因此,如果客户端想要操作这样的服务器,必须通过某种手段,让服务器发生状态转换,而这种转换是建立在表现层(把资源具体呈现出来的形式)之上的,所以就是表现层状态转换。具体说,就是HTTP协议中,四个表示操作的动词:GET,POST,PUT,DELETE。它们分别对应四个基本操作:GET用来获取资源,POST用来新建资源,PUT用来更新资源,DELETE用来删除资源。

restful的示例:

  1. /account/1,HTTP GET:获取id=1的account
  2. /account/1,HTTP DELETE:删除id=1的account
  3. /account/1,HTTP PUT:更新id=1的account
  4. /account,HTTP POST:新增account
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>常用注解的使用</title></head><body><a href="/anno/testPathVariable/10">testPathVariable</a></body></html>
package com.example.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;@Controller@RequestMapping("/anno")public class AnnoController {    @RequestMapping("/testPathVariable/{sid}") //sid和下面PathVariable的参数必须一致    public String testPathVariable(@PathVariable("sid") String id){        System.out.println(id);        return "success";    }}

RequestHeader

作用:用于获取请求头

属性:

  1. value:提供消息头名称。例如:@RequestHeader(value="Accept")
  2. required:是否必须有请求头。
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>常用注解的使用</title></head><body><a href="/anno/testRequestHeader">testRequestHeader</a></body></html>
package com.example.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;@Controller@RequestMapping("/anno")public class AnnoController {    @RequestMapping("/testRequestHeader")    public String testRequestHeader(@RequestHeader(value = "Accept") String header){        System.out.println(header);        return "success";    }}

CookieValue

作用:用于把指定cookie名称的值传入控制器方法参数。

属性:

  1. value:指定cookie的名称。
  2. required:是否必须有此cookie。
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>常用注解的使用</title></head><body><a href="/anno/testCookieValue">testCookieValue</a></body></html>
package com.example.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;@Controller@RequestMapping("/anno")public class AnnoController {    @RequestMapping("testCookieValue")    public String testCookieValue(@CookieValue("JSESSIONID") String cookie){        System.out.println(cookie);        return "success";    }}

ModelAttribute

作用:出现在方法上,表示当前方法会在控制器的方法执行之前先运行,它可以修饰没有返回值的方法,也可以修饰有返回值的方法。出现在参数上,获取指定的数据给当前参数赋值。

属性:value用于获取数据的key,key可以是POJO的属性名称,也可以是map结构的key。

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>常用注解的使用</title></head><body><form action="/anno/testModelAttribute" method="post">    用户姓名:<input type="text" name="uname"><br>    用户年龄:<input type="text" name="age"><br>    <input type="submit" value="提交"></form></body></html>
//有返回值的方式package com.example.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;@Controller@RequestMapping("/anno")public class AnnoController {    @RequestMapping("testModelAttribute")    public String testModelAttribute(User user){        System.out.println(user);        System.out.println("testModelAttribute执行了");        return "success";    }    @ModelAttribute    public User showUser(String uname){//提交表单不完整时使用        User user =new User();        user.setUname(uname);        user.setAge(20);        user.setDate(new Date());        System.out.println("showUser执行了");        return user;    }}
//无返回值的方式package com.example.controller;import com.example.domain.User;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;import java.util.Date;import java.util.Map;@Controller@RequestMapping("/anno")public class AnnoController {    @RequestMapping("testModelAttribute")    public String testModelAttribute(@ModelAttribute("abc") User user){        System.out.println(user);        System.out.println("testModelAttribute执行了");        return "success";    }    @ModelAttribute //注意:获取前端的参数,必须和标签的name属性一致    public void showUser(String uname, Map<String,User> map) {//提交表单不完整时使用        User user = new User();        System.out.println(uname);        user.setUname(uname);        user.setAge(20);        user.setDate(new Date());        map.put("abc",user);    }}

SessionAttributes

作用:用于多次执行控制器方法间的参数共享,该注解只能作用在类上。

属性:

  1. value:用于指定存入的属性名称。
  2. type:用于指定存入数据的类型。
package com.example.controller;import com.example.domain.User;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletRequest;@Controller@RequestMapping("/anno")@SessionAttributes(value = {"msg"})public class AnnoController {    @RequestMapping("/testSessionAttributes")    public String testSessionAttributes(Model model){ //Model中存储数据实际上放入request域中        model.addAttribute("msg","消息123");        return "success";    }    @RequestMapping("/getSessionAttributes")public String getSessionAttributes(ModelMap model){ //Model中存储数据实际上放入request域中    String msg = (String) model.get("msg");    System.out.println(msg);    return "success";}@RequestMapping("/delSessionAttributes")public String delSessionAttributes(SessionStatus status){ //删除session的内容    status.setComplete();    return "success";}}
<!--发送请求--><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>常用注解的使用</title></head><body><a href="/anno/testSessionAttributes">testSessionAttributes</a><a href="/anno/getSessionAttributes">getSessionAttributes</a><a href="/anno/delSessionAttributes">delSessionAttributes</a></body></html>
<!--获取session中的值--><%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %><html><head>    <title>Title</title></head><body><h3>成功</h3>${msg}${sessionScope}</body></html>
来源:https://www.icode9.com/content-4-833751.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
SpringMVC上传文件demo(转)
spring的controller是单例还是多例,怎么保证并发的安全。
springmvc注解@Controller和@RequestMapping
Java中防止数据重复提交超简单的6种方法
AnnotationHelloController.java
Spring 3 MVC and JSON example
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服