StaffAction:
package com.xushuai.bos.web.action;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.xushuai.bos.entity.Staff;
import com.xushuai.bos.service.StaffService;
import com.xushuai.bos.utils.BOSUtils;
import com.xushuai.bos.utils.PageBean;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
@Controller("staffAction")
@Scope("prototype")
public class StaffAction extends BaseAction<Staff> {
@Autowired
@Qualifier("staffService")
private StaffService staffService;
public void setStaffService(StaffService staffService) {
this.staffService = staffService;
}
//接收分页参数
private int page;
private int rows;
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;
}
//接收要删除的取派员的ID
private String ids;
public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
/**
* 新增取派员
* @return
*/
public String add(){
staffService.save(model);
return LIST;
}
/**
* 分页查询取派员
* @return
* @throws IOException
*/
public String pageQuery() throws IOException{
//封装离线查询条件
DetachedCriteria criteria = DetachedCriteria.forClass(Staff.class);
//创建分页对象
PageBean pageBean = new PageBean();
pageBean.setCriteria(criteria);
pageBean.setCurrentPage(page);
pageBean.setPageSize(rows);
//调用service#pageQuery(PageBean)
staffService.pageQuery(pageBean);
HttpServletResponse response = BOSUtils.getResponse();
response.setContentType("text/json;charset=UTF-8");
//将PageBean转换为JSON串,返回给页面
JsonConfig jsonConfig = new JsonConfig();
//去除不需要返回的值
jsonConfig.setExcludes(new String[]{"currentPage","pageSize","criteria"});
//将查询结果转换为json串
String json = JSONObject.fromObject(pageBean, jsonConfig).toString();
//返回json数据
response.getWriter().print(json);
return NONE;
}
/**
* 删除取派员(假删除)
* @return
*/
public String delete(){
String[] strings = ids.split(",");
staffService.delete(strings);
return LIST;
}
/**
* 修改取派员
* @return
*/
public String edit(){
staffService.edit(model);
return LIST;
}
}
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="basicstruts2" extends="struts-default">
<!-- 自定义拦截器 -->
<interceptors>
<!-- 声明拦截器 -->
<interceptor name="loginInterceptor" class="com.xushuai.bos.web.interceptor.LoginInterceptor">
<!-- 不拦截login -->
<param name="excludeMethods">login</param>
</interceptor>
<!-- 自定义拦截器栈 -->
<interceptor-stack name="mystack">
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 引用自定义拦截器栈 -->
<default-interceptor-ref name="mystack"></default-interceptor-ref>
<!-- 声明全局结果集 -->
<global-results>
<result name="login">/login.jsp</result>
</global-results>
<!-- 需要进行权限控制的页面访问 -->
<action name="page_*_*">
<result type="dispatcher">/WEB-INF/pages/{1}/{2}.jsp</result>
</action>
<!-- 数据字典 -->
<action name="BasedictAction" class="basedictAction"></action>
<!-- 用户模块 -->
<action name="UserAction_*" class="userAction" method="{1}">
<result name="login">/login.jsp</result>
<result name="home">/index.jsp</result>
</action>
<!-- 取派员模块 -->
<action name="StaffAction_*" class="staffAction" method="{1}">
<result name="list">/WEB-INF/pages/base/staff.jsp</result>
</action>
<!-- 区域模块 -->
<action name="RegionAction_*" class="regionAction" method="{1}">
<result name="list">/WEB-INF/pages/base/region.jsp</result>
</action>
</package>
</struts>
StaffService:
package com.xushuai.bos.service;
import com.xushuai.bos.entity.Staff;
import com.xushuai.bos.utils.PageBean;
public interface StaffService {
/**
* 保存新增取派员
* @param model
*/
void save(Staff model);
/**
* 分页查询取派员
* @param pageBean
*/
void pageQuery(PageBean pageBean);
/**
* 删除指定取派员
* @param strings
*/
void delete(String[] ids);
/**
* 修改取派员
* @param model
*/
void edit(Staff model);
}
StaffServiceImpl:
package com.xushuai.bos.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xushuai.bos.dao.StaffDao;
import com.xushuai.bos.entity.Staff;
import com.xushuai.bos.service.StaffService;
import com.xushuai.bos.utils.PageBean;
@Service("staffService")
@Transactional
public class StaffServiceImpl implements StaffService {
@Autowired
@Qualifier("staffDao")
private StaffDao staffDao;
public void setStaffDao(StaffDao staffDao) {
this.staffDao = staffDao;
}
@Override
public void save(Staff staff) {
staffDao.save(staff);
}
@Override
public void pageQuery(PageBean pageBean) {
staffDao.findByPage(pageBean);
}
@Override
public void delete(String[] ids) {
for (String id : ids) {
staffDao.executeUpdate("staff.delete", id);
}
}
@Override
public void edit(Staff staff) {
Staff _staff = staffDao.findById(staff.getId());
//更新页面传递过来的相关字段,切记,不要直接把staff扔到方法参数上。会造成数据的错误
_staff.setName(staff.getName());
_staff.setTelephone(staff.getTelephone());
_staff.setHaspda(staff.getHaspda());
_staff.setStation(staff.getStation());
_staff.setStandard(staff.getStandard());
staffDao.update(_staff);
}
}
StaffDao、StaffDaoImpl:
package com.xushuai.bos.dao;
import com.xushuai.bos.entity.Staff;
public interface StaffDao extends BaseDao<Staff> {
}
package com.xushuai.bos.dao.impl;
import org.springframework.stereotype.Repository;
import com.xushuai.bos.dao.StaffDao;
import com.xushuai.bos.entity.Staff;
@Repository("staffDao")
public class StaffDaoImpl extends BaseDaoImpl<Staff> implements StaffDao {
}
在Staff.hbm.xml中的hibernate-mapping下配置:
<query name="staff.delete">
UPDATE Staff SET deltag = '1' WHERE id = ?
</query>
页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<!-- 导入jquery核心类库 -->
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
<!-- 导入easyui类库 -->
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/js/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/js/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/js/easyui/ext/portal.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/css/default.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/easyui/ext/jquery.portal.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/easyui/ext/jquery.cookie.js"></script>
<script
src="${pageContext.request.contextPath }/js/easyui/locale/easyui-lang-zh_CN.js"
type="text/javascript"></script>
<script type="text/javascript">
function doAdd(){
//alert("增加...");
$('#addStaffWindow').window("open");
}
function doView(){
$('#selStaffWindow').window("open");
}
function doDelete(){
//获取选中的所有行
var rows = $("#grid").datagrid('getSelections');
//校验是否有行被选中
if(rows.length == 0){//如果未选中任何行
$.messager.alert('提示信息','请选择您要删除的取派员','warning');
}else{
$.messager.confirm('提示信息','确定要执行删除操作吗?',function(r){
if(r){
//定义一个数组,存放ID值
Array :arr = new Array();
for(var i = 0; i < rows.length; i++){
arr.push(rows[i].id);
}
//将数组格式化成一个字符串,用逗号分隔每个ID值
var ids = arr.join(",");
location.href = "StaffAction_delete.action?ids=" + ids;
}
});
}
}
function doRestore(){
$("#grid").datagrid('clearSelections');
}
//工具栏
var toolbar = [ {
id : 'button-view',
text : '查询',
iconCls : 'icon-search',
handler : doView
}, {
id : 'button-add',
text : '增加',
iconCls : 'icon-add',
handler : doAdd
}, {
id : 'button-delete',
text : '删除',
iconCls : 'icon-cancel',
handler : doDelete
},{
id : 'button-save',
text : '取消选中',
handler : doRestore
}];
// 定义列
var columns = [ [ {
field : 'id',
checkbox : true,
},{
field : 'name',
title : '姓名',
width : 120,
align : 'center'
}, {
field : 'telephone',
title : '手机号',
width : 120,
align : 'center'
}, {
field : 'haspda',
title : '是否有PDA',
width : 120,
align : 'center',
formatter : function(data,row, index){
if(data=="1"){
return "有";
}else{
return "无";
}
}
}, {
field : 'deltag',
title : '状态',
width : 120,
align : 'center',
formatter : function(data,row, index){
if(data=="0"){
return "正常使用"
}else{
return "已删除";
}
}
}, {
field : 'standard',
title : '取派标准',
width : 120,
align : 'center'
}, {
field : 'station',
title : '所在单位',
width : 200,
align : 'center'
} ] ];
$(function(){
// 先将body隐藏,再显示,不会出现页面刷新效果
$("body").css({visibility:"visible"});
// 取派员信息表格
$('#grid').datagrid( {
iconCls : 'icon-forward',
fit : true,
border : false,
rownumbers : true,
striped : true,
pageList: [15,20,28],
pageSize:28,
pagination : true,
toolbar : toolbar,
url : "StaffAction_pageQuery.action",
idField : 'id',
columns : columns,
onDblClickRow : doDblClickRow
});
// 添加取派员窗口
$('#addStaffWindow').window({
title: '添加取派员',
width: 400,
modal: true,
shadow: true,
closed: true,
height: 400,
resizable:false
});
// 修改取派员窗口
$('#editStaffWindow').window({
title: '编辑取派员',
width: 400,
modal: true,
shadow: true,
closed: true,
height: 400,
resizable:false
});
// 查询取派员窗口
$('#selStaffWindow').window({
title: '查询取派员',
width: 400,
modal: true,
shadow: true,
closed: true,
height: 400,
resizable:false
});
//新增窗口中,保存按钮
$("#save").click(function(){
var formFlag = $("#addStaffWindow").form('validate');
//alert(formFlag);
//判断表单数据检验情况
if(formFlag){//校验通过,提交表单
$("#addStaffForm").submit();
}
});
//修改窗口中,修改按钮
$("#edit").click(function(){
var formFlag = $("#editStaffWindow").form('validate');
//alert(formFlag);
//判断表单数据检验情况
if(formFlag){//校验通过,提交表单
$("#editStaffForm").submit();
}
});
//查询窗口中,查询按钮
$("#sel").click(function(){
//查询
alert("查询");
$("#selStaffWindow").window("close");
});
});
//双击修改取派员
function doDblClickRow(rowIndex, rowData){
$('#editStaffWindow').window("open");
//回显取派员信息
$('#editStaffWindow').form("load",rowData);
}
</script>
</head>
<body class="easyui-layout" style="visibility:hidden;">
<div region="center" border="false">
<table id="grid"></table>
</div>
<div class="easyui-window" title="对收派员进行添加或者修改" id="addStaffWindow" collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
<div region="north" style="height:31px;overflow:hidden;" split="false" border="false" >
<div class="datagrid-toolbar">
<a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true" >保存</a>
</div>
</div>
<div region="center" style="overflow:auto;padding:5px;" border="false">
<form id="addStaffForm" action="StaffAction_add.action" method="post">
<table class="table-edit" width="80%" align="center">
<tr class="title">
<td colspan="2">收派员信息</td>
</tr>
<!-- TODO 这里完善收派员添加 table -->
<!-- <tr>
<td>取派员编号</td>
<td><input type="text" name="id" class="easyui-validatebox" required="true"/></td>
</tr> -->
<tr>
<td>姓名</td>
<td><input type="text" name="name" class="easyui-validatebox" required="true"/></td>
</tr>
<tr>
<td>手机</td>
<td>
<!-- 添加validatebox校验规则 -->
<script type="text/javascript">
var reg = /^1[3|4|5|7|8][0-9]{9}$/;
//扩展手机号校验规则
$.extend($.fn.validatebox.defaults.rules, {
telephone: {
validator: function(value,param){
return reg.test(value);
},
message: '手机号格式错误!'
}
});
</script>
<input type="text" name="telephone" class="easyui-validatebox" data-options="validType:'telephone'" required="true"/>
</td>
</tr>
<tr>
<td>单位</td>
<td><input type="text" name="station" class="easyui-validatebox" required="true"/></td>
</tr>
<tr>
<td colspan="2">
<input type="checkbox" name="haspda" value="1" />
是否有PDA</td>
</tr>
<tr>
<td>取派标准</td>
<td>
<input type="text" name="standard" class="easyui-validatebox" required="true"/>
</td>
</tr>
</table>
</form>
</div>
</div>
<!-- 编辑取派员信息 -->
<div class="easyui-window" title="对收派员进行添加或者修改" id="editStaffWindow"
collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
<div region="north" style="height:31px;overflow:hidden;" split="false" border="false" >
<div class="datagrid-toolbar">
<a id="edit" icon="icon-edit" href="#" class="easyui-linkbutton" plain="true" >修改</a>
</div>
</div>
<div region="center" style="overflow:auto;padding:5px;" border="false">
<form id="editStaffForm" action="StaffAction_edit.action" method="post">
<input type="hidden" name="id"></input>
<table class="table-edit" width="80%" align="center">
<tr class="title">
<td colspan="2">收派员信息</td>
</tr>
<!-- TODO 这里完善收派员添加 table -->
<!-- <tr>
<td>取派员编号</td>
<td><input type="text" name="id" class="easyui-validatebox" required="true"/></td>
</tr> -->
<tr>
<td>姓名</td>
<td><input type="text" name="name" class="easyui-validatebox" required="true"/></td>
</tr>
<tr>
<td>手机</td>
<td>
<!-- 添加validatebox校验规则 -->
<script type="text/javascript">
var reg = /^1[3|4|5|7|8][0-9]{9}$/;
//扩展手机号校验规则
$.extend($.fn.validatebox.defaults.rules, {
telephone: {
validator: function(value,param){
return reg.test(value);
},
message: '手机号格式错误!'
}
});
</script>
<input type="text" name="telephone" class="easyui-validatebox" data-options="validType:'telephone'" required="true"/>
</td>
</tr>
<tr>
<td>单位</td>
<td><input type="text" name="station" class="easyui-validatebox" required="true"/></td>
</tr>
<tr>
<td colspan="2">
<input type="checkbox" name="haspda" value="1" />
是否有PDA</td>
</tr>
<tr>
<td>取派标准</td>
<td>
<input type="text" name="standard" class="easyui-validatebox" required="true"/>
</td>
</tr>
</table>
</form>
</div>
</div>
<!-- 查询取派员 -->
<div class="easyui-window" title="对收派员进行相关操作" id="selStaffWindow"
collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
<div region="north" style="height:31px;overflow:hidden;" split="false" border="false" >
<div class="datagrid-toolbar">
<a id="sel" icon="icon-search" href="#" class="easyui-linkbutton" plain="true" >查询</a>
</div>
</div>
<div region="center" style="overflow:auto;padding:5px;" border="false">
<form id="selStaffForm" action="StaffAction_pageQuery.action" method="post">
<table class="table-sel" width="80%" align="center">
<tr class="title">
<td colspan="2" > 查询条件</td>
</tr>
<!-- TODO 这里完善收派员添加 table -->
<tr>
<td>取派员编号</td>
<td><input type="text" name="id" class="easyui-validatebox"/></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name" class="easyui-validatebox"/></td>
</tr>
<tr>
<td>手机</td>
<td>
<input type="text" name="telephone" class="easyui-validatebox" />
</td>
</tr>
<tr>
<td>单位</td>
<td><input type="text" name="station" class="easyui-validatebox"/></td>
</tr>
<tr>
<td>取派标准</td>
<td>
<input type="text" name="standard" class="easyui-validatebox"/>
</td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
遗留:取派员查询
取派员查询(新增代码)
StaffAction(只需修改pageQuery方法):修改后的pageQuery代码如下
/**
* 分页查询取派员
* @return
* @throws IOException
*/
public String pageQuery() throws IOException{
//获取离线查询条件
DetachedCriteria dc = pageBean.getCriteria();
//健壮性判断
String name = model.getName();
String standard = model.getStandard();
String station = model.getStation();
String telephone = model.getTelephone();
//只要输入的条件不为空,将值赋给离线查询对象
if(StringUtils.isNotBlank(name)){
dc.add(Restrictions.like("name", "%"+name+"%"));
}
if(StringUtils.isNotBlank(standard)){
dc.add(Restrictions.like("standard", "%"+standard+"%"));
}
if(StringUtils.isNotBlank(station)){
dc.add(Restrictions.eq("station", station));
}
if(StringUtils.isNotBlank(telephone)){
dc.add(Restrictions.like("telephone", "%"+telephone+"%"));
}
//调用service#pageQuery(PageBean)
staffService.pageQuery(pageBean);
//将pageBean转换为json字符串回写到页面
BOSUtils.writerJson(pageBean, new String[]{"currentPage","pageSize","criteria"});
return NONE;
}
页面修改(为sel的按钮绑定单击事件):
//查询窗口中,查询按钮
$("#sel").click(function(){
//将form中的表单数据转换为json,用于提交
var params = $("#selStaffForm").serializeJson();
//提交参数,加载数据,关闭查询窗口
$("#grid").datagrid('load',params);
$("#selStaffWindow").window("close");
});