easyui补全
1针对类的增删改查
(1)写关于类的增删改查的dao方法
package com.Liuyujian.Ddo;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.Liuyujain.util.JsonBaseDao;
import com.Liuyujain.util.PageBean;
public class TypeDao extends JsonBaseDao {
/**
* 查询
* @param paMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> list(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from tb_type where true";
return super.executeQuery(sql, pageBean);
}
/**
* 修改
* @param paMap
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int edit(Map<String,String[]> paMap) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql="update tb_type set tname=? where tid= ? ";
return super.executeUpdate(sql, new String[] {"tname","tid"},paMap);
}
/**
* 增加
* @param paMap
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int add(Map<String,String[]> paMap) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql="insert into tb_type(tname)values(?) ";
return super.executeUpdate(sql, new String[] {"tname"},paMap);
}
/**
* 删除
* @param paMap
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int dele(Map<String,String[]> paMap) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql="delete from tb_type where tid= ?";
return super.executeUpdate(sql, new String[] {"tid"},paMap);
}
}
(2)写类的selvet
package com.Liuyujian.web;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.Liuyujain.util.PageBean;
import com.Liuyujain.util.ResponseUtil;
import com.Liuyujian.Ddo.TypeDao;
import com.dengrenli.framework.ActionSupport;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TypeAction extends ActionSupport {
TypeDao dao=new TypeDao();
public String list(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
ObjectMapper om=new ObjectMapper();
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
try {
List<Map<String, Object>> list = this.dao.list(req.getParameterMap(), pageBean);
Map<String, Object> map=new HashMap<String,Object>();
map.put("total", pageBean.getTotal());
map.put("rows", list);
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String edit(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
try {
int code=this.dao.edit(req.getParameterMap());
ObjectMapper om=new ObjectMapper();
Map<String, Object> map=new HashMap<String,Object>();
System.out.println(code);
map.put("code", code);
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
public String add(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
try {
int code=this.dao.add(req.getParameterMap());
ObjectMapper om=new ObjectMapper();
Map<String, Object> map=new HashMap<String,Object>();
map.put("code", code);
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
public String dele(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
try {
int code=this.dao.dele(req.getParameterMap());
ObjectMapper om=new ObjectMapper();
Map<String, Object> map=new HashMap<String,Object>();
map.put("code", code);
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
}
(3)jsp页面
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/static/js/public/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/static/js/public/easyui/themes/icon.css">
<script type="text/javascript"
src="${pageContext.request.contextPath}/static/js/public/easyui/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath}/static/js/type.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath}/static/js/public/easyui/jquery.easyui.min.js"></script>
</head>
<body>
<table id="dg"></table>
<div id="dd" class="easyui-dialog" title="操作" style="width:400px;height:200px;"
data-options="iconCls:'icon-save',resizable:true,modal:true, closed:true,buttons:'#bb'">
<form id="ff" method="post">
<input type="hidden" name="tid">
<div>
<label for="tname">tname:</label>
<input class="easyui-validatebox" type="text" name="tname" data-options="required:true" />
</div>
</form>
</div>
<div id="bb">
<a href="#" class="easyui-linkbutton" onclick="add()">增加</a>
<a href="#" class="easyui-linkbutton" onclick="ok()">修改</a>
<a href="#" class="easyui-linkbutton">关闭</a>
</div>
</body>
</html>
(4)写js
$(function(){
$('#dg').datagrid({
url:'TypeAction.action?methodName=list',
fit:true,
fitColumns:true,
pagination:true,
singleSelect:true,
columns:[[
{field:'tid',title:'Id',width:150},
{field:'tname',title:'类别名',width:150},
]] ,
toolbar: [{
iconCls: 'icon-add',
handler: function(){
$('#dd').dialog('open')
$("#ff").form('load',row);
}
},'-',{
iconCls: 'icon-edit',
handler: function(){
$('#dd').dialog('open')
//在datag控件中找到需要回填的数据
var row= $('#dg').datagrid('getSelected');
if(row){
//get_data.php指的是回填的数据
$("#ff").form('load',row);
}
}
},'-',{
iconCls: 'icon-remove',
handler: function(){
var row =$('#dg').datagrid('getSelected');//选择你要删除的行
if(row){//是否选中
$.ajax({
url:$("#ctx").val()+'/TypeAction.action?methodName=dele&&tid='+row.tid, //传一个删除del方法跟serialNo列名值
});
$('#dg').datagrid('reload');//刷新方法
alert('删除成功');
}
else{
alert('删除失败');
}
}
}]
});
})
function ok(){
alert('ok');
$('#ff').form('submit', {
url:'TypeAction.action?methodName=edit',
success:function(data){
//清空
$('#ff').form('clear');
//保存后关闭
$('#dd').dialog('close');
//刷新
$('#dg').datagrid('reload');
}
});
}
function add(){
$('#ff').form('submit', {
url:'TypeAction.action?methodName=add',
success:function(data){
$('#ff').form('clear');
$('#dd').dialog('close');
$('#dg').datagrid('reload');
}
});
}
增加
结果
删除
结果
修改
结果
2:上传图片
(1):工具类找控件
写入jsp页面
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/static/js/public/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/static/js/public/easyui/themes/icon.css">
<script type="text/javascript"
src="${pageContext.request.contextPath}/static/js/public/easyui/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath}/static/js/book.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath}/static/js/public/easyui/jquery.easyui.min.js"></script>
</head>
<body>
<table id="dg"></table>
<div id="dd" class="easyui-dialog" title="操作" style="width:400px;height:200px;"
data-options="iconCls:'icon-save',resizable:true,modal:true, closed:true,buttons:'#bb'">
<form id="ff" method="post">
<input type="hidden" name="bid">
<div>
<label for="bname">bname:</label>
<input class="easyui-validatebox" type="text" name="bname" data-options="required:true" />
</div>
<div>
<!-- <label for="type">type:</label> -->
类型: <select name="type" id="type" style="width:150px;">
<option value="0">--请选择--</option>
<option value="文艺">文艺</option>
<option value="小说">小说</option>
<option value="青春">青春</option>
<option value="奇幻">奇幻</option>
<option value="玄幻">玄幻</option>
<select>
<!-- <input class="easyui-validatebox" type="text" name="type" data-options="required:true" /> -->
</div>
<div>
<label for="price">price:</label>
<input class="easyui-validatebox" type="text" name="price" data-options="required:true" />
</div>
<div>
<label for="picture">picture:</label>
<input class="easyui-filebox" style="width:300px" name="picture">
</div>
</form>
</div>
<div id="dd" class="easyui-dialog" title="操作" style="width:400px;height:200px;"
data-options="iconCls:'icon-save',resizable:true,modal:true, closed:true,buttons:'#cc'">
<from id="xx" method="post">
<input type="text" name="name">
</from>
</div>
<div id="cc">
<a href="#" class="easyui-linkbutton" onclick="eld()">查询</a>
</div>
<div id="bb">
<a href="#" class="easyui-linkbutton" onclick="add()">增加</a>
<a href="#" class="easyui-linkbutton" onclick="ok()">修改</a>
<a href="#" class="easyui-linkbutton">关闭</a>
</div>
</body>
</html>
(3)js
$('#fb').filebox({
buttonText: '选择文件',
buttonAlign: 'left'
})
测试:
结果
3:登录2星权限:
(1):创建dao方法
package com.Liuyujian.Ddo;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.Liuyujain.util.JsonBaseDao;
import com.Liuyujain.util.JsonUtils;
import com.Liuyujain.util.PageBean;
import com.Liuyujain.util.StringUtils;
import com.Liuyujian.entity.TreeNode;
public class MenuDao extends JsonBaseDao {
/**
* 给前台返回tree_data1.json的字符串
*
* @param paMap
* 从前台jsp传递过来的参数集合
* @param pageBean
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public List<TreeNode> listTreeNode(Map<String, String[]> paMap, PageBean pageBean)
throws InstantiationException, IllegalAccessException, SQLException {
List<Map<String, Object>> listMap = this.listMapAuth(paMap, pageBean);
List<TreeNode> listTreeNode = new ArrayList<>();
this.listMapToListTreeNode(listMap, listTreeNode);
return listTreeNode;
}
/**
* [{'Menuid':001,'Menuname':‘学生管理’},{{'Menuid':001,'Menuname':‘后勤管理’}}]
*
* @param paMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> listMap(Map<String, String[]> paMap, PageBean pageBean)
throws InstantiationException, IllegalAccessException, SQLException {
String sql = "select * from t_module where true";
String menuId = JsonUtils.getParamVal(paMap, "id");
if (StringUtils.isNotBlank(menuId)) {
sql += " and pid= " +menuId;
} else {
sql += " and pid= -1";
}
// 这里面存放的是数据库中菜单信息
List<Map<String, Object>> listMap = super.executeQuery(sql, pageBean);
return listMap;
}
//为什么要将id改成pid?
//原因:之前的方法只能查出当前节点的所有子节点,不能将当前节点查询出来
//让权限分配后的树形菜单显示出主目录
public List<Map<String, Object>> listMapAuth(Map<String, String[]> paMap, PageBean pageBean)
throws InstantiationException, IllegalAccessException, SQLException {
String sql = "select * from t_module where true";
String menuId = JsonUtils.getParamVal(paMap, "id");
if (StringUtils.isNotBlank(menuId)) {
sql += " and id in ("+menuId+")";
} else {
System.out.println("2222222");
sql += " and id=1";
}
// 这里面存放的是数据库中菜单信息
List<Map<String, Object>> listMap = super.executeQuery(sql, pageBean);
return listMap;
}
/**
* {'Menuid':001,'Menuname':‘学生管理’}
* -->
* {id:...,text:...}
* @param map
* @param treeNode
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private void mapToTreeNode(Map<String, Object> map, TreeNode treeNode) throws InstantiationException, IllegalAccessException, SQLException {
treeNode.setId(map.get("id")+"");
treeNode.setText(map.get("text")+"");
treeNode.setAttrributes(map);
// 将子节点添加到父节点当中,建立数据之间的父子关系 001
// treeNode.setChildren(children);
Map<String, String[]> childrenMap = new HashMap<>();
childrenMap.put("id", new String[] {treeNode.getId()});
List<Map<String, Object>> listMap = this.listMap(childrenMap, null);
List<TreeNode> listTreeNode = new ArrayList<>();
this.listMapToListTreeNode(listMap, listTreeNode);
treeNode.setChildren(listTreeNode);
}
/**
* [{'Menuid':001,'Menuname':‘学生管理’},{{'Menuid':001,'Menuname':‘后勤管理’}}]
* -->
* tree_data1.json
* @param listMap
* @param listTreeNode
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private void listMapToListTreeNode(List<Map<String, Object>> listMap, List<TreeNode> listTreeNode) throws InstantiationException, IllegalAccessException, SQLException {
TreeNode treeNode = null;
for (Map<String, Object> map : listMap) {
treeNode = new TreeNode();
mapToTreeNode(map, treeNode);
listTreeNode.add(treeNode);
}
}
}
(2)在用户dao方法里写拿id方法
public List<Map<String, Object>> getMenuByUid(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from tb_user2 where true ";
String uid=JsonUtils.getParamVal(paMap, "uid");
if(StringUtils.isNotBlank(uid)){
sql+=" and uid="+uid;
}
return super.executeQuery(sql, pageBean);
}
(3)在UserAction里面写一个登录方法
package com.Liuyujian.web;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.Liuyujain.util.PageBean;
import com.Liuyujain.util.ResponseUtil;
import com.Liuyujian.Ddo.Bookdao;
import com.Liuyujian.Ddo.UserDao;
import com.dengrenli.framework.ActionSupport;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class UserAction extends ActionSupport {
UserDao dao=new UserDao();
public String login(HttpServletRequest req,HttpServletResponse resp) {
try {
//系统中是否有当前用户
try {
Map<String, Object> map = this.dao.list(req.getParameterMap(),null).get(0);
//有
if(map!=null && map.size()>0) {
StringBuffer sb=new StringBuffer();
System.out.println("111111111111111");
//查询用户菜单中间表,获取menuid的集合
List<Map<String, Object>> menuByUid = this.dao.getMenuByUid(req.getParameterMap(),null);
for (Map<String, Object> m : menuByUid) {
sb.append(","+m.get("id"));
}
req.setAttribute("id", sb.substring(1));
return "index";
}else {
//没有
req.setAttribute("hh", "用户不存在");
return "login";
}
} catch (Exception e) {
// TODO Auto-generated catch block
req.setAttribute("hh", "用户不存在");
return "login";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "login";
}
}
public String list(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
ObjectMapper om=new ObjectMapper();
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
try {
List<Map<String, Object>> list = this.dao.list(req.getParameterMap(), pageBean);
Map<String, Object> map=new HashMap<String,Object>();
map.put("total", pageBean.getTotal());
map.put("rows", list);
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String edit(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
try {
int code=this.dao.edit(req.getParameterMap());
ObjectMapper om=new ObjectMapper();
Map<String, Object> map=new HashMap<String,Object>();
System.out.println(code);
map.put("code", code);
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
public String add(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
try {
int code=this.dao.add(req.getParameterMap());
ObjectMapper om=new ObjectMapper();
Map<String, Object> map=new HashMap<String,Object>();
map.put("code", code);
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
public String dele(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
try {
int code=this.dao.dele(req.getParameterMap());
ObjectMapper om=new ObjectMapper();
Map<String, Object> map=new HashMap<String,Object>();
map.put("code", code);
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
}
(3)新增login页面:
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/UserAction.action?methodName=login" method="post">
uid:<input type="text" name="uid">
upwd:<input type="text" name="upwd">
<input type="submit" value="提交">
<p>
${hh}
</p>
</form>
</body>
</html>
4(配置xml)
<?xml version="1.0" encoding="UTF-8"?>
<!--
config标签:可以包含0~N个action标签
-->
<config>
<action path="/menuAction" type="com.Liuyujian.web.MenuAction">
</action>
<!-- <action type="com.Liuyujian.web.MenuTree" path="/userAction">
<forward path="/index.jsp" redirect="false" name="index"/>
<forward path="/login.jsp" redirect="false" name="login"/>
</action> -->
<action type="com.Liuyujian.web.BookAction" path="/bookAction">
</action>
<action type="com.Liuyujian.web.UserAction" path="/UserAction">
<forward name="index" path="/index.jsp" redirect="false" />
<forward name="login" path="/login.jsp" redirect="false" />
</action>
<action type="com.Liuyujian.web.TypeAction" path="/TypeAction">
</action>
</config>
测试1:
结果:
测试2:
结果2: