- 一、layui入门
- 1、什么是layui
- 2、layui入门
- 3、自定义模块
- 二、json回顾
- 三、layui登录基本功能
layui(谐音:类 UI) 是一套开源的 Web UI 解决方案,采用自身经典的模块化规范,并遵循原生 HTML/CSS/JS 的开发方式,极易上手,拿来即用。其风格简约轻盈,而组件优雅丰盈,从源代码到使用方法的每一处细节都经过精心雕琢,非常适合网页界面的快速开发。layui 区别于那些基于 MVVM 底层的前端框架,却并非逆道而行,而是信奉返璞归真之道。准确地说,它更多是面向后端开发者,你无需涉足前端各种工具,只需面对浏览器本身,让一切你所需要的元素与交互,从这里信手拈来。
2、layui入门官方网站:https://www.layui.com/(已下线)
参考地址:http://layui.org.cn/demo/index.html(已下线,非官网)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
请输入:
运行效果:
jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
请输入:
请输入相关文字:
mymod.js:
layui.define(function(exports){ //提示:模块也可以依赖其它模块,如:layui.define('mod1', callback);
var obj = {
hello: function(str){
alert('Hello '+ (str||'mymod'));
},
say: function(str){
alert('say '+ (str||'mymod'));
}
};
//输出 mymod 接口
exports('mymod', obj);
});
mymodule_config.js:
//config的设置是全局的
layui.config({
base: 'static/js/layui/mymodule/js/' //假设这是你存放拓展模块的根目录
}).extend({ //设定模块别名
mymod: 'mymod' //如果 mymod.js 是在根目录,也可以不用设定别名
});
运行效果:
package com.xnx.demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Demo1{
public static void main(String[] args) throws Exception {
ArrayList
运行效果:
实体类:
package com.xnx.entity;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
// TODO Auto-generated constructor stub
}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
}
头部js代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
UserAction代码:
package com.xnx.web; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.xnx.entity.User; import com.zking.framework.ActionSupport; import com.zking.framework.ModelDriver; import com.zking.util.ResponseUtil; public class UserAction extends ActionSupport implements ModelDriver{ private User user=new User(); // 写一个方法处理前台的请求 public String login(HttpServletRequest req, HttpServletResponse resp) { Map map=new HashMap<>(); if("zhangsan".equals(user.getUsername())&&"123456".equals(user.getPassword())) { // 登录成功 map.put("code", 200); map.put("msg", "成功"); }else { // 登录失败 map.put("code", 0); map.put("msg", "账户密码错误"); } try { ResponseUtil.writeJson(resp, map); } catch (Exception e) { e.printStackTrace(); } return null; } @Override public User getModel() { // TODO Auto-generated method stub return user; } }
jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="common/header.jsp" %>
Insert title here
xml配置:
运行效果:
登录成功后:



