PHP前端开发

layui登录界面美化效果展示

百变鹏仔 1个月前 (11-16) #layui
文章标签 界面

完整代码展示如下:

(学习视频分享:编程入门)

1、前端html

login.html

nbsp;html&gt;<meta><meta><title>登录</title><link><link>    <p>          </p>
            

                用户名:                

                    ${UnameErrMsg?if_exists}                

                        

                密码:                

                    ${PwdErrMsg?if_exists}                

                        

                验证码:                

                    ${yzmErrMsg?if_exists}
                @@##@@                

                        

                            登录            

                  <script></script>

登录界面样式

@CHARSET "UTF-8";body{    background-image:url(/static/images/login-bg.png);}.login {    padding-top: 15%;    width: 26%;}  .btn-center{    text-center:center;    margin:0 auto;}

2、写控制器

其中就是login方法与ulogin方法

controller包下IndexController类

package cn.pangpython.controller;  import com.jfinal.aop.Before;import com.jfinal.core.Controller;import com.jfinal.ext.kit.SessionIdKit;  import cn.pangpython.model.User;import cn.pangpython.utils.DateUtils;import cn.pangpython.utils.MD5;import cn.pangpython.validate.RegistValidator;import cn.pangpython.validate.UserLoginValidator;  /** * @author pangPython * 主页控制器 */public class IndexController extends Controller {    public void index(){        renderText("index");    }      //渲染注册页面    public void regpage(){        render("regist.html");    }      //处理注册    @Before(RegistValidator.class)    public void regist(){        String pwd = getPara("user.pwd");        String confirm = getPara("reg.confirm");          //验证码验证        boolean result = validateCaptcha("reg.yzm");        if(!result){            setAttr("yzmErrMsg", "验证码错误!");            render("regist.html");            return;        }        //确认密码验证        if(!pwd.equals(confirm)){            setAttr("confirmErrMsg", "请正确填写确认密码!");            render("regist.html");            return;        }          String uname = getPara("user.name");        User user = getModel(User.class);        String reg_time = DateUtils.dateToUnixTimestamp(DateUtils.getNowTime())+"";        //使用用户注册日期作为md5密码加密的盐值,可节省一个salt数据库字段        pwd = MD5.GetMD5Code(pwd+reg_time);          //给user实体类填充数据        user.setName(uname);        user.setPwd(pwd);        user.setRegTime(reg_time);          //使用jfinal的保存操作        user.save();          renderText("注册成功!");    }      public void login(){        render("login.html");    }      @Before(UserLoginValidator.class)    public void ulogin(){        // 验证码结果        boolean result = validateCaptcha("yzm");        if (!result) {            setAttr("yzmErrMsg", "验证码错误!");            render("login.html");            return;        }          String uname = getPara("user.name");        String sql = "select * from user where name = ? limit 1";          User user = User.dao.findFirst(sql, uname);        if (user != null) {            String pwd = MD5.GetMD5Code(getPara("user.pwd") + user.getRegTime());              if (user.getPwd().equals(pwd)) {                  // 生成唯一标识                String sessionId = SessionIdKit.me().generate(getRequest());                // 设置服务器端session                setSessionAttr(sessionId, user);                // 设置用户端cookie                setCookie("cuser", sessionId, 60000);                //redirect("/user");                renderText("登录成功!");              } else {                // 密码不正确                setAttr("UnameErrMsg", "用户名或密码不正确!");                render("login.html");            }          } else {            // 用户名不存在            setAttr("UnameErrMsg", "用户名不存在!");            render("login.html");        }      }  }

3、写登录验证器

validator包下的UserLoginValidator继承JFinal的Validator

import com.jfinal.core.Controller;import com.jfinal.validate.Validator;  public class UserLoginValidator extends Validator {      @Override    protected void handleError(Controller controller) {        controller.keepPara();      }      @Override    protected void validate(Controller arg0) {        validateRequired("user.name", "UnameErrMsg", "请输入用户名!");        validateRequired("user.pwd", "PwdErrMsg", "请输入密码!");        validateRequired("yzm", "yzmErrMsg", "请输入验证码!");    }  }

效果展示: