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表达式的前后不能出现空格,否则不会生效
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: 解决前端到后端、后端到数据库、后端到前端的乱码问题
- 下一篇:没有了
