阿华代码,不是逆风,就是我疯
你们的点赞收藏是我前进最大的动力!!
希望本文内容能够帮助到你!!
目录
一:实践
1:获取URL中的参数
(1)@PathVariable
2:上传文件
(1)普通写法
(2)@RequestPart
(3)注意
二:获取Cookie和Session
1:状态
2:Cookie
编辑 3:Session
4:两者区别
5:获取Cookie代码实现
(1)普通代码
(2)lambda表达式
(3)注解形式
6:Cookie造假
7:Session存储
(0)获取session
(1)第一次发送请求
(2)构架Session对象
(3)第二次请求
8:Seesion读取
9:精华总结
(1)公共部分提取
(2)提取公共部分封装成HttpSession
(3)封装获取属性
一:实践
1:获取URL中的参数
(1)@PathVariable
路径变量这个注解主要作⽤在请求URL路径上的数据绑定
默认传递参数写在URL上,SpringMVC就可以获取到
注意①请求路径中不能再{password}对应的地方写个字符串类型,尽管url是字符串类型,但是最后到服务器上转化为的是Integer类型
注意②下面这段代码中括号中的参数是连续赋值的这样一个过程
@RequestMapping("m1/{password}/{name}")
    public String m1(@PathVariable("password") Integer id , @PathVariable("name") String name){
        return "id是:" + id +"    name是:" + name;
    }
2:上传文件
(1)普通写法
@RequestMapping("m2")
    public String getfile(MultipartFile file){
        String fileName = file.getOriginalFilename();
        return "接受到的文件的名称为:" + fileName;
    }
(2)@RequestPart
@RequestPart这个注解可以作为中间商进行传参
@RequestMapping("m2")
    public String getfile(@RequestPart("fileTemp") MultipartFile file){
        String fileName = file.getOriginalFilename();
        return "接受到的文件的名称为:" + fileName;
    }(3)注意
上传的文件体积可能会过大,此时就要注意看日志了,往往错误的日志信息才是提升自己代码能力的关键
二:获取Cookie和Session
1:状态
理解状态和无状态:HTTP是一种无状态协议,什么是无状态呢?
无状态:在服务器和客户端的连接中,不保存对端信息。在下一次再连接时,需要再次验证身份。
有状态:类似第一次去医院,需要办理一张就诊卡,把身份信息登记一下,再看病。第二次再去医院的时候,直接刷卡,工作人员就知道你是谁了,不需要再走登记身份信息的流程了
2:Cookie

 3:Session
 3:Session
 
 
 
 
 
4:两者区别
5:获取Cookie代码实现
(1)普通代码
@RequestMapping("m3")
    public String m3(HttpServletRequest request , HttpServletResponse response){
        //获取Cookie信息
        Cookie[] cookies = request.getCookies();
        //打印Cookie信息
        StringBuilder builder = new StringBuilder();
        if(cookies != null){
            for(Cookie ck : cookies){
                builder.append(ck.getName() + ":" + ck.getValue());
            }
        }
        return "返回的Cookie信息是:" + builder;
    }
(2)lambda表达式
 @RequestMapping("m3_5")
    public String m3_5(HttpServletRequest request , HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            Arrays.stream(cookies).forEach(ck-> System.out.println(ck.getName() + " " + ck.getValue()));
        }
        return "获取Cookie";
    }(3)注解形式
RequestMapping("m3.2")
    public String m3_2(@CookieValue("bite") String bite){
        return "bite:" + bite;
    }

6:Cookie造假

7:Session存储
(0)获取session
public String m4(HttpServletRequest request){
        HttpSession session = request.getSession();
        if(session != null){
            session.setAttribute("username","java");
        }
        return "session存储成功";
    }简洁获取Session
①通过注解@SessionAttribute获取
先调用m6方法,获取session为null,在调用m4方法存储session,在调用m6
@RequestMapping("m6")
    public String m6(@SessionAttribute(value = "username" , required = false) String username){
        return "m6方法中返回的username参数为:" + username;
    }

②通过Spring MVC内置对象HttpSession 来获取
@RequestMapping("m7")
    public String m7(HttpSession session){
        String username = (String) session.getAttribute("username");
        return "m7方法中返回的username参数为:" + username;
    }(1)第一次发送请求
 
 
(2)构架Session对象
 
 
(3)第二次请求
 
 
8:Seesion读取
@RequestMapping("m5")
    public String m5(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        String username = null;
        if(session != null && session.getAttribute("username") != null ){
            username = (String)session.getAttribute("username");
        }
        return "username:" + username;
    } 
 
  
 
   
 
 9:精华总结
(1)公共部分提取
 
 
 (2)提取公共部分封装成HttpSession

(3)封装获取属性





















