1.list.jsp中初始化数据表格
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/WEB-INF/layout/header.jsp" %>
<!-- Theme style -->
<link rel="stylesheet" href="static/dist/css/adminlte.min.css">
<link rel="stylesheet" href="static/plugins/bootstrap-table/bootstrap-table.min.css">
</head>
<body style="background-color: #e3e3e3;">
<div class="container-fluid mt-2">
<div class="content">
<div class="card">
<section class="card-header">
<h3 class="card-title">权限配置</h3>
<span class="float-right">
<a class="refresh-page text-dark" href="javascript:window.location.reload();" title="刷新当前页面">
<i class="fa fa-sync"></i>
</a>
</span>
</section>
<section class="card-body">
<div id="toolbar" class="input-group">
<button class="btn btn-success add-item mr-2">新增内容</button>
<button class="btn btn-danger del-check">删除选中</button>
</div>
<table id="bootstrapTable"></table>
</section>
</div>
</div>
</div>
<%@include file="/WEB-INF/layout/footer.jsp" %>
<script src="static/plugins/bootstrap-table/bootstrap-table.min.js"></script>
<script src="static/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script>
($=>{
$(()=>{
const table = $("#bootstrapTable");
table.bootstrapTable({
toolbar:"#toolbar",
buttonsClass:"secondary",
search:true,
searchOnEnterKey:true,
pagination:true,
sidePagination: "server",
pageNumber:1,
pageSize:10,
pageList:[10,20,50,100,200],
sortName:"addTime",
sortOrder:"DESC",
showColumns: true,
showRefresh: true,
striped:true,
cache:false,
method:"post",
contentType:"application/json;charset=utf-8",
uniqueId:"id",
url:"master/userRuleData.do",
queryParams(params){
return JSON.stringify($.extend({},params,{
extras:{}
}));
},
columns:[{
checkbox: true
},{
field:"id",
title:"编号",
align:"center",
},{
field:"pid",
title:"父级权限",
align:"center",
},{
field:"ruleName",
title:"权限名称",
align:"center",
},{
field:"ruleUrl",
title:"访问路径",
align:"center",
},{
field:"ruleMethod",
title:"方法",
align:"center",
},{
field:"ruleType",
title:"类型",
align:"center",
},{
field:"ruleIcon",
title:"图标",
align:"center",
},{
field:"ruleDes",
title:"描述信息",
align:"center",
},{
field:"addTime",
title:"添加时间",
align:"center",
},{
field:"updateTime",
title:"修改时间",
align:"center",
visible:false,
},{
field:"delTime",
title:"操作栏",
align:"center",
events:"actionEvents",
formatter(value,row,index){
return [
'<a class="text-info edit"><i class="fa fa-edit"></i></a>',
'<a class="text-danger delete"><i class="fa fa-trash"></i></a>'
].join("");
}
}],
});
$("#toolbar").on("click","button",function (e) {
const target= $(this);
if (target.hasClass("add-item")){
} else if (target.hasClass("del-checked")){
}
})
});
window.actionEvents = {
"click .edit":function (e,value,row,index) {
},
"click .delete":function (e,value,row,index) {
},
};
})(jQuery);
</script>
</body>
</html>
2.db-service中添加三个接口
BaseService UserRuleService UserMasterService
3.新建BootsTableRequest.java
package peanut.bean;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import peanut.utils.PeanutUtils;
import java.util.Map;
import java.util.regex.Pattern;
@Getter
@Setter
@ToString
public class BootsTableRequest {
private Integer limit;
private Integer offset;
private String order;
private String search;
private String sort;
private Map<String,Object> extras;
public void setLimit(Integer limit){
this.limit = limit == null ? 1:limit > 1 ?limit :1;
}
public void setOffset(Integer offset) {
this.offset = offset == null ? 0 : offset > 0 ? offset : 0;
}
public void setOrder(String order) {
this. order ="ASC".equalsIgnoreCase(order) ? "ASC" : "DESC";
}
public void setSort(String sort) {
this.sort = sort;
if (Pattern.matches("[0-9a-zA-Z_]+", sort)){
this.sort =PeanutUtils.humpToLine(sort);
}else{
this.sort = null;
}
}
public void setSearch(String search) {
if (Pattern.matches("[0-9a-zA-Z_\u4300-\u9fa5]+", search)){
this.search = "%"+ search + "%";
}else{
this.search = null;
}
}
}
4…新建BootsTableResponse.java
package peanut.bean;
import com.github.pagehelper.PageInfo;
import db.model.BaseModel;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.List;
@Getter
@Setter
@ToString
public class BootsTableResponse<T extends BaseModel> {
private long total;
private List<T> rows;
public BootsTableResponse(List<T> rows) {
PageInfo<T> info = new PageInfo<>(rows);
this.total = info.getTotal();
this.rows = rows;
}
}
5.PeanutUtils.java中添加驼峰命名转化为下划线
private static final Pattern humpPattern = Pattern.compile("[A-Z]");
public static String humpToLine(String str){
if (StringUtils.isEmpty(str)) return null;
Matcher matcher = humpPattern.matcher(str);
StringBuffer buffer = new StringBuffer();
while (matcher.find()){
matcher.appendReplacement(buffer,"_"+matcher.group(0).toLowerCase());
}
matcher.appendTail(buffer);
return buffer.toString();
}
6.UserMasterServiceImpl.java中导包
package edu.td.peanut.service;
import db.mapper.UserMasterMapper;
import db.model.UserMaster;
import db.service.UserMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import peanut.bean.BootsTableRequest;
import peanut.bean.BootsTableResponse;
import static org.mybatis.dynamic.sql.SqlBuilder.*;
import static db.mapper.UserMasterDynamicSqlSupport.*;
@Service
@Primary
public class UserMasterServiceImpl implements UserMasterService {
@Autowired
private UserMasterMapper masterMapper;
@Override
public UserMaster findByPrimaryKey(Integer key){
if (key == null ||key < 1) return null;
return masterMapper.selectByPrimaryKey(key).orElse(null);
}
@Override
public BootsTableResponse<UserMaster> bootsData(BootsTableRequest tableRequest) {
return null;
}
@Override
public UserMaster findMasterByUsername(String userName){
if (StringUtils.isEmpty(userName))return null;
return masterMapper.selectOne(entity->entity
.where(delTime,isEqualTo(0))
.and(masterUsername,isEqualTo(userName))
).orElse(null);
}
}
7.UserRuleServiceImpl中实现分页查询
package edu.td.peanut.service;
import com.github.pagehelper.PageHelper;
import db.mapper.UserRuleMapper;
import db.model.UserRule;
import db.service.UserRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import peanut.bean.BootsTableRequest;
import peanut.bean.BootsTableResponse;
import static org.mybatis.dynamic.sql.SqlBuilder.*;
import static db.mapper.UserRuleDynamicSqlSupport.*;
@Service
@Primary
public class UserRuleServiceImpl implements UserRuleService {
@Autowired
private UserRuleMapper ruleMapper;
@Override
public UserRule findByPrimaryKey(Integer key) {
if (key == null || key < 1) return null;
return ruleMapper.selectByPrimaryKey(key).orElse(null);
}
@Override
public BootsTableResponse<UserRule> bootsData(BootsTableRequest tableRequest) {
if (tableRequest==null)return null;
PageHelper.offsetPage(tableRequest.getOffset(),tableRequest.getLimit());
return new BootsTableResponse<>(ruleMapper.select(entity->{
entity.where(delTime,isEqualTo(0))
.and(
ruleName,isLikeWhenPresent(tableRequest.getSearch()),
or(ruleMethod,isLikeWhenPresent(tableRequest.getSearch())),
or(ruleType,isLikeWhenPresent(tableRequest.getSearch())),
or(ruleUrl,isLikeWhenPresent(tableRequest.getSearch()))
);
if (tableRequest.getSort()==null) {
entity.orderBy(addTime.descending());
}else if ("ASC".equals(tableRequest.getOrder())) {
entity.orderBy(userRule.column(tableRequest.getSort()));
}else {
entity.orderBy(userRule.column(tableRequest.getSort()).descending());
}
return entity;
}));
}
}
8.添加debug
package edu.td.peanut.interceptor;
import db.model.UserMaster;
import db.service.UserMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class MasterLoginInterceptor extends HandlerInterceptorAdapter {
@Value("${system.debug}")
private boolean debug;
@Autowired
private UserMasterService masterService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
if (session.getAttribute("loginUser") == null){
if (debug){
UserMaster master = masterService.findByPrimaryKey(1);
session.setAttribute("loginUser",master);
return true;
}
response.sendRedirect("/master/login.do");
return false;
}
return true;
}
}
9.打印返回信息
project.properties中添加系统配置
新建log4j.properties
log4j.rootLogger=TRACE, stdout
log4j.logger.db.mapper=DEBUG
#定义打印的样式
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.ConversionPattern=%d{HH:mm:ss} %-5p %c{1}:%L -%m%n
#关闭插件内的log4j日志信息
log4j.logger.org=OFF
log4j.logger.com=OFF
10.添加org.apache.log4j.Logger
package edu.td.peanut.controller;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import peanut.utils.PeanutUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public abstract class PublicController {
protected Logger LOG = Logger.getLogger(this.getClass());
protected HttpServletRequest request;
protected HttpServletResponse response;
protected HttpSession session;
protected ModelMap modelMap;
@ModelAttribute
public void _init(HttpServletRequest request,HttpServletResponse response,HttpSession session,ModelMap modelMap){
this.request=request;
this.response=response;
this.session=session;
this.modelMap=modelMap;
}
protected void createToken(){
String sessionToken = PeanutUtils.randomStr(12);
session.setAttribute("sessionToken",sessionToken);
modelMap.addAttribute("token",sessionToken);
}
protected void createCode(){
String codeKey = PeanutUtils.randomStr(6,"0123456789");
session.setAttribute("codeKey",codeKey);
modelMap.addAttribute("code",codeKey);
}
protected boolean checkToken(String token){
if (StringUtils.isEmpty(token)) return false;
String sessionToken = (String) session.getAttribute("sessionToken");
session.removeAttribute("sessionToken");
return token.equals(sessionToken);
}
protected boolean checkCode(String code){
if (StringUtils.isEmpty(code)) return false;
String codeKey = (String) session.getAttribute("codeKey");
return code.equalsIgnoreCase(codeKey);
}
}
|