在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表达式的前后不能出现空格,否则不会生效
本文介绍了一个基于Java的Web应用程序中的用户登录流程与会话管理实现方式。具体包括如何在后端验证用户输入的用户名和密码,并在验证通过后将用户信息存储到HTTP会话中,以及如何在前端页面通过JSTL表达式读取这些会话信息来展示用户的登录状态。

被折叠的 条评论
为什么被折叠?



