锋哥原创的Springboot+Layui python222网站实战:
后端:
python
package com.python222.controller.admin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.python222.entity.Link;
import com.python222.entity.PageBean;
import com.python222.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* 管理员-友情链接控制器
* @author Administrator
*
*/
@RestController
@RequestMapping(value = "/admin/link")
public class LinkAdminController {
@Autowired
private LinkService linkService;
/**
* 根据条件分页查询友情链接
* @param page
* @param limit
* @return
* @throws Exception
*/
@RequestMapping(value = "/list")
public Map<String,Object> list(@RequestParam(value="page",required=false)Integer page,@RequestParam(value="limit",required=false)Integer limit)throws Exception{
Map<String, Object> resultMap = new HashMap<>();
PageBean pageBean=new PageBean(page,limit);
Page<Link> linkPage = linkService.page(new Page<>(pageBean.getPage(), pageBean.getPageSize()));
resultMap.put("code", 0);
resultMap.put("count", linkPage.getTotal());
resultMap.put("data", linkPage.getRecords());
return resultMap;
}
/**
* 添加或者修改友情链接
* @param link
* @return
*/
@RequestMapping("/save")
public Map<String,Object> save(Link link){
if(link.getId()==null){
linkService.save(link);
}else{
linkService.updateById(link);
}
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("success", true);
return resultMap;
}
/**
* 删除友情链接
* @param id
* @return
* @throws Exception
*/
@RequestMapping("/delete")
public Map<String,Object> delete(Integer id)throws Exception{
Map<String, Object> resultMap = new HashMap<>();
linkService.removeById(id);
resultMap.put("success", true);
return resultMap;
}
/**
* 根据id查询友情链接实体
* @param id
* @return
* @throws Exception
*/
@RequestMapping("/findById")
public Map<String,Object> findById(Integer id)throws Exception{
Map<String, Object> resultMap = new HashMap<>();
Link link=linkService.getById(id);
resultMap.put("link", link);
resultMap.put("success", true);
return resultMap;
}
}
前端linkManage.html
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>友情链接管理</title>
<link rel="stylesheet" href="/static/layui/css/layui.css"></link>
<link rel="stylesheet" href="/static/css/css.css"></link>
</head>
<body>
<div style="padding: 20px">
<span class="layui-breadcrumb">
<a>首页</a>
<a><cite>友情链接管理</cite></a>
</span>
<div style="padding-top: 20px;">
<div>
<div>
<button class="layui-btn layuiadmin-btn-list" data-type="batchdel" onclick="addLink()">添加</button>
</div>
</div>
<div>
<table width="100%" id="linkListTable" ></table>
</div>
</div>
</div>
<script src="/static/layui/layui.js"></script>
<script src="/static/js/jquery.js"></script>
<script src="/static/js/common.js"></script>
<script type="text/javascript">
layui.use(['element','form','table'], function(){
var form=layui.form;
var element = layui.element; //导航的hover效果、二级菜单等功能,需要依赖element模块
$ = layui.jquery; // 使用jquery
table = layui.table;
table.render({
elem: '#linkListTable'
,url:'/admin/link/list'
,cols: [[
{type:'checkbox'}
,{field:'id', width:100,title: '编号'}
,{field:'name', width:200,title: '友情链接名称'}
,{field:'url', width:250,title: '友情链接地址'}
,{field:'sort', width:100, title: '排列序号',align:'center'}
,{field:'remark', title: '描述',align:'center'}
,{field:'action', width:150, title: '操作',align:'center',templet:formatAction}
]]
,page: true
});
});
function deleteOne(id){
layer.confirm('您确定要删除这条记录吗?', {
title:"系统提示"
,btn: ['确定','取消'] //按钮
}, function(){
layer.closeAll('dialog');
$.post("/admin/link/delete",{"id":id},function(result){
if(result.success){
layer.msg("删除成功!");
table.reload("linkListTable",{});
}else{
layer.msg("删除失败,请联系管理员!");
}
},"json");
}, function(){
});
}
function addLink(){
layer.open({
type: 2,
title: '添加友情链接',
area: ['500px', '500px'],
content: '/admin/saveLink.html' //iframe的url
});
}
function modifyLink(id){
layer.open({
type: 2,
title: '修改友情链接',
area: ['500px', '500px'],
content: '/admin/saveLink.html?id='+id //iframe的url
});
}
function formatAction(d){
return "<button class='layui-btn layui-btn-normal layui-btn-xs' onclick='modifyLink("+d.id+")'><i class='layui-icon layui-icon-edit'></i>编辑</button><button class='layui-btn layui-btn-warm layui-btn-xs' onclick='deleteOne("+d.id+")'><i class='layui-icon layui-icon-delete' ></i>删除</button>";
}
</script>
</body>
</html>
saveLink.html
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加或者修改友情链接</title>
<link rel="stylesheet" href="/static/layui/css/layui.css"></link>
<style type="text/css">
table tr td{
padding: 10px;
}
</style>
</head>
<body>
<div style="padding: 20px">
<form method="post">
<table>
<tr>
<td>友情链接名称:</td>
<td><input type="text" id="name" name="name" class="layui-input" style="width: 300px"/></td>
</tr>
<tr>
<td>友情链接地址:</td>
<td><input type="text" id="url" name="url" class="layui-input" style="width: 300px"/></td>
</tr>
<tr>
<td>友情链接描述:</td>
<td>
<textarea type="text" id="remark" name="remark" class="layui-input" style="width: 300px;height: 100px" ></textarea>
</td>
</tr>
<tr>
<td>排列序号:</td>
<td><input type="text" id="sort" name="sort" class="layui-input" style="width: 100px;display: inline;"/> <span>(根据数值从小到大排序)</span></td>
</tr>
<tr>
<td><button class="layui-btn" onclick="submitData();return false;">提交</button></td>
<td><font id="errorInfo" color="red"></font></td>
</tr>
</table>
</form>
</div>
<script src="/static/layui/layui.js"></script>
<script src="/static/js/jquery.js"></script>
<script src="/static/js/common.js"></script>
<script type="text/javascript">
layui.use(['form'], function(){
});
function submitData(){
var name=$("#name").val().trim();
var url=$("#url").val().trim();
var remark=$("#remark").val().trim();
var sort=$("#sort").val().trim();
if(name=="") {
$("#errorInfo").text("请输入友情链接名称!");
$("#name").focus();
return false;
}
if(url=="") {
$("#errorInfo").text("请输入友情链接地址!");
$("#url").focus();
return false;
}
if(!IsURL(url)){
$("#errorInfo").text("友情链接地址格式不正确!");
$("#url").focus();
return false;
}
if(sort=="") {
$("#errorInfo").text("请输入排列序号!");
$("#sort").focus();
return false;
}
if (!(/(^[1-9]\d*$)/.test(sort))) {
$("#errorInfo").text("排列序号必须是正整数!");
$("#sort").focus();
return false;
}
var id=getQueryVariable("id");
if(id){
$.post("/admin/link/save",{id:id,name:name,url:url,sort:sort,remark:remark},function(result){
if(result.success){
layer.alert('修改成功!',function () {
parent.reloadPage();
});
}else{
$("#errorInfo").text(result.errorInfo);
}
},"json");
}else{
$.post("/admin/link/save",{name:name,url:url,sort:sort,remark:remark},function(result){
if(result.success){
layer.alert('添加成功!',function () {
parent.reloadPage();
});
}else{
$("#errorInfo").text(result.errorInfo);
}
},"json");
}
}
function getQueryVariable(variable){
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
$(function(){
var id=getQueryVariable("id");
if(id){
$.post("/admin/link/findById",{id:id},function(result){
if(result.success){
var link=result.link;
$("#name").val(link.name);
$("#url").val(link.url);
$("#sort").val(link.sort);
$("#remark").val(link.remark);
}else{
layer.alert('服务器加载有问题,请联系管理员!');
}
},"json");
}
});
</script>
</body>
</html>