购物平台商品实时推荐系统(二)

一、完善用户信息,实现用户注册和验证
--------------------------------------------------------
    0.更新pom.xml
        
        
            4.0.0

            com.test
            eshop
            1.0-SNAPSHOT

            
                
                    junit
                    junit
                    4.11
                
                
                    c3p0
                    c3p0
                    0.9.1.2
                

                
                
                    org.hibernate
                    hibernate-core
                    4.3.8.Final
                

                
                
                    org.springframework
                    spring-context-support
                    4.3.3.RELEASE
                
                
                    org.springframework
                    spring-jdbc
                    4.3.3.RELEASE
                
                
                    org.springframework
                    spring-orm
                    4.3.3.RELEASE
                
                
                    org.springframework
                    spring-webmvc
                    4.3.3.RELEASE
                
                
                    org.springframework
                    spring-tx
                    4.3.3.RELEASE
                
                
                    org.aspectj
                    aspectjweaver
                    1.8.10
                

                
                
                    mysql
                    mysql-connector-java
                    5.1.17
                

                
                    javax.servlet
                    servlet-api
                    2.5
                

                
                    javax.servlet
                    jstl
                    1.2
                

            

        

    1.完善用户信息
        a.User类添加新的字段
            email
            nickname
            class User{
                    //...
                    private String email;
                    private String nickName ;
                    //getXxx/setXxx
                }

        b.修改mysql数据库
            mysql> alter table users add email varchar(20) ;
            mysql> alter table users add nickname varchar(20) ;

        c.修改映射文件resources/User.hbmx.xml
            
            

    2.编写工具类 -- 校验用户信息
        package com.test.eshop.util;

        import java.util.Collection;

        /**
         * 用户校验工具类
         */
        public class ValidateUtil {

            /**
             * 判断集合有效性
             */
            public static boolean isValid(Collection col){
                if(col == null || col.isEmpty())
                    return false ;
                return true ;
            }

        }

    3.拓展用户服务接口UserService.java
        public interface UserService extends  BaseService{

            //判断邮箱是否注册
            public boolean isRegisted(String email) ;

        }

    4.完善用户服务接口的实现类UserServiceImpl.java
        package com.test.eshop.service.impl;


        import com.test.eshop.dao.BaseDao;
        import com.test.eshop.model.User;
        import com.test.eshop.service.UserService;
        import com.test.eshop.util.ValidateUtil;
        import org.springframework.stereotype.Repository;
        import org.springframework.stereotype.Service;

        import javax.annotation.Resource;
        import java.util.List;

        @Service("userService")
        public class UserServiceImpl extends BaseServiceImpl implements UserService {

            /**
             * 重新该方法,需要注入指定的UserDao对象
             */
            @Resource(name = "userDao")
            public void setDao(BaseDao dao) {
                super.setDao(dao);
            }

            /**
             * 判断邮箱是否注册
             */
            public boolean isRegisted(String email) {
                String hql = "from User u where u.email = ?" ;
                List list = this.findByHQL(hql,email);
                return ValidateUtil.isValid(list) ;
            }
        }



    5.编写控制器UserController.java
        package com.test.eshop.web.controller;

        import com.test.eshop.model.User;
        import com.test.eshop.service.UserService;
        import org.springframework.stereotype.Controller;
        import org.springframework.ui.Model;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;

        import javax.annotation.Resource;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpSession;
        import java.util.List;

        /**
         * 用户Controller
         */
        @Controller
        public class UserController {

            //注入userService给us变量
            @Resource(name = "userService")
            private UserService us;

            /**
             *去注册页面
             */
            @RequestMapping(value = "/toRegPage",method = RequestMethod.GET)
            public String toRegPage(){
                return "userReg" ;
            }

            /**
             *去登录页面
             */
            @RequestMapping(value = "/toLoginPage",method = RequestMethod.GET)
            public String toLoginPage(){
                return "login" ;
            }

            /**
             * 从注册页面取得注册用户和密码,并跳转到登录界面
             */
            /**
             * 完成注册
             */
            @RequestMapping(value = "/doReg",method = RequestMethod.POST)
            public String doReg(User user, HttpServletRequest req, Model m){
                //得到确认密码
                String confirmPass = req.getParameter("confirmPass");

                //1.判断密码是否一致
                if(!user.getPassword().equals(confirmPass)){
                    m.addAttribute("error.password.nosame","两次密码输入不一致,确认后请重新输入!!");
                    return "userReg" ;
                }

                //2.判断email是否唯一
                boolean b = us.isRegisted(user.getEmail());
                if(b){
                    m.addAttribute("error.email.registed","邮箱已经注册!");
                    return "userReg" ;
                }

                //保存用户
                us.saveEntity(user);
                System.out.println("注册成功了!");
                return "login" ;
            }

            /**
             * 登录用户验证程序
             */
            @RequestMapping(value = "/doLogin",method = RequestMethod.POST)
            public String doLogin(User u, HttpSession hs , Model m){
                String hql = "from User u where u.name = ? and u.password = ?";
                List list = us.findByHQL(hql, u.getName(), u.getPassword());
                if (list == null || list.isEmpty()) {
                    System.out.println("登录失败" + u.getName() + ":" + u.getPassword());
                    //向客户端回传数据
                    m.addAttribute("error","用户名/密码验证失败,请重试!!");

                } else {
                    //验证成功,将用户信息放入session中,作为会话保存
                    User user = list.get(0);
                    hs.setAttribute("name",user.getName());
                    System.out.println("登录成功" + u.getName() + ":" + u.getPassword());
                }
                return "index" ;
            }

        }


    6.编写首页index.html
        
        
        
            
            首页.html
        
        
            this is a page!
            用户注册

        
        

    7.编写注册界面userReg.jsp
        <%@ page contentType="text/html;charset=UTF-8" language="java" %>
        <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
        
        
            用户注册页面.jsp
        
        
        
Username :
Password :
ConfirmPass :
Email :
NickName:
8.编写登录界面login.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> 用户登录页面.jsp
Username :
Password :
9.编写欢迎界面index.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page session="true" %> 欢迎页面index.jsp 欢迎 二、添加用户管理员 ------------------------------------------------------- 1.BaseService.java //添加方法声明 public List findAllEntities(); 2.基本服务实现类BaseServiceImp.java //添加方法的实现 /** * BaseService实现类. */ public abstract class BaseServiceImpl implements BaseService { ... private Class clazz ; public BaseServiceImpl(){ ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass(); clazz = (Class)type.getActualTypeArguments()[0]; } ... //查询所有实体 public List findAllEntities(){ String hql = "from " + clazz.getSimpleName() ; return this.findByHQL(hql) ; } } 3.添加管理员控制器AdminController.java package com.test.eshop.web.controller; import com.it18zhang.eshop.model.User; import com.it18zhang.eshop.service.UserService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import javax.annotation.Resource; import java.util.List; /** * 管理员控制器 */ @Controller public class AdminController { @Resource private UserService us ; /** * 查看用户列表 */ @RequestMapping(value = "/admin/userlist",method = RequestMethod.GET) public String userList(Model m){ List allUsers = us.findAllEntities(); m.addAttribute("allUsers",allUsers); return "userList" ; } } 4.编写userList.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> userList.jsp
ID Name password nickName email regDate
三、添加管理员功能 -- 删除用户/查看用户/更改用户信息 -------------------------------------------------- 1.编辑控制器AdminController.java package com.test.eshop.web.controller; import com.test.eshop.model.User; import com.test.eshop.service.UserService; import com.test.eshop.service.impl.UserServiceImpl; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import javax.annotation.Resource; import java.util.List; @Controller public class AdminController { @Resource(name = "userService") private UserService us ; /** * 查看用户列表 */ @RequestMapping(value = "/admin/userlist",method = RequestMethod.GET) public String userList(Model m){ List allUsers = us.findAllEntities(); m.addAttribute("allUsers",allUsers); return "userList" ; } /** * 删除用户 */ @RequestMapping(value="/admin/delUser",method = RequestMethod.GET) public String delUser(@RequestParam("uid") int uid){ User u = new User(); u.setId(uid); us.deleteEntity(u); //重定向到新的url地址 return "redirect:/admin/userlist"; } //查看用户信息 @RequestMapping(value="/admin/viewUser",method = RequestMethod.GET) public String viewUser(@RequestParam("uid") int uid,Model m){ User u = us.getEntity(uid); m.addAttribute("user",u); return "/viewUser"; } //编辑用户信息 @RequestMapping(value="/admin/editUser",method = RequestMethod.GET) public String editUser(@RequestParam("uid") int uid,Model m){ User u = us.getEntity(uid); m.addAttribute("user",u); return "editUser"; } //提交更改的用户信息 @RequestMapping(value="/admin/updateUser",method = RequestMethod.POST) public String updateUser(User u){ us.updateEntity(u); return "redirect:/admin/userlist"; } } 2.修改userList.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 展示用户列表界面.jsp
ID Name password nickName email regDate 删除
">删除 ">查看 ">编辑
3.添加editUser.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 编辑用户信息界面.jsp
"> Username :">
Password :">
ConfirmPass :
Email :">
NickName:">
4.添加viweUser.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 查看用户信息界面.jsp
ID
Name
Password
Email
NickName
regDate

你可能感兴趣的:(大数据,项目实战)