@CookieValue中有属性required默认为true,意为必须存在,否则报错,如果设置为false,则如果获取不到则为null
6.@RequestBody 处理json请求,post请求体获取信息
1.代码实例
1.index.html
Title
基本注解
2.ParameterController.java
package com.sun.springboot.controller;
import org.springframework.web.bind.annotation.*;
/**
* @author 孙显圣
* @version 1.0
*/
@RestController
public class ParameterController {
@PostMapping("requestBody")
public String getRequestBody(@RequestBody String requestBody) { //获取请求体
System.out.println(requestBody);
return "success";
}
}
3.测试
7.@RequestAttribute 请求域获取信息
1.代码实例
1.RequestController.java
package com.sun.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
/**
* @author 孙显圣
* @version 1.0
*/
@Controller
public class RequestController {
@GetMapping("/login")
public String login(HttpServletRequest request) {
//在Request域中存放一些信息
request.setAttribute("name", "sun");
request.setAttribute("age", 13);
//调用视图解析器,请求转发到/ok
return "forward:/ok";
}
@ResponseBody
@GetMapping("/ok")
public String ok(@RequestAttribute(value = "name", required = false) String name) { //使用注解来获取请求域中的信息并封装到参数中
System.out.println("name: " + name);
return "success"; //返回json给浏览器
}
}
2.配置视图解析器 application.yml
spring:
mvc:
view: #配置了视图解析器
suffix: .html #后缀
prefix: / #前缀,指的是根目录
3.测试
8.@SessionAttribute session域获取信息
1.代码实例
1.SessionController.java
package com.sun.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import javax.servlet.http.HttpServletRequest;
/**
* @author 孙显圣
* @version 1.0
*/
@Controller
public class SessionController {
@GetMapping("/login")
public String login(HttpServletRequest request) {
//在session域中设置信息
request.getSession().setAttribute("session", "session_value");
//调用视图解析器,请求转发到/ok
return "forward:/ok";
}
@ResponseBody
@GetMapping("/ok")
public String ok(@SessionAttribute(value = "session") String value) { //使用注解来获取session域中的信息并封装到参数中
System.out.println("session: " + value);
return "success"; //返回json给浏览器
}
}
2.配置视图解析器(同上)
3.测试
9.复杂参数
1.代码实例
1.RequestController.java
package com.sun.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
/**
* @author 孙显圣
* @version 1.0
*/
@Controller
public class RequestController {
@GetMapping("/login")
public String login(Map map, Model model, HttpServletResponse response) {
//给map封装信息
map.put("user", "sun");
map.put("job", "工程师");
//model封装信息
model.addAttribute("sal", 1000);
//结果最后都会封装到request域中
//调用视图解析器,请求转发到/ok
return "forward:/ok";
}
@ResponseBody
@GetMapping("/ok")
public String ok(@RequestAttribute("user") String user, @RequestAttribute("job") String job,
@RequestAttribute("sal") Integer sal) { //使用注解来获取请求域中的信息并封装到参数中
System.out.println("user:" + user + " job:" + job + " sal:" +sal);
return "success"; //返回json给浏览器
}
}
2.测试
2.HttpServletResponse给浏览器设置cookie
1.代码实例
package com.sun.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
/**
* @author 孙显圣
* @version 1.0
*/
@Controller
public class RequestController {
@GetMapping("/login")
public String login(HttpServletResponse response) {
Cookie cookie = new Cookie("cookie_name", "cookie_value");
response.addCookie(cookie);
//调用视图解析器,重定向到/ok,不能使用请求转发,因为虽然响应给客户端cookie了,
// 但是由于是请求转发,第二个controller得到的是最开始的请求,那时候还没有cookie
return "redirect:/ok";
}
@ResponseBody
@GetMapping("/ok")
public String ok(@CookieValue("cookie_name") Cookie cookie) {
//获取cookie
System.out.println("key:" + cookie.getName() + " value:" + cookie.getValue());
return "success"; //返回json给浏览器
}
}
2.测试