第一步加上头文件
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
因为是要登陆才能使用,使用用了EL表达式
${empty user}判断是否session空
- <c:choose>
- <c:when test="${empty user}">
- 登陆表单
- </c:when>
- <c:otherwise>
- 登陆后显示是信息
- 可以修改的资料
- </c:otherwise>
- </c:choose>
- <c:choose>
- <c:when test="${empty user}">
- <div class="form_subtitle">登录你的帐号</div>
- <form name="register" action="user/login.do" method="post">
- <div class="form_row">
- <label class="contact"><strong>用户名:</strong></label>
- <input type="text" class="contact_input" name="username"/>
- </div>
- <div class="form_row">
- <label class="contact"><strong>密码:</strong></label>
- <input type="text" class="contact_input" name="password" />
- </div>
- <div class="form_row">
- <div class="terms">
- </div>
- </div>
- <div class="form_row">
- <input type="submit" class="register" value="登录" />
- </div>
- </form>
- </c:when>
- <c:otherwise>
- <div class="form_subtitle">用户资料</div>
- <div class="title">
- <label ><strong>您好 ${sessionScope.user.username} 欢迎光临</strong></label>
- </div>
- <div class="form_row">
- <label ><strong> 您是我们第${sessionScope.user.id}个客户,感谢您一直以来的陪伴! </strong></label>
- </div>
- <div class="form_row"><img src="p_w_picpaths/bullet6.gif" alt="" title="" />
- <label ><strong>用户类型: ${sessionScope.user.usertype} </strong></label>
- </div>
- <div class="form_row"><img src="p_w_picpaths/bullet6.gif" alt="" title="" />
- <label ><strong>用户等级: ${sessionScope.user.rank}级 </strong></label>
- </div>
- <div class="form_row"><img src="p_w_picpaths/bullet6.gif" alt="" title="" />
- <label ><strong>电话号码: ${sessionScope.user.phone} </strong></label>
- </div>
- <div class="form_row"><img src="p_w_picpaths/bullet6.gif" alt="" title="" />
- <label ><strong>默认送货地址: ${sessionScope.user.send_address} </strong></label>
- </div>
- <div class="form_row"><img src="p_w_picpaths/bullet6.gif" alt="" title="" />
- <label ><strong>累计消费金额: ${sessionScope.user.money} </strong></label>
- </div>
- <a href="updateuser.jsp" class="view_cart">修改个人相关资料</a>
- <div class="cart">
- <div class="title"><span class="title_icon"><img src="p_w_picpaths/cart.gif" alt="" title="" /></span>我的购物车</div>
- <div class="home_cart_content"> 3 x 件 | <span class="red">总共: 100$</span> </div>
- <a href="cart.jsp" class="view_cart">查看购物车</a> </div>
- </c:otherwise>
- </c:choose>
登陆form进入 user/login.do
UserController中的 user/login.do中 session.setAttribute("user", user); 记录下session
- @Controller
- @RequestMapping("/user")
- public class UserController {
- @Resource(name="userDao")
- UserDao udao;
- @RequestMapping(value="/login.do",method=RequestMethod.POST)
- public String login(String username,String password,HttpSession session,Map map)
- {
- System.out.print("--------begin--login.do-------------");
- User user=udao.login(username, password);
- System.out.print("--------end--login.do 1-------------");
- if(user!=null)
- {
- session.setAttribute("user", user);
- System.out.print("--------end--login.do-------------");
- map.put("user",user);
- return "redirect:/myaccount.jsp";
- }
- map.put("error", "密码错误或用户名不存在!");
- return "redirect:/myaccount.jsp";
- }
UserDao中用了hql
- @Repository
- public class UserDao extends BaseDao<User>{
- public User login(String username,String password)
- {
- String hql="from User where username=:username and password=:password";
- Query query=getSession().createQuery(hql);
- query.setParameter("username", username);
- query.setParameter("password", password);
- User u=(User) query.uniqueResult(); //用户名不能一样
- return u;
- }
就这样进入用户管理页面啦!
接下来是修改个人信息时也要实时更新显示
这个在/updateuser.do中加上
- User user=udao.login(username, pwd1);
- session.removeAttribute("user"); //remove旧的session
- session.setAttribute("user", user);//新session
仿login.do写的,先
- @RequestMapping(value="/updateuser.do",method=RequestMethod.POST)
- public String updateuser(String username,String password,String pwd1,String pwd2,String phone,String send_address,HttpSession session,Map map)
- {
- System.out.println("----------------begin--updateUser---------------------------");
- username=username.trim();
- password=password.trim();
- pwd1=pwd1.trim();
- phone=phone.trim();
- send_address=send_address.trim();
- boolean result=false;
- if(pwd1.equals(pwd2)){
- result=udao.updateUser(username,password,pwd1,phone,send_address);
- User user=udao.login(username, pwd1);
- session.removeAttribute("user"); //remove旧的session
- session.setAttribute("user", user);//新session
- return result?"redirect:/myaccount.jsp":"redirect:/updateuser.jsp";
- }
- else
- {
- System.out.print("--------end--/updateuser.do 2-------------");
- return "redirect:/updateuser.jsp";
- }
- }
这样就行啦!