ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringMVC常用注解详解

2021-01-25 12:36:11  阅读:122  来源: 互联网

标签:RequestMapping SpringMVC import springframework 详解 org 注解 public String


常用注解

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>

标签:RequestMapping,SpringMVC,import,springframework,详解,org,注解,public,String
来源: https://www.cnblogs.com/acknowledge/p/14324545.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有