在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表达式的前后不能出现空格,否则不会生效