这里写目录标题
一、前言
上篇博客给大家分享了这次项目的界面编写这次给大家分享后端编写
二、登录实现
user实体类
package com.chendongyang.entity;
public class User {
private long id;
private String name;
private String pwd;
private int type;
public long getId() {
return id;
}
public void setId(long 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 int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + ", type=" + type + "]";
}
public User() {}
public User(long id, String name, String pwd, int type) {
super();
this.id = id;
this.name = name;
this.pwd = pwd;
this.type = type;
}
}
userdao 类
package com.chendongyang.dao;
import java.sql.SQLException;
import java.util.List;
import com.chendongyang.entity.User;
import com.chendongyang.util.BaseDao;
import com.chendongyang.util.PageBean;
import com.chendongyang.util.StringUtils;
public class UserDao extends BaseDao<User>{
public User list(User user) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_user where true";
String name=user.getName();
String pwd=user.getPwd();
if(StringUtils.isNotBlank(name)) {
sql +=" and name = '"+name+"'";
}
if(StringUtils.isNotBlank(pwd)) {
sql +=" and pwd = '"+pwd+"'";
}
List<User> users = super.executeQuery(sql, User.class, null);
if(users.size()==0) {
return null;
}
return users.get(0);
}
}
userAction
package com.chendongyang.web;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.chendongyang.dao.UserDao;
import com.chendongyang.entity.User;
import com.chendongyang.framework.ActionSupport;
import com.chendongyang.framework.ModelDriven;
public class UserAction extends ActionSupport implements ModelDriven<User>{
private User user=new User();
private UserDao userDao=new UserDao();
@Override
public User getModel() {
return user;
}
public String login(HttpServletRequest req,HttpServletResponse resp){
try {
user.setPwd(req.getParameter("pwd"));
User curentUser = this.userDao.list(user);
if(curentUser == null) {
return "login";
}
req.getSession().setAttribute("curentUser", curentUser);
} catch (Exception e) {
e.printStackTrace();
return "login";
}
return "main";
}
}
mvc.xml 配置
<action path="/user" type="com.chendongyang.web.UserAction">
<forward name="login" path="/login.jsp" redirect="false" />
<forward name="main" path="/main.jsp" redirect="false" />
</action>
二、登录权限功能实现
下面图片是不同用户登录显示不同的内容
实现代码
rolePermission 实体类
package com.chendongyang.entity;
public class RolePermission {
private long rid;
private long pid;
public long getRid() {
return rid;
}
public void setRid(long rid) {
this.rid = rid;
}
public long getPid() {
return pid;
}
public void setPid(long pid) {
this.pid = pid;
}
@Override
public String toString() {
return "RolePermission [rid=" + rid + ", pid=" + pid + "]";
}
public RolePermission() {}
public RolePermission(long rid, long pid) {
super();
this.rid = rid;
this.pid = pid;
}
}
dao 方法
package com.chendongyang.dao;
import java.sql.SQLException;
import java.util.List;
import com.chendongyang.entity.RolePermission;
import com.chendongyang.util.BaseDao;
import com.chendongyang.util.PageBean;
public class RolePermissionDao extends BaseDao<RolePermission>{
public List<RolePermission> list(RolePermission rolePermission,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_Role_Permission where true ";
long rid=rolePermission.getRid();
if(rid!=0) {
sql+=" and rid="+rid;
}
return super.executeQuery(sql, RolePermission.class, pageBean);
}
}
permissionAction 类修改
package com.chendongyang.web;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.chendongyang.dao.PermissionDao;
import com.chendongyang.dao.RolePermissionDao;
import com.chendongyang.entity.Permission;
import com.chendongyang.entity.RolePermission;
import com.chendongyang.entity.User;
import com.chendongyang.framework.ActionSupport;
import com.chendongyang.framework.ModelDriven;
import com.chendongyang.util.ResponseUtil;
import com.chendongyang.vo.TreeVo;
public class PermissionAction extends ActionSupport implements ModelDriven<Permission> {
private Permission permission = new Permission();
private PermissionDao permissionDao = new PermissionDao();
private RolePermissionDao rolePermissionDao=new RolePermissionDao();
public String menuTree(HttpServletRequest req, HttpServletResponse resp) throws Exception{
try {
/*TreeVo<Permission> toNode = this.permissionDao.toNode(null, null);
List<TreeVo<Permission>> list = new ArrayList<TreeVo<Permission>>();
list.add(toNode);
ResponseUtil.writeJson(resp, list);*/
User curentUser = (User) req.getSession().getAttribute("curentUser");
RolePermission rolePermission=new RolePermission();
rolePermission.setRid(curentUser.getType());
List<RolePermission> rolePermissions = this.rolePermissionDao.list(rolePermission, null);
// ,0,2,3,4,5
StringBuilder pids = new StringBuilder();
for (RolePermission rp : rolePermissions) {
pids.append(",").append(rp.getPid());
}
List<TreeVo<Permission>> nodePermission = this.permissionDao.toNodePermission(pids.substring(1), null);
ResponseUtil.writeJson(resp, nodePermission);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//结果码的配置就是为了,在mvc.xml中寻找到底是重定向还是转发
return null;
}
@Override
public Permission getModel() {
// TODO Auto-generated method stub
return permission;
}
}
permissionDao类修改
package com.chendongyang.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.chendongyang.entity.Permission;
import com.chendongyang.util.BaseDao;
import com.chendongyang.util.BuildTree;
import com.chendongyang.util.PageBean;
import com.chendongyang.vo.TreeVo;
import com.fasterxml.jackson.databind.ObjectMapper;
public class PermissionDao extends BaseDao<Permission> {
/**
* 是直接从数据库获取到的数据
* @param permission
* @param pageBean
* @return
* @throws Exception
*/
public List<Permission> list(Permission permission,PageBean pageBean) throws Exception{
String sql="select * from t_easyui_Permission ";
return super.executeQuery(sql, Permission.class, pageBean);
}
public List<Permission> listPermission(String pids,PageBean pageBean) throws Exception{
String sql="select * from t_easyui_Permission where id in("+pids+")";
return super.executeQuery(sql, Permission.class, pageBean);
}
/**
* 能够将数据库中的数据体现出父子结构
* @param permission
* @param pageBean
* @return
* @throws Exception
*/
public TreeVo<Permission> toNode(Permission permission,PageBean pageBean) throws Exception{
List<Permission> list = this.list(permission, pageBean);
List<TreeVo<Permission>> nodes = new ArrayList<TreeVo<Permission>>();
TreeVo treeVo = null;
for (Permission p : list) {
treeVo = new TreeVo();
treeVo.setId(p.getId()+"");
treeVo.setText(p.getName());
treeVo.setParentId(p.getPid()+"");
Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put("self", p);
treeVo.setAttributes(attributes);
nodes.add(treeVo);
}
return BuildTree.build(nodes);
}
public List<TreeVo<Permission>> toNodePermission(String pids,PageBean pageBean) throws Exception{
List<Permission> list = this.listPermission(pids, pageBean);
List<TreeVo<Permission>> nodes = new ArrayList<TreeVo<Permission>>();
TreeVo treeVo = null;
for (Permission p : list) {
treeVo = new TreeVo<>();
treeVo.setId(p.getId()+"");
treeVo.setText(p.getName());
treeVo.setParentId(p.getPid()+"");
Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put("self", p);
treeVo.setAttributes(attributes);
nodes.add(treeVo);
}
return BuildTree.buildList(nodes, "0");
}
public static void main(String[] args) throws Exception{
PermissionDao psdao = new PermissionDao();
List<Permission> listPermission = psdao.list(null, null);
//通过工具类完成指定的格式输出
List<TreeVo<Permission>> nodes = new ArrayList<TreeVo<Permission>>();
//permission的格式不满足 easyui的tree组件的展现的数据格式的
//目的:将List<permission>转换成List<TreeVo<T>>
//实现:将List<permission>得到的单个Permission转成TreeVo,将TreeVo加入到nodes
TreeVo treeVo = null;
for (Permission p : listPermission) {
treeVo = new TreeVo();
treeVo.setId(p.getId()+"");
treeVo.setText(p.getName());
treeVo.setParentId(p.getPid()+"");
nodes.add(treeVo);
}
TreeVo<Permission> parent = BuildTree.build(nodes);
ObjectMapper om = new ObjectMapper();
String jsonstr = om.writeValueAsString(parent);
System.out.println(jsonstr);
}
}
三、书籍类别
addbook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>书籍新增</title>
<!-- 全局样式 -->
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/themes/default/easyui.css">
<!-- 定义图标 -->
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/themes/default/easyui.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/jquery.min.js"></script>
<!-- 组件库源码的js文件 -->
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/jquery.easyui.min.js"></script>
<script src="${pageContext.request.contextPath}/static/js/book.js"></script>
</head>
<body>
<input type="hidden" id="ctx" value="${pageContext.request.contextPath}">
<div style="margin:20px 0;"></div>
<div class="easyui-panel" title="已下架书籍" style="width:100%;padding:30px 60px;">
<form id="ff" action="${pageContext.request.contextPath}/book.action?methodName=add" method="post">
<div style="margin-bottom:20px">
<input class="easyui-textbox" name="name" style="width:100%" data-options="label:'书名:',required:true">
</div>
<div style="margin-bottom:20px">
<input id="cid" name="cid" value="" label="类别" >
<%--<select class="easyui-combobox" name="cid" label="类别" style="width:100%">--%>
<%--<option value="1">文艺</option>--%>
<%--<option value="2">小说</option>--%>
<%--<option value="3">青春</option>--%>
<%--</select>--%>
</div>
<div style="margin-bottom:20px">
<input class="easyui-textbox" name="author" style="width:100%" data-options="label:'作者:',required:true">
</div>
<div style="margin-bottom:20px">
<input class="easyui-textbox" name="price" style="width:100%"
data-options="label:'价格:',required:true">
</div>
<div style="margin-bottom:20px">
<input class="easyui-textbox" name="publishing" style="width:100%"
data-options="label:'出版社:',required:true">
</div>
<div style="margin-bottom:20px">
<input class="easyui-textbox" name="description" style="width:100%;height:60px"
data-options="label:'简介:',required:true">
</div>
<%--默认未上架--%>
<input type="hidden" name="state" value="1">
<%--默认起始销量为0--%>
<input type="hidden" name="sales" value="0">
</form>
<div style="text-align:center;padding:5px 0">
<a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()" style="width:80px">Submit</a>
<a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()" style="width:80px">Clear</a>
</div>
</div>
<script>
$(function () {
$('#cid').combobox({
url:'${pageContext.request.contextPath}/category.action?methodName=combobox',
valueField:'id',
textField:'name'
});
});
function submitForm() {
$('#ff').form('submit',{
success:function (param) {
$('#ff').form('clear');
}
});
}
function clearForm() {
$('#ff').form('clear');
}
</script>
</body>
</html>
book.js
$(function(){
var ctx=$("#ctx").val();
$('#cid').combobox({
url:ctx+'category.action?methodName=combobox',
valueField:'id',
textField:'name'
});
})
category 实体类
package com.chendongyang.entity;
public class Category {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + "]";
}
public Category() {}
public Category(long id, String name) {
super();
this.id = id;
this.name = name;
}
}
categorydao 方法
package com.chendongyang.dao;
import java.sql.SQLException;
import java.util.List;
import com.chendongyang.entity.Category;
import com.chendongyang.util.BaseDao;
import com.chendongyang.util.PageBean;
public class CategoryDao extends BaseDao<Category>{
public List<Category> list(Category category,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_category";
return super.executeQuery(sql, Category.class, pageBean);
}
}
categoryAction
package com.chendongyang.web;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.chendongyang.dao.CategoryDao;
import com.chendongyang.entity.Category;
import com.chendongyang.framework.ActionSupport;
import com.chendongyang.framework.ModelDriven;
import com.chendongyang.util.ResponseUtil;
public class CategoryAction extends ActionSupport implements ModelDriven<Category>{
private CategoryDao cyd=new CategoryDao();
private Category category=new Category();
@Override
public Category getModel() {
// TODO Auto-generated method stub
return category;
}
public String combobox(HttpServletRequest req,HttpServletResponse resp) throws Exception {
try {
List<Category> list = this.cyd.list(null, null);
ResponseUtil.writeJson(resp, list);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
mvc.xml配置
<action path="/category" type="com.chendongyang.web.CategoryAction">
</action>