springmvc中将值存入session,jsp页面获取(ps:加获取不去值的坑)

在session中存值:

public ModelAndView loginCkeck(ShopUser shopUser, HttpSession httpSession){
        ModelAndView fail = new ModelAndView("login");
        String username = shopUser.getUsername();
        String password = shopUser.getUpassword();

        List<ShopUser> list =userLoginService.login(username,password);
        if( (list != null) &&(list.size()>=1) ){
            httpSession.setAttribute("userInfo",list.get(0));
            return new ModelAndView("redirect:/category");
        }else{
            return fail.addObject("fail","用户名或密码错误");
        }
    }
在前台页面中取值:

			<c:choose>
					<c:when test="${not empty sessionScope.get('userInfo')}">
						<li id="headerLogin" class="headerLogin" style="display: list-item;">
								${sessionScope.get('userInfo').username}<a href="./user_logout.action">[退出]</a>|
						</li>
						<li id="headerLogout" class="headerLogout" style="display: list-item;"><a href="./order_findByUid.action?page=1">我的订单</a>|
						</li>
					</c:when>
					<c:otherwise>
						<li id="headerLogin" class="headerLogin" style="display: list-item;"><a href="./user_LoginPage">会员登录</a>|</li>
						<li id="headerRegister" class="headerRegister" style="display: list-item;"><a href="./user_RegistPage">会员注册</a>|</li>
					</c:otherwise>

				</c:choose>


这里有个坑就是在JSTL表达式的前后不能出现空格,否则不会生效

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值