zoukankan      html  css  js  c++  java
  • SpringMVC使用Session

    Session在用户登录,一些特殊场合在页面间传递数据的时候会经常用到
    @

    修改IndexController

    映射 /check 到方法check()
    为方法check()提供参数HttpSession session,这样就可以在方法体中使用session了
    接下来的逻辑就是每次访问为session中的count+1.
    最后跳转到check.jsp页面

    package controller;
     
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
     
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
     
    @Controller
    public class IndexController {
        @RequestMapping("/index")
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
            ModelAndView mav = new ModelAndView("index");
            mav.addObject("message", "Hello Spring MVC");
            return mav;
        }
     
        @RequestMapping("/jump")
        public ModelAndView jump() {
            ModelAndView mav = new ModelAndView("redirect:/index");
            return mav;
        }
     
        @RequestMapping("/check")
        public ModelAndView check(HttpSession session) {
            Integer i = (Integer) session.getAttribute("count");
            if (i == null)
                i = 0;
            i++;
            session.setAttribute("count", i);
            ModelAndView mav = new ModelAndView("check");
            return mav;
        }
     
    }
    

    check.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8" isELIgnored="false"%>
     
    session中记录的访问次数:${count}
    

    效果

    在这里插入图片描述

  • 相关阅读:
    echarts圆套圆
    两个对象深度比较,借鉴,记录
    js异步加载的方式
    elementUI使用el-card高度自适应
    如何在页面上实现一个圆形的可点击区域
    清除浮动
    水平垂直居中的几种方式
    BFC原理
    正则表达式
    Vue项目中难点问题
  • 原文地址:https://www.cnblogs.com/xiuzhublog/p/12918816.html
Copyright © 2011-2022 走看看