页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>部门管理</title>
<link rel="stylesheet" type="text/css" href="ui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="ui/themes/icon.css">
<script type="text/javascript" src="ui/jquery.min.js"></script>
<script type="text/javascript" src="ui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="ui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="ui/jquery.serializejson.min.js"></script>
<script type="text/javascript">
var method = "";
$(function(){
$('#grid').datagrid({
url:'dep_list',
columns:[[
{field:'id',title:'部门编号',width:100},
{field:'name',title:'部门名称',width:100},
{field:'tele',title:'部门联系电话',width:100,align:'left'},
{field:'-',title:'操作',formatter: function(value,row,index){
var oper = "<a href=\"javascript:void(0)\" onclick=\"edit(" + row.id + ')">修改</a>';
oper += ' <a href="javascript:void(0)" onclick="del(' + row.id + ')">删除</a>';
return oper;
}}
]],
singleSelect: true,
pagination: true,
toolbar: [{
text: '新增',
iconCls: 'icon-add',
handler: function(){
method = "add";
$('#editDlg').dialog('open');
}
}]
});
$('#btnSearch').bind('click',function(){
var formData = $('#searchForm').serializeJSON();
$.ajax({
url: 'dep_listBySerch',
data: formData,
dataType: 'json',
type: 'post',
success:function(rtn){
$('#grid').datagrid('loadData',rtn);
}
});
});
$('#editDlg').dialog({
title: '部门编辑',
width: 300,
height: 200,
closed: true,
modal: true
});
$('#btnSave').bind('click',function(){
var formData = $('#editForm').serializeJSON();
$.ajax({
url: 'dep_' + method,
data: formData,
dataType: 'json',
type: 'post',
success:function(rtn){
if(rtn){
$.messager.alert('提示','操作成功','info');
}else{
$.messager.alert('提示','操作失败','error');
}
$('#editDlg').dialog('close');
$('#grid').datagrid('reload');
}
});
});
});
function del(id){
$.messager.confirm("确认","确认要删除吗?",function(yes){
if(yes){
$.ajax({
url: 'dep_delete?id=' + id,
dataType: 'json',
type: 'post',
success:function(rtn){
if(rtn){
$.messager.alert("提示","删除成功",'info',function(){
$('#grid').datagrid('reload');
});
}else{
$.messager.alert("提示","删除失败",'error',function(){
$('#grid').datagrid('reload');
});
}
}
});
}
});
}
function edit(id){
$('#editDlg').dialog('open');
$('#editForm').form('clear');
method = "update";
$('#editForm').form('load','dep_getById?id=' + id);
}
</script>
</head>
<body>
<div class="easyui-panel" style="padding-left:4px;border-bottom:0px;">
<div style="height:2px;"></div>
<form id="searchForm">
<table>
<tr>
<td>部门名称</td>
<td><input name="dep.name"></td>
</tr>
<tr>
<td>部门电话</td>
<td><input name="dep.tele"></td>
</tr>
</table>
<button id="btnSearch" type="button">查询</button>
</form>
<div style="height:2px;"></div>
</div>
<table id="grid"></table>
<div id="editDlg">
<form id="editForm">
<table>
<tr>
<td>部门名称</td>
<td><input name="dep.name"><input type="hidden" name="dep.id" /></td>
</tr>
<tr>
<td>部门电话</td>
<td><input name="dep.tele"></td>
</tr>
</table>
<button id="btnSave" type="button">保存</button>
</form>
</div>
</body>
</html>
action
package action;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import entity.Dep;
import service.IDepBiz;
public class DepAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private IDepBiz depBiz;
private Dep dep;
private boolean flag;
private String id;
private int page;
private int rows;
public void list(){
int firstData = (page - 1) * rows;
List<Dep> dlist = depBiz.getList(firstData,rows);
int num = depBiz.getCount();
Map map = new HashMap<String,Object>();
map.put("total", num);
map.put("rows", dlist);
String jsonString = JSON.toJSONString(map);
HttpServletResponse response = ServletActionContext.getResponse();
try {
response.setContentType("text/html;charset=utf-8");
response.getWriter().print(jsonString);
} catch (Exception e) {
e.printStackTrace();
}
}
public void listBySerch(){
int firstData = (page - 1) * rows;
List<Dep> dlist = depBiz.getList(dep,firstData,rows);
int num = depBiz.getCount();
Map map = new HashMap<String,Object>();
map.put("total", num);
map.put("rows", dlist);
String jsonString = JSON.toJSONString(map);
HttpServletResponse response = ServletActionContext.getResponse();
try {
response.setContentType("text/html;charset=utf-8");
response.getWriter().print(jsonString);
} catch (Exception e) {
e.printStackTrace();
}
}
public void update(){
flag = depBiz.update(dep);
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
response.getWriter().print(flag);
} catch (IOException e) {
e.printStackTrace();
}
}
public void getById(){
Dep depby = depBiz.getById(id);
String jsonString = JSON.toJSONString(depby);
String fixString = fixString(jsonString,"dep");
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
response.getWriter().print(fixString);
} catch (IOException e) {
e.printStackTrace();
}
}
public String fixString(String jsonMap ,String prefix ){
Map <String ,Object> map = JSON.parseObject(jsonMap);
Map<String,Object> mapp = new HashMap<>();
for (String key : map.keySet()) {
mapp.put(prefix+"."+key, map.get(key));
}
return JSON.toJSONString(mapp);
}
public void add(){
flag = depBiz.add(dep);
HttpServletResponse response = ServletActionContext.getResponse();
try {
response.setContentType("text/html;charset=utf-8");
response.getWriter().print(flag);
} catch (Exception e) {
e.printStackTrace();
}
}
public void delete(){
flag = depBiz.delete(id);
HttpServletResponse response = ServletActionContext.getResponse();
try {
response.setContentType("text/html;charset=utf-8");
response.getWriter().print(flag);
} catch (Exception e) {
e.printStackTrace();
}
}
public IDepBiz getDepBiz() {
return depBiz;
}
public void setDepBiz(IDepBiz depBiz) {
this.depBiz = depBiz;
}
public Dep getDep() {
return dep;
}
public void setDep(Dep dep) {
this.dep = dep;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="dep_*" class="action.DepAction" method="{1}"></action>
</package>
</struts>