LayUi介绍&前言

简介: LayUi介绍&前言

1.什么是layui

  layui(谐音:类 UI) 是一套开源的 Web UI 解决方案,采用自身经典的模块化规范,并遵循原生 HTML/CSS/JS 的开发方式,极易上手,拿来即用。其风格简约轻盈,而组件优雅丰盈,从源代码到使用方法的每一处细节都经过精心雕琢,非常适合网页界面的快速开发。layui 区别于那些基于    MVVM 底层的前端框架,却并非逆道而行,而是信奉返璞归真之道。准确地说,它更多是面向后端开发者,你无需涉足前端各种工具,只需面对浏览器本身,让一切你所需要的元素与交互,从这里信手拈来。

  官方网站https://www.layui.com/(已下线)

  参考地址http://layui.org.cn/demo/index.html(已下线,非官网)

  由国人开发(作者贤心),16年出厂的框架,其主要提供了很多好看、方便的样式,并且基本拿来即用,和Bootstrap有些相似,但该框架有个极大的好处就是定义了很多前后端交互的样式接口,如分页表格,只需在前端配置好接口,后端则按照定义好的接口规则返回数据,即可完成页面的展示,极大减少了后端人员的开发成本。

2.layui、easyui与bootstrap的对比

 有趣的对比方式,嘿嘿嘿....

 easyui=jquery+html4(用来做后台的管理界面) 半老徐娘

 bootstrap=jquery+html5 美女 拜金

 layui 清纯少女

     2.1 layui和bootstrap对比(这两个都属于UI渲染框架)

         1.layui是国人开发的一套框架,2016年出来的,现在已更新到2.X版本了。比较新,轻量级,样式简单好看(目前官网已下架,开源了)

         2.bootstrap 相对来说是比较成熟的一个框架,现在已经更新到4.X版本。是一个很成熟的框架,这个大部分人一般都用过

         1)适用范围不一样

     

           1.layui 其实更偏向与后端开发人员使用,在服务端页面上有非常好的效果。

     

           2.适合做后台框架

           3.layui是提供给后端开发人员最好的ui框架,基于DOM驱动,在实现前端交互上比较麻烦,页面的增删改查都需要查询DOM元素。所以在不需要交互的时候,用layui还是不错的(说这句话的人,只能说明你对layui不了解)

           

           4.bootstrap 在前端响应式方面做得很好,PC端和移动端表现都不错。

     

           5.适合做网站

           

           6.如果是类似官网,且需要同时满足PC端和移动端效果,bootstrap 表现很好,但是如果是要交互的网站,比如商城之类,layui显然更好,前后端分离

         2)大小不一样

         

            1.layui 轻量级

            2.bootsrap 因为成熟,所以使用方便,但是同时也是因为成熟,就显得有些冗余

     2.2 layui和easyui对比

         1.easyui 是非开源的,有需要解决的问题的话,就只能等官方更新了

         2.layui是开源的,社区比较活跃,解决问题还是比较快的

         3.easyui诞生的早些,所以功能相对完善一些,很多功能都能是比较健全的

         4.layui就相对来说少一些了,不过,功能都是像官网说的,精雕细琢

         5.layui更符合现在的审美

3.layui入门

  将layui下载到了本地,那么可将其完整地放置到你的项目目录(或静态资源服务器),并在页面中分别引入:

<!-- 引入 layui.css -->
   <link rel="stylesheet" href="xxx/layui.css">
   <!-- 引入 layui.js -->
   <script src="xxx/layui.js"></script>

      3.1 路径问题

      1) 相对路径/绝对路径

      2) base标签

  入门案例:点击弹出框  

4.如何扩展一个layui(自定义模块)    参考 模块规范

  1)第一步:确认模块名,假设为:test.js文件放入项目任意目录下(注意:不用放入layui目录)

  2)编写test.js

 

layui.define(function(exports){ //提示:模块也可以依赖其它模块,如:layui.define('layer', callback);
      var obj = {
         hello: function(str){
            alert('Hello '+ (str||'test'));
         }
      };
      //输出test接口
      exports('test', obj);
   });

  3)设定扩展模块所在的目录,然后就可以在别的JS文件中使用

layui.config({
      base: '/res/js/' //假设这是test.js所在的目录
   }).extend({ //设定模块别名
      test: 'test' //如果test.js是在根目录,也可以不用设定别名
   });
   //使用test
   layui.use('test', function(){
      var test = layui.test;
      test.hello('World!'); //弹出Hello World!
   });

  案例:

  1)弹出hello方法

  2)日期格式转换

5.构建登录页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="common/header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" rev="stylesheet" href="${pageContext.request.contextPath}/static/css/iconfont.css" type="text/css" media="all">
<link rel="stylesheet" rev="stylesheet" href="${pageContext.request.contextPath}/static/css/login.css" type="text/css" media="all">
<style> body{color:#;}a{color:#;}a:hover{color:#;}.bg-black{background-color:#;}.tx-login-bg{background:url(static/images/bg.jpg) no-repeat 0 0;}</style>
</head>
  <body class="tx-login-bg">
        <div class="tx-login-box">
            <div class="login-avatar bg-black"><i class="iconfont icon-wode"></i></div>
        <ul class="tx-form-li row">
          <li class="col-24 col-m-24"><p><input type="text" id="username" placeholder="登录账号" class="tx-input"></p></li>
          <li class="col-24 col-m-24"><p><input type="password" id="password" placeholder="登录密码" class="tx-input"></p></li>
          <li class="col-24 col-m-24"><p class="tx-input-full"><button id="login" class="tx-btn tx-btn-big bg-black">登录</button></p></li>
          <li class="col-12 col-m-12"><p><a href="#" class="f-12 f-gray">新用户注册</a></p></li>
          <li class="col-12 col-m-12"><p class="ta-r"><a href="#" class="f-12 f-gray">忘记密码</a></p></li>
        </ul>
            </div>
<script>
layui.use(['layer', 'jquery'], function(){
  var layer = layui.layer
  ,form = layui.form
  ,$=layui.jquery;
  $("#login").click(function(){
    $.ajax({
      url:'${pageContext.request.contextPath}/user.action?methodName=login',
      dataType:'json',
      data:{
       loginName:$("#username").val(),
       pwd:$("#password").val()
      },
      method:'post',
      success:function(data){
        if(data){
         layer.alert('恭喜'+data.name+'登录成功', {icon: 1});
        }else{
         layer.msg('登录失败', {icon: 5});
        }
      }
    });
  });
});
</script> 
    </body>
</html>

       5.1 dao方法

package com.zking.dao;
import java.util.List;
import com.zking.entity.User;
import com.zking.util.BaseDao;
import com.zking.util.PageBean;
public class UserDao extends BaseDao<User> {
  public List<User> list(User user, PageBean pageBean) throws Exception {
    String sql="select *from t_oa_user where 1 = 1";
    return super.executeQuery(sql, User.class, pageBean);
  }
  public User login(User user) throws Exception {
    String sql="select *from t_oa_user where loginName = '"+user.getLoginName()+"' and pwd ='"+user.getPwd()+"' ";
    List<User> lst = super.executeQuery(sql, User.class, null);
    if(lst != null && lst.size() ==1) {
      return lst.get(0);
    }
    return null;
  }
}

       5.2 Action

package com.zking.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zking.dao.UserDao;
import com.zking.entity.User;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.ResponseUtil;
public class UserAction extends ActionSupport implements ModelDriver<User> {
  private User user = new User();
  private UserDao usedDao = new UserDao();
  public void login(HttpServletRequest req, HttpServletResponse resp) {
    try {
      User u = usedDao.login(user);
      ResponseUtil.writeJson(resp, u);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  @Override
  public User getModel() {
    return user;
  }
}

       5.3 mvc搭建

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <action path="/blog" type="com.zking.web.BlogAction">
    <forward name="list" path="/blogList.jsp" redirect="false" />
    <forward name="toList" path="/blog.action?methodName=list"
      redirect="true" />
    <forward name="toEdit" path="/blogEdit.jsp" redirect="false" />
  </action>
    <action path="/user" type="com.zking.web.UserAction">
  </action>
<!--  <action path="/meetingInfo" type="com.zking.web.MeetingInfoAction"> -->
<!--    <forward name="list" path="/meetingInfoList.jsp" redirect="false" /> -->
<!--    <forward name="toList" path="/meetingInfo.action?methodName=list" -->
<!--      redirect="true" /> -->
<!--    <forward name="toEdit" path="/meetingInfoEdit.jsp" redirect="false" /> -->
<!--  </action> -->
<!--  <action path="/solrBlog" type="com.test.web.SolrBlogAction"> -->
<!--    <forward name="list" path="/solrBlogList.jsp" redirect="false" /> -->
<!--    <forward name="toList" path="/solrBlog.action?methodName=list" -->
<!--      redirect="true" /> -->
<!--    <forward name="toEdit" path="/solrBlogEdit.jsp" redirect="false" /> -->
<!--  </action> -->
  <action path="/studentBlog" type="com.test.web.StudentBlogAction">
    <forward name="list" path="/studentBlogList.jsp" redirect="false" />
    <forward name="toList" path="/studentBlog.action?methodName=list" redirect="true" />
    <forward name="toEdit" path="/studentBlogEdit.jsp" redirect="false" />
  </action>
  <action path="/solrBlog" type="com.xiaoli.web.SolrBlogAction">
    <forward name="list" path="/solrBlogList.jsp" redirect="false" />
    <forward name="toList" path="/solrBlog.action?methodName=list" redirect="true" />
    <forward name="toEdit" path="/solrBlogEdit.jsp" redirect="false" />
  </action>
</config>

6.主页搭建

  参考地址:http://layui.org.cn/demo/admin.html

 


相关文章
|
7月前
|
JSON 前端开发 JavaScript
layui介绍及入门基础(看我这篇就会了!!!)
layui介绍及入门基础(看我这篇就会了!!!)
106 0
|
7月前
|
前端开发 JavaScript Java
Layui之入门
Layui之入门
59 0
|
6月前
|
前端开发 JavaScript API
Layui的入门
Layui的入门
55 0
|
8月前
|
XML 前端开发 程序员
Layui入门及案例演示2
Layui入门及案例演示2
45 0
|
7月前
|
前端开发 JavaScript 搜索推荐
layui入门
layui入门
36 0
|
5月前
|
JavaScript 前端开发
layui使用实践总结
layui使用实践总结
45 0
|
5月前
|
前端开发 JavaScript 开发者
Layui 简单介绍及入门
Layui 简单介绍及入门
|
5月前
|
前端开发 JavaScript 开发者
Layui(入门)
Layui(入门)
31 0
|
5月前
|
前端开发 JavaScript Java
一篇万字博客带你入门layUI
一篇万字博客带你入门layUI
49 0
|
6月前
|
前端开发 JavaScript Java
LayUI入门简介(详解)
LayUI入门简介(详解)
510 0