注册模块

注册模块

基本原理:

从/reglogin页面中提交username和password,此页面的form表单action–》/reg进入controller中方法,requestMapping(“/reg”),注册,addUser,和response.set(cookie),并model.addAtri(“msg”,msg). 最后跳转到/(主页)。

这里设计3个urI,提交前:/reglogin,提交时:/reg,提交后:/。

输入时model,@param,model;

返回的是model.addAttri(”msg“),和跳转页面

====================下面是详细解析==================

0、准备UML类

1、建立Bean.和SQL语句。

2、在DAO层中写入SQL语句,通过bean的规范把类中的属性字段注入。

3、测试Dao层,检测是否可以实现增删改查

4、写service层,例如注册,实际上就是Dao.add(bean);(传入的参数是)

5、测试service【基本上不用测试】

6、写页面传入参数

注册模块主要分为两个部分:

1、基本的dao+SQl+bean等

2、自身的业务service+controller

二、自身的业务service+controller:

总结来看service中,对业务进行处理,并返回model层需要属性并存储到map中,主要是准备数据。

Controller层中调用Service层中方法即可得到 map,Controller根据map不同来跳转到到注册时候成功,失败的jsp页面。

servicer层中,主要功能的有:

输入的是页面提交数据:username和password

新建一个msgMap用来返回到页面的交互验证

1合法性检测:username不能为空,password不能为空,username不能重复,如果出现上述则加入到HashMap中用于返回。

2调用UserDao.add(User),这里user已经setXXX了。

3增加一个ticket(userId,expired,ticket,status)


    public HashMap<String,String>  register(String username,String password){
        HashMap<String,String> msgMap=new HashMap<>();
        if (StringUtils.isBlank(username)){
            msgMap.put("msg","用户名不能为空");
            return msgMap;
        }
        if (StringUtils.isBlank(password)){
            msgMap.put("msg","密码不能为空");
            return msgMap;
        }
    //下面有一个业务,需要判断你当前注册的user在数据库中时候存在
    //所以需要把数据库中user提取出来 getUser(String username)
       User userDB=userDao.selectByName(username);
        if (userDB!=null){//这里表示user在数据库中已经存在了
            msgMap.put("msg","用户名已经存在了");
        return msgMap;
        }

    //上面是检测合法性,下面开始真正的注册,也就是开始加入user到一个数据库分钟
    User userReg=new User();
    userReg.setName(username);
    //随机生成一段盐
    userReg.setSalt(UUID.randomUUID().toString().substring(0,5));
    userReg.setPassword(WendaUtil.MD5(password+userReg.getSalt()));
        userReg.setHeadUrl(String.format("http://images.nowcoder.com/head/%dt.png",new Random().nextInt(1000)));

    userDao.addUser(userReg);


    String ticket=addLoginTicket(userReg.getId());
    //需要下发到浏览器中
    msgMap.put("ticket",ticket);
    return msgMap;

    }

controller层中,输入的有:

1、由model(用于返回到页面交互的map等数据),

2、@param页面提交的参数,

3、httpResponse等参数,可以用来设置cookie等

本层主要作用是,根据传入参数,调用service层业务方法,并根据业务不同跳转到不同的页面


      @Autowired
    UserService userService;
    //这里是写入数据用Post:这车过来需要传入两个参数,username和password;
    //这里面的页面之间跳转就是你的业务逻辑,需要思考
    @RequestMapping(path = "/reg/")
    public String register(Model model,
       @RequestParam("username") String username,
       @RequestParam("password") String password,
       HttpServletResponse response){
    //把可能出现的异常try起来
        try {
            HashMap<String,String> map=userService.register(username, password);
            if (map.containsKey("ticket")){
    //把的cookie写入到response之中
                Cookie cookie=new Cookie("ticket",map.get("ticket"));
                cookie.setPath("/");
                response.addCookie(cookie);
                return "redirect:/";
            }else{
            model.addAttribute("msg",map.get("msg"));
            return  "login";
        }
    }catch (Exception e){
            logger.error("注册异常: "+e.getMessage());
            return "login";
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值