将之前写好的ssm复制,重新命名为myecpbm,然后修改相关文件名和配置,文件目录结构和要修改的项目名如下图一
图一
1、选中改文件,Window==>show view==>Navigator
2、pom.xml文件
3、web==>WEB-INF==>dispatcherServlet-servlet.xml 下
4、到次修改完毕
验证登录,成功证明前面的配置完成了
开始搭建
1、在com.myecpbm.pojo包中,创建实体类 AdminInfo
package com.myecpbm.pojo;
import java.util.List;
public class AdminInfo {
private int id;
private String name;
private String pwd;
// 关联的属性
private List<Functions> fs;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public List<Functions> getFs() {
return fs;
}
public void setFs(List<Functions> fs) {
this.fs = fs;
}
}
2、 在com.myecpbm.pojo包中,创建实体类 Funcions
package com.myecpbm.pojo;
import java.util.HashSet;
import java.util.Set;
public class Functions implements Comparable<Functions> {
private int id;
private String name;
private int parentid;
private boolean isleaf;
// 关联的属性
private Set ais = new HashSet();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getParentid() {
return parentid;
}
public void setParentid(int parentid) {
this.parentid = parentid;
}
public boolean isIsleaf() {
return isleaf;
}
public void setIsleaf(boolean isleaf) {
this.isleaf = isleaf;
}
public Set getAis() {
return ais;
}
public void setAis(Set ais) {
this.ais = ais;
}
// @Override
public int compareTo(Functions arg0) {
return ((Integer) this.getId()).compareTo((Integer) (arg0.getId()));
}// 比较2个ID,相同时返回0,否则返回非0数
}
3、再com.myecpbm.dao包中创建接口AdminInfoDao
package com.myecpbm.dao;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import com.myecpbm.pojo.AdminInfo;
public interface AdminInfoDao {
// 根据登录名和密码查询管理员
@Select("select * from admin_info where name = #{name} and pwd = #{pwd}")
public AdminInfo selectByNameAndPwd(AdminInfo ai);
// 根据管理员id获取管理员对象及关联的功能集合
@Select("select * from admin_info where id = #{id}")
@Results({ @Result(id = true, column = "id", property = "id"), @Result(column = "name", property = "name"), @Result(column = "pwd", property = "pwd"),
@Result(column = "id", property = "fs", many = @Many(select = "com.myecpbm.dao.FunctionDao.selectByAdminId", fetchType = FetchType.EAGER)) })
AdminInfo selectById(Integer id);
}
4、再com.myecpbm.dao包中创建接口FunctionDao
package com.myecpbm.dao;
import java.util.List;
import org.apache.ibatis.annotations.Select;
import com.myecpbm.pojo.Functions;
public interface FunctionDao {
// 根据管理员id,获取功能权限
@Select("select * from functions where id in (select fid from powers where aid = #{aid} )")
public List<Functions> selectByAdminId(Integer aid);
}
5、在com.myecpbm.service包中,创建业务逻辑层接口AdminInfoService
package com.myecpbm.service;
import com.myecpbm.pojo.AdminInfo;
public interface AdminInfoService {
// 登录验证
public AdminInfo login(AdminInfo ai);
// 根据管理员编号,获取管理员对象及关联的功能权限
public AdminInfo getAdminInfoAndFunctions(Integer id);
}
6、在com.myecpbm.service.impl包中,创建接口实现类AdminInfoServiceImpl
package com.myecpbm.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.myecpbm.dao.AdminInfoDao;
import com.myecpbm.pojo.AdminInfo;
import com.myecpbm.service.AdminInfoService;
@Service("adminInfoService")
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public class AdminInfoServiceImpl implements AdminInfoService {
@Autowired
private AdminInfoDao adminInfoDao;
// @Override
public AdminInfo login(AdminInfo ai) {
return adminInfoDao.selectByNameAndPwd(ai);
}
// @Override
public AdminInfo getAdminInfoAndFunctions(Integer id) {
return adminInfoDao.selectById(id);
}
}
7、在webapp根目录下创建admin_login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>电子商务平台——后台登录页</title>
<!-- 引入EasyUI的相关css和js文件 -->
<link href="EasyUI/themes/default/easyui.css" rel="stylesheet"
type="text/css" />
<link href="EasyUI/themes/icon.css" rel="stylesheet" type="text/css" />
<link href="EasyUI/demo.css" rel="stylesheet" type="text/css" />
<script src="EasyUI/jquery.min.js" type="text/javascript"></script>
<script src="EasyUI/jquery.easyui.min.js" type="text/javascript"></script>
<script src="EasyUI/easyui-lang-zh_CN.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
function clearForm() {
$('#adminLoginForm').form('clear');
}
function checkAdminLogin() {
$("#adminLoginForm").form("submit", {
// 向控制器类AdminInfoController中login方法发送请求
url : 'admininfo/login',
success : function(result) {//处理返回success的回调函数
var result = eval('(' + result + ')');//将字符串result转换为表达式
if (result.success == 'true') {
window.location.href = 'admin.jsp';
$("#adminLoginDlg").dialog("close");
} else {
$.messager.show({
title : "提示信息",
msg : result.message
});
}
}
});
}
</script>
<div id="adminLoginDlg" class="easyui-dialog"
style="left: 550px; top: 200px;width: 300;height: 200"
data-options="title:'后台登录',buttons:'#bb',modal:true">
<form id="adminLoginForm" method="post">
<table style="margin:20px;font-size: 13;">
<tr>
<th >用户名</th>
<td><input class="easyui-textbox" type="text" id="name"
name="name" data-options="required:true" value="admin"></input></td>
</tr>
<tr>
<th>密码</th>
<td><input class="easyui-textbox" type="text" id="pwd"
name="pwd" data-options="required:true" value="123456"></input></td>
</tr>
</table>
</form>
</div>
<div id="bb">
<a href="javascript:void(0)" class="easyui-linkbutton"
onclick="checkAdminLogin()">登录</a> <a href="javascript:void(0)"
class="easyui-linkbutton" onclick="clearForm();">重置</a>
</div>
</body>
</html>
8、在com.myecpbm.controller包中创建控制类AdminInfoController
package com.myecpbm.controller;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.myecpbm.pojo.AdminInfo;
import com.myecpbm.pojo.Functions;
import com.myecpbm.pojo.TreeNode;
import com.myecpbm.service.AdminInfoService;
import com.myecpbm.util.JsonFactory;
@SessionAttributes(value = { "admin" })
@Controller
@RequestMapping("/admininfo")
public class AdminInfoController {
@Autowired
private AdminInfoService adminInfoService;
@RequestMapping(value = "/login", produces = "text/html;charset=UTF-8")
@ResponseBody
public String login(AdminInfo ai, ModelMap model) {
// 后台登录验证
AdminInfo admininfo = adminInfoService.login(ai);
if (admininfo != null && admininfo.getName() != null) {
// 验证通过后,再判断是否已为该管理员分配功能权限
if (adminInfoService.getAdminInfoAndFunctions(admininfo.getId()).getFs().size() > 0) {
// 验证通过且已分配功能权限,则将admininfo对象存入model中
model.put("admin", admininfo);
// 以JSON格式向页面发送成功信息
return "{\"success\":\"true\",\"message\":\"登录成功\"}";
} else {
return "{\"success\":\"false\",\"message\":\"您没有权限,请联系超级管理员设置权限!\"}";
}
} else
return "{\"success\":\"false\",\"message\":\"登录失败\"}";
}
@RequestMapping("getTree")
@ResponseBody
public List<TreeNode> getTree(@RequestParam(value = "adminid") String adminid) {
// 根据管理员编号,获取AdminInfo对象
AdminInfo admininfo = adminInfoService.getAdminInfoAndFunctions(Integer.parseInt(adminid));
List<TreeNode> nodes = new ArrayList<TreeNode>();
// 获取关联的Functions对象集合
List<Functions> functionsList = admininfo.getFs();
// 对List<Functions>类型的Functions对象集合排序
Collections.sort(functionsList);
// 将排序后的Functions对象集合转换到List<TreeNode>类型的列表nodes
for (Functions functions : functionsList) {
TreeNode treeNode = new TreeNode();
treeNode.setId(functions.getId());
treeNode.setFid(functions.getParentid());
treeNode.setText(functions.getName());
nodes.add(treeNode);
}
// 调用自定义的工具类JsonFactory的buildtree方法,为nodes列表中的各个TreeNode元素中的
// children属性赋值(该节点包含的子节点)
List<TreeNode> treeNodes = JsonFactory.buildtree(nodes, 0);
return treeNodes;
}
}
9、在webapp根目录下创建admin.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%
if (session.getAttribute("admin") == null)
response.sendRedirect("/myecpbm/admin_login.jsp");
%>
<html>
<head>
<title>后台管理首页面</title>
<link href="EasyUI/themes/default/easyui.css" rel="stylesheet"
type="text/css" />
<link href="EasyUI/themes/icon.css" rel="stylesheet" type="text/css" />
<link href="EasyUI/demo.css" rel="stylesheet" type="text/css" />
<script src="EasyUI/jquery.min.js" type="text/javascript"></script>
<script src="EasyUI/jquery.easyui.min.js" type="text/javascript"></script>
<script src="EasyUI/easyui-lang-zh_CN.js" type="text/javascript"></script>
</head>
<body class="easyui-layout">
<div data-options="region:'north',border:false"
style="height: 60px; background: #B3DFDA; padding: 10px">
<div align="left">
<div style="font-family: Microsoft YaHei; font-size: 16px;">电商平台后台管理系统</div>
</div>
<div align="right">
欢迎您,<font color="Red">${sessionScope.admin.name}</font>
</div>
</div>
<div data-options="region:'west',split:true,title:'功能菜单'"
style="width: 180px">
<ul id="tt"></ul>
</div>
<div data-options="region:'south',border:false"
style="height: 50px; background: #A9FACD; padding: 10px; text-align: center">powered
by miaoyong</div>
<div data-options="region:'center'">
<div id="tabs" data-options="fit:true" class="easyui-tabs"
style="width: 500px; height: 250px;"></div>
</div>
<script type="text/javascript">
// 为tree指定数据
$('#tt').tree({
url : 'admininfo/getTree?adminid=${sessionScope.admin.id}'
});
$('#tt').tree({
onClick : function(node) {
if ("商品列表" == node.text) {
if ($('#tabs').tabs('exists', '商品列表')) {
$('#tabs').tabs('select', '商品列表');
} else {
$('#tabs').tabs('add', {
title : node.text,
href : 'productlist.jsp',
closable : true
});
}
} else if ("商品类型列表" == node.text) {
if ($('#tabs').tabs('exists', '商品类型列表')) {
$('#tabs').tabs('select', '商品类型列表');
} else {
$('#tabs').tabs('add', {
title : node.text,
href : 'typelist.jsp',
closable : true
});
}
} else if ("查询订单" == node.text) {
if ($('#tabs').tabs('exists', '查询订单')) {
$('#tabs').tabs('select', '查询订单');
} else {
$('#tabs').tabs('add', {
title : node.text,
href : 'searchorder.jsp',
closable : true
});
}
} else if ("创建订单" == node.text) {
if ($('#tabs').tabs('exists', '创建订单')) {
$('#tabs').tabs('select', '创建订单');
} else {
$('#tabs').tabs('add', {
title : node.text,
href : 'createorder.jsp',
closable : true
});
}
} else if ("客户列表" == node.text) {
if ($('#tabs').tabs('exists', '客户列表')) {
$('#tabs').tabs('select', '客户列表');
} else {
$('#tabs').tabs('add', {
title : node.text,
href : 'userlist.jsp',
closable : true
});
}
} else if ("退出系统" == node.text) {
$.ajax({
url : 'admininfo/logout',
success : function(data) {
window.location.href = "admin_login.jsp";
}
})
}
}
});
</script>
</body>
</html>
10、在com.myecpbm.pojo包中创建TreeNode
package com.myecpbm.pojo;
import java.util.List;
public class TreeNode {
private int id; // 节点id
private String text; // 节点名称
private int fid; // 父节点id
private List<TreeNode> children; // 包含的子节点
// 添加set,get方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getFid() {
return fid;
}
public void setFid(int fid) {
this.fid = fid;
}
public List<TreeNode> getChildren() {
return children;
}
public void setChildren(List<TreeNode> children) {
this.children = children;
}
}
11、创建com.myecpbm.util,并在其中创建JsonFactory
package com.myecpbm.util;
import java.util.ArrayList;
import java.util.List;
import com.myecpbm.pojo.TreeNode;
public class JsonFactory {
public static List<TreeNode> buildtree(List<TreeNode> nodes, int id) {
List<TreeNode> treeNodes = new ArrayList<TreeNode>();
for (TreeNode treeNode : nodes) {
TreeNode node = new TreeNode();
node.setId(treeNode.getId());
node.setText(treeNode.getText());
if (id == treeNode.getFid()) {
// 递给调用buildtree方法给TreeNode中的children属性赋值
node.setChildren(buildtree(nodes, node.getId()));
treeNodes.add(node);
}
}
return treeNodes;
}
}
运行admin_login.jsp文件,得到以下结果就成功了