springboot后端无法获取session

springboot后端无法获取session

原来代码

  @RequestMapping("sendSMS")
    public Result<String> sendSMS(@RequestBody TelCode telCode, HttpSession session){
        //获取手机号码
        String mobile = telCode.getMobile();
        String code = ValidateCodeUtil.generateValidateCode(6).toString();
        session.setAttribute(mobile,code);
        return Result.success("手机验证码发送成功");

    }
 @RequestMapping("loginByMobile")
    public Result<TelCode> loginByMobile(@RequestBody Map map, HttpSession session,TelCode telCode){
        String mobile = map.get("mobile").toString();
        telCode.setMobile(mobile);
        //获取验证码
        String code = map.get("code").toString();
        telCode.setCode(code);
        //获取Session中保存的验证码
        String codeInSession = (String) session.getAttribute(mobile);
        return Result.success("200","成功", telCode);
    }

使用 HttpServletRequest中的`

request.getServletContext().setAttribute(mobile,code);
String codeInSession = (String) request.getServletContext().getAttribute(mobile);

解决

### Spring Boot 后端菜单国际化的实现 在 Spring Boot 中实现菜单的国际化可以通过配置 `MessageSource` 和使用资源文件来完成。以下是具体方法: #### 配置 MessageSource 通过定义一个 Bean 来加载消息源,可以支持多语言切换。 ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ReloadableResourceBundleMessageSource; @Configuration public class InternationalizationConfig { @Bean public ReloadableResourceBundleMessageSource messageSource() { ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource(); source.setBasename("classpath:messages"); // 设置基础名称为 messages 文件夹下的属性文件 source.setDefaultEncoding("UTF-8"); return source; } } ``` 此部分代码用于初始化可重载的消息源[^1]。 #### 创建资源文件 创建多个 `.properties` 文件以存储不同语言的文字内容。例如,在 `src/main/resources/messages/` 下放置以下文件: - `menu_en.properties`: 英语版本的菜单项。 - `menu_zh.properties`: 中文版本的菜单项。 示例内容如下: **menu_en.properties** ```properties home=Home about=About Us contact=Contact settings=Settings ``` **menu_zh.properties** ```properties home=首页 about=关于我们 contact=联系我们 settings=设置 ``` 这些键值对将被用来动态替换页面上的文字显示[^2]。 #### 使用 Thymeleaf 或其他模板引擎渲染前端 如果采用 Thymeleaf,则可以在 HTML 模板中调用 `${#messages.msg(...)}` 方法获取翻译后的字符串。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title th:text="#{app.title}">Application Title</title> </head> <body> <ul> <li><a href="#" th:text="#{menu.home}">Home</a></li> <li><a href="#" th:text="#{menu.about}">About</a></li> <li><a href="#" th:text="#{menu.contact}">Contact</a></li> <li><a href="#" th:text="#{menu.settings}">Settings</a></li> </ul> </body> </html> ``` 上述代码片段展示了如何利用 Thymeleaf 的国际化功能展示不同的菜单选项[^3]。 #### 动态更改 Locale 为了允许用户实时改变界面的语言环境,需提供一种机制更新当前会话中的 locale 值。通常可通过拦截器或者控制器参数的形式处理请求头 Accept-Language 字段或专门设计 API 接口供客户端调用修改 session 属性。 ```java @ControllerAdvice public class LocalizationInterceptor extends HandlerInterceptorAdapter { private final LocaleResolver localeResolver; public LocalizationInterceptor(LocaleResolver localeResolver) { this.localeResolver = localeResolver; } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String langParam = request.getParameter("lang"); if (langParam != null && !langParam.isEmpty()) { Locale locale = new Locale(langParam); localeResolver.setLocale(request, response, locale); } return true; } } ``` 以上代码实现了基于 URL 参数 (`?lang=en`) 切换语言的功能[^4]。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值