一、入门
前提
先导jar包依赖
jsp里面引入css和js
< !-- 引入 layui.css -->
< link rel=“stylesheet” href=“${pageContext.request.contextPath}/static/js/layui.css”>
< !-- 引入 layui.js -->
< script src=“${pageContext.request.contextPath}/static/js/layui/layui.js”>< /script >
案例1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- 引入 layui.css -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/js/layui.css">
<!-- 引入 layui.js -->
<script src="${pageContext.request.contextPath}/static/js/layui/layui.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
请输入,<input type="text" value="默认值asaacx" id="layui_demo1_input" >
<button id="layui_demo1_btn">点我</button>
<script>
//导入js相对应的模块jquery.js
//将模块赋值给变量
layui.use(['layer', 'form','jquery'], function(){
var layer = layui.layer
,form = layui.form
,$ = layui.jquery;
$('#layui_demo1_btn').click(function(){
var layui_demo1_input = $("#layui_demo1_input").val();
layer.msg(layui_demo1_input);
});
layer.msg('Hello World');
});
</script>
</body>
</html>
运行结果
案例2:使用自定义模块
先搞一个mymod,
下面展示 mymod代码
。
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的配置文件
下面展示mymodule_config.js代码
。
//config的设置是全局的
layui.config({
base: 'static/js/layui/mymodule/js/' //假设这是你存放拓展模块的根目录
}).extend({ //设定模块别名
mymod: 'mymod' //如果 mymod.js 是在根目录,也可以不用设定别名
});
下面展示 案例2代码
。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- 引入 layui.css -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/js/layui.css">
<!-- 引入 layui.js -->
<script src="${pageContext.request.contextPath}/static/js/layui/layui.js"></script>
<script src="${pageContext.request.contextPath}/static/js/layui/mymodule/js/mymod.js"></script>
<script src="${pageContext.request.contextPath}/static/js/layui/mymodule/mymodule_config.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
请输入,<input type="text" value="默认值asaacx" id="layui_demo1_input" >
<button id="layui_demo1_btn">点我</button>
请输入相关文字,<input type="text" value="默认值" id="layui_demo2_input" >
<button id="layui_demo2_btn">点我</button>
<script>
//导入js相对应的模块jquery.js
//将模块赋值给变量
layui.use(['layer', 'form','jquery','mymod'], function(){
var layer = layui.layer
,form = layui.form
,mymod = layui.mymod
,$ = layui.jquery;
$('#layui_demo1_btn').click(function(){
var layui_demo1_input = $("#layui_demo1_input").val();
layer.msg(layui_demo1_input);
});
//给自定义模块按钮添加点击事件
$('#layui_demo2_btn').click(function(){
var layui_demo2_input = $("#layui_demo2_input").val();
mymod.say(layui_demo2_input);
});
layer.msg('Hello World');
});
</script>
</body>
</html>
运行结果
二、json:数组或对象转成字符串
json对象字符串的来源有两个:map和对象
json数组字符串的来源有两个:list《map》和list《user》
下面展示 代码
。
package com.yzp.demo1;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class YzpDemo1 {
public static void main(String[] args) throws Exception {
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("name", "猪崽");
map1.put("age", 19);
ObjectMapper om = new ObjectMapper();
// writeValueAsString将对象/数组转成json字符串
System.out.println(om.writeValueAsString(map1));
User user = new User("猪崽",10);
System.out.println(om.writeValueAsString(user));
}
}
class User{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
public User() {
// TODO Auto-generated constructor stub
}
}
运行结果
package com.yzp.demo1;
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 YzpDemo1 {
public static void main(String[] args) throws Exception {
ArrayList<Object> list1 = new ArrayList<>();
ArrayList<Object> list2 = new ArrayList<>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("name", "猪崽");
map1.put("age", 19);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("name", "荣崽");
map2.put("age", 12);
list1.add(map1);
list1.add(map2);
ObjectMapper om = new ObjectMapper();
// writeValueAsString将对象/数组转成json字符串
System.out.println(om.writeValueAsString(map1));
User user = new User("猪崽",10);
User user2 = new User("文逼崽",10);
list2.add(user);
list2.add(user2);
System.out.println(om.writeValueAsString(user));
System.out.println(om.writeValueAsString(list1));
System.out.println(om.writeValueAsString(list2));
}
}
class User{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
public User() {
// TODO Auto-generated constructor stub
}
}
三、登录基本功能
首先先写一个jsp页面
下面展示登录jsp代码
。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="common/header.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Language" content="zh-CN">
<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>
<title>Insert title here</title>
</head>
<body>
<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 type="text/javascript">
layui.use(['jquery','layer'],function(){
let $=layui.jquery//es6
,layer=layui.layer;
$("#login").click(function(){
$.ajax({
url:"${pageContext.request.contextPath}/user.action?methodName=login"
,dataType:'json'
,data:{
username:$("#username").val(),
password:$("#password").val()
}
,success:function(data){
console.log(data);
if(data.code==200){
layer.alert(data.msg,{icon:1});
location.href="main.jsp";
}else{
layer.alert(data.msg,{icon:2});
}
}
})
})
})
</script>
</body>
</body>
</html>
然后写一个useraction
下面展示 useraction代码
。
package com.zking.web;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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();
//写一个方法处理前台的请求
public String login(HttpServletRequest req, HttpServletResponse resp) {
Map<String, Object> map = new HashMap<String, Object>();
if("ww".equals(user.getUsername()) && "123123".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;
}
}
在mvc.xml里面配置
运行结果:登录失败的
登录成功的:
成功之后的页面跳转
代码优化
下面展示 优化代码
。
package com.zking.web;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zking.entity.User;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.R;
import com.zking.util.ResponseUtil;
public class UserAction extends ActionSupport implements ModelDriver<User> {
private User user = new User();
//写一个方法处理前台的请求
// public String login(HttpServletRequest req, HttpServletResponse resp) {
// Map<String, Object> map = new HashMap<String, Object>();
// if("ww".equals(user.getUsername()) && "123123".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;
// }
public String login(HttpServletRequest req, HttpServletResponse resp) {
try {
if("ww".equals(user.getUsername()) && "123123".equals(user.getPassword())) {
//登录成功
ResponseUtil.writeJson(resp, new R().data("code", 200).data("msg", "成功!"));
}else {
//登录失败
ResponseUtil.writeJson(resp, new R().data("code", 0).data("msg", "账户或密码错误"));
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
之所以能够一行代码解决是因为有一个工具类R
下面展示一些 R工具类代码
。
package com.zking.util;
import java.util.HashMap;
public class R extends HashMap{
public R data(String key, Object value) {
this.put(key, value);
return this;
}
}