Spring MVC-如何使用session

本文介绍了一个简单的Spring MVC应用案例,通过使用Session来记录用户的访问次数,并在每次访问/check时更新计数器。此方法适用于登录状态跟踪及页面间的数据传递。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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}
  • 测试

    访问页面
    http://127.0.0.1:8080/springmvc/check
    每次访问,次数都+1




### 关于 Spring Web MVC 5.3.41 的相关信息 Spring Web MVCSpring Framework 中的一个模块,主要用于支持基于 Java 的 Web 开发。对于特定版本的信息或依赖下载,可以通过 Maven Central Repository 或其他官方资源获取。 #### 版本信息 Spring Web MVC 作为 Spring Framework 的一部分,在其生命周期中遵循语义化版本控制策略。具体到版本 `5.3.41`,这是属于 Spring Framework 5.x 系列中的一个维护更新版本[^1]。此版本可能包含了若干修复、优化以及兼容性改。 要了解该版本的具体变更日志,可以访问以下链接查看详细的 Release Notes 和 Changelog: - 官方文档地址:https://spring.io/projects/spring-framework#learn - GitHub 发布页面:https://github.com/spring-projects/spring-framework/releases/tag/v5.3.41 #### 下载方式 通过 Maven 构建工具可以直接引入所需依赖。以下是针对 Spring Web MVC 5.3.41 的 Maven 配置: ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.41</version> </dependency> ``` 如果使用 Gradle,则配置如下: ```gradle implementation 'org.springframework:spring-webmvc:5.3.41' ``` 这些配置会自动从中央仓库拉取对应的 JAR 文件及其必要的传递依赖项[^2]。 #### 主要功能特性 Spring Web MVC 提供了一套完整的解决方案来简化 Web 应用程序的开发过程。它包括但不限于以下几个方面: - 支持灵活的控制器机制。 - 内置多种视图解析器以适应不同类型的前端技术。 - 自动绑定请求参数至方法参数的功能。 - 对国际化 (i18n) 及主题的支持。 - 易于集成第三方框架如 Thymeleaf、Freemarker 等模板引擎。 此外还提供了诸如向 Session 域或者 Application 域存储数据的能力,这通常用于跨请求间保持状态信息。 #### 示例代码片段 下面展示如何在一个简单的 Controller 类里操作 session 和 application 范围的数据: ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; @Controller public class ScopeController { @RequestMapping("/testSession") public String testSession(HttpSession session){ session.setAttribute("testSessionScope", "hello,session"); return "success"; } @RequestMapping("/testApplication") public String testApplication(HttpSession session){ ServletContext application = session.getServletContext(); application.setAttribute("testApplicationScope", "hello,application"); return "success"; } } ``` 以上代码展示了如何分别设置 session 属性和 application 属性。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值