实现NETCTOSS项目中资费列表功能
- 在数据访问层增加查询全部资费的方法
- 创建资费表,并插入预置数据:
DROP TABLE IF EXISTS `cost`;
create table cost(
cost_id int(4) auto_increment,
name varchar(50) not null,
base_duration int(11),
base_cost double(7,2),
unit_cost double(7,4),
status char(1),
descr varchar(100),
creatime datetime default now(),
startime datetime,
cost_type char(1),
constraint cost_id_pk primary key(cost_id)
);
insert into cost values (null,'5.9元套餐',20,5.9,0.4,0,'5.9元20小时/月,超出部分0.4元/时',default,default,null);
insert into cost values (null,'6.9元套餐',40,6.9,0.3,0,'6.9元40小时/月,超出部分0.3元/时',default,default,null);
insert into cost values (null,'8.5元套餐',100,8.5,0.2,0,'8.5元100小时/月,超出部分0.2元/时',default,default,null);
insert into cost values (null,'10.5元套餐',200,10.5,0.1,0,'10.5元200小时/月,超出部分0.1元/时',default,default,null);
insert into cost values (null,'计时收费',null,null,0.5,0,'0.5元/时,不使用不收费',default,default,null);
insert into cost values (null,'包月',null,20,null,0,'每月20元,不限制使用时间',default,default,null);
insert into cost values (null,'mm',10,5,null,0,'',default,default,null);
insert into cost values (null,'nn',10,7,null,0,'',default,default,null);
insert into cost values (null,'qq',10,4,null,0,'',default,default,null);
- 创建资费实体类Cost:
package entity;
public class Cost implements Serializable {
private Integer costId;
private String name;
private Integer baseDuration;
private Double baseCost;
private Double unitCost;
private String status;
private String descr;
private Timestamp creatime;
private Timestamp startime;
private String costType;
}
- Generate Getters and Setters
- Generate hashCode() and equals()
- Generate toString()
- (添加Mapper接口)资费数据访问接口:CostDao:
package dao;
import java.util.List;
import entity.Cost;
public interface CostDao {
List<Cost> findAll();
}
- 在Mapper(映射文件)配置添加SQL, CostMapper.xml:
<mapper namespace="dao.CostDao">
<select id="findAll" resultType="entity.Cost">
select
cost_id as costId,
name as name,
base_duration as baseDuration,
base_cost as baseCost,
unit_cost as unitCost,
status as status,
descr as descr,
creatime as creatime,
startime as startime,
cost_type as costType
from
cost
order by
#{costId}
</select>
- 在业务层增加查询资费的业务方法:
- 定义业务接口:CostService:定义查询资费方法:
public interface CostService {
List<Cost> findAll();
}
- 创建资费业务组件CostServiceImpl:
@Service("CostService")
public class CostServiceImpl implements CostService {
@Resource
private CostDao costDao;
public List<Cost> findAll() {
return costDao.findAll();
}
}
- 在控制器增加处理查询请求的方法
- 创建资费模块处理器CostController:
@Controller
@RequestMapping("/cost")
public class CostController {
@Resource
private CostService costService;
@RequestMapping("/find.do")
public String find(ModelMap model){
List<Cost> list = costService.findAll();
model.addAttribute("costs",list);
return "cost/cost_list";
}
}
- 在页面上使用JSTL+EL循环输出数据
- 在WEB-INF/jsp/cost下创建资费列表页面cost_list.jsp:
<%@page pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>达内-NetCTOSS</title>
<link type="text/css" rel="stylesheet" media="all" href="../styles/global.css" />
<link type="text/css" rel="stylesheet" media="all" href="../styles/global_color.css" />
<script language="javascript" type="text/javascript">
//排序按钮的点击事件
function sort(btnObj) {
if (btnObj.className == "sort_desc")
btnObj.className = "sort_asc";
else
btnObj.className = "sort_desc";
}
//启用
function startFee() {
var r = window.confirm("确定要启用此资费吗?资费启用后将不能修改和删除。");
document.getElementById("operate_result_info").style.display = "block";
}
//删除
function deleteFee() {
var r = window.confirm("确定要删除此资费吗?");
document.getElementById("operate_result_info").style.display = "block";
}
</script>
</head>
<body>
<!--Logo区域开始-->
<div id="header">
<img src="../images/logo.png" alt="logo" class="left"/>
<a href="#">[退出]</a>
</div>
<!--Logo区域结束-->
<!--导航区域开始-->
<div id="navi">
<ul id="menu">
<li><a href="../index.html" class="index_off"></a></li>
<li><a href="../role/role_list.html" class="role_off"></a></li>
<li><a href="../admin/admin_list.html" class="admin_off"></a></li>
<li><a href="../fee/fee_list.html" class="fee_on"></a></li>
<li><a href="../account/account_list.html" class="account_off"></a></li>
<li><a href="../service/service_list.html" class="service_off"></a></li>
<li><a href="../bill/bill_list.html" class="bill_off"></a></li>
<li><a href="../report/report_list.html" class="report_off"></a></li>
<li><a href="../user/user_info.html" class="information_off"></a></li>
<li><a href="../user/user_modi_pwd.html" class="password_off"></a></li>
</ul>
</div>
<!--导航区域结束-->
<!--主要区域开始-->
<div id="main">
<form action="" method="">
<!--排序-->
<div class="search_add">
<div>
<!--<input type="button" value="月租" class="sort_asc" οnclick="sort(this);" />-->
<input type="button" value="基费" class="sort_asc" οnclick="sort(this);" />
<input type="button" value="时长" class="sort_asc" οnclick="sort(this);" />
</div>
<input type="button" value="增加" class="btn_add" οnclick="location.href='fee_add.html';" />
</div>
<!--启用操作的操作提示-->
<div id="operate_result_info" class="operate_success">
<img src="../images/close.png" οnclick="this.parentNode.style.display='none';" />
删除成功!
</div>
<!--数据区域:用表格展示数据-->
<div id="data">
<table id="datalist">
<tr>
<th>资费ID</th>
<th class="width100">资费名称</th>
<th>基本时长</th>
<th>基本费用</th>
<th>单位费用</th>
<th>创建时间</th>
<th>开通时间</th>
<th class="width50">状态</th>
<th class="width200"></th>
</tr>
<c:forEach items="${costs}" var="c">
<tr>
<td>${c.costId}</td>
<td><a href="fee_detail.html">${c.name}</a></td>
<td>${c.baseDuration}</td>
<td>${c.baseCost}</td>
<td>${c.unitCost}</td>
<td>${c.creatime}</td>
<td>${c.startime}</td>
<td>
<c:if test="${c.status==0}">开通</c:if>
<c:if test="${c.status==1}">暂停</c:if>
</td>
<td>
<input type="button" value="启用" class="btn_start" οnclick="startFee();" />
<input type="button" value="修改" class="btn_modify" οnclick="location.href='fee_modi.html';" />
<input type="button" value="删除" class="btn_delete" οnclick="deleteFee();" />
</td>
</tr>
</c:forEach>
</table>
<p>业务说明:<br />
1、创建资费时,状态为暂停,记载创建时间;<br />
2、暂停状态下,可修改,可删除;<br />
3、开通后,记载开通时间,且开通后不能修改、不能再停用、也不能删除;<br />
4、业务账号修改资费时,在下月底统一触发,修改其关联的资费ID(此触发动作由程序处理)
</p>
</div>
<!--分页-->
<div id="pages">
<a href="#">上一页</a>
<a href="#" class="current_page">1</a>
<a href="#">2</a>
<a href="#">3</a>
<a href="#">4</a>
<a href="#">5</a>
<a href="#">下一页</a>
</div>
</form>
</div>
<!--主要区域结束-->
<div id="footer">
<p>[源自北美的技术,最优秀的师资,最真实的企业环境,最适用的实战项目]</p>
<p>版权所有(C)加拿大达内IT培训集团公司</p>
</div>
</body>
</html>
- 测试:
- 此处NULL值没有显示为0,故在SQL语句中加入IFNULL()函数:
IFNULL(base_duration,0) as baseDuration,
IFNULL(base_cost,0) as baseCost,
IFNULL(unit_cost,0) as unitCost,
- 浏览器访问http://localhost:8080/netctoss-trySSM/cost/find.do,浏览器显示结果如下图:
- 创建资费表,并插入预置数据:
DROP TABLE IF EXISTS `cost`; create table cost( cost_id int(4) auto_increment, name varchar(50) not null, base_duration int(11), base_cost double(7,2), unit_cost double(7,4), status char(1), descr varchar(100), creatime datetime default now(), startime datetime, cost_type char(1), constraint cost_id_pk primary key(cost_id) ); insert into cost values (null,'5.9元套餐',20,5.9,0.4,0,'5.9元20小时/月,超出部分0.4元/时',default,default,null); insert into cost values (null,'6.9元套餐',40,6.9,0.3,0,'6.9元40小时/月,超出部分0.3元/时',default,default,null); insert into cost values (null,'8.5元套餐',100,8.5,0.2,0,'8.5元100小时/月,超出部分0.2元/时',default,default,null); insert into cost values (null,'10.5元套餐',200,10.5,0.1,0,'10.5元200小时/月,超出部分0.1元/时',default,default,null); insert into cost values (null,'计时收费',null,null,0.5,0,'0.5元/时,不使用不收费',default,default,null); insert into cost values (null,'包月',null,20,null,0,'每月20元,不限制使用时间',default,default,null); insert into cost values (null,'mm',10,5,null,0,'',default,default,null); insert into cost values (null,'nn',10,7,null,0,'',default,default,null); insert into cost values (null,'qq',10,4,null,0,'',default,default,null);
- 创建资费实体类Cost:
package entity; public class Cost implements Serializable { private Integer costId; private String name; private Integer baseDuration; private Double baseCost; private Double unitCost; private String status; private String descr; private Timestamp creatime; private Timestamp startime; private String costType; }
- Generate Getters and Setters
- Generate hashCode() and equals()
- Generate toString()
package dao; import java.util.List; import entity.Cost; public interface CostDao { List<Cost> findAll(); }
- 在Mapper(映射文件)配置添加SQL, CostMapper.xml:
<mapper namespace="dao.CostDao"> <select id="findAll" resultType="entity.Cost"> select cost_id as costId, name as name, base_duration as baseDuration, base_cost as baseCost, unit_cost as unitCost, status as status, descr as descr, creatime as creatime, startime as startime, cost_type as costType from cost order by #{costId} </select>
- 定义业务接口:CostService:定义查询资费方法:
public interface CostService { List<Cost> findAll(); }
- 创建资费业务组件CostServiceImpl:
@Service("CostService") public class CostServiceImpl implements CostService { @Resource private CostDao costDao; public List<Cost> findAll() { return costDao.findAll(); } }
- 创建资费模块处理器CostController:
@Controller @RequestMapping("/cost") public class CostController { @Resource private CostService costService; @RequestMapping("/find.do") public String find(ModelMap model){ List<Cost> list = costService.findAll(); model.addAttribute("costs",list); return "cost/cost_list"; } }
- 在WEB-INF/jsp/cost下创建资费列表页面cost_list.jsp:
<%@page pageEncoding="utf-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>达内-NetCTOSS</title> <link type="text/css" rel="stylesheet" media="all" href="../styles/global.css" /> <link type="text/css" rel="stylesheet" media="all" href="../styles/global_color.css" /> <script language="javascript" type="text/javascript"> //排序按钮的点击事件 function sort(btnObj) { if (btnObj.className == "sort_desc") btnObj.className = "sort_asc"; else btnObj.className = "sort_desc"; } //启用 function startFee() { var r = window.confirm("确定要启用此资费吗?资费启用后将不能修改和删除。"); document.getElementById("operate_result_info").style.display = "block"; } //删除 function deleteFee() { var r = window.confirm("确定要删除此资费吗?"); document.getElementById("operate_result_info").style.display = "block"; } </script> </head> <body> <!--Logo区域开始--> <div id="header"> <img src="../images/logo.png" alt="logo" class="left"/> <a href="#">[退出]</a> </div> <!--Logo区域结束--> <!--导航区域开始--> <div id="navi"> <ul id="menu"> <li><a href="../index.html" class="index_off"></a></li> <li><a href="../role/role_list.html" class="role_off"></a></li> <li><a href="../admin/admin_list.html" class="admin_off"></a></li> <li><a href="../fee/fee_list.html" class="fee_on"></a></li> <li><a href="../account/account_list.html" class="account_off"></a></li> <li><a href="../service/service_list.html" class="service_off"></a></li> <li><a href="../bill/bill_list.html" class="bill_off"></a></li> <li><a href="../report/report_list.html" class="report_off"></a></li> <li><a href="../user/user_info.html" class="information_off"></a></li> <li><a href="../user/user_modi_pwd.html" class="password_off"></a></li> </ul> </div> <!--导航区域结束--> <!--主要区域开始--> <div id="main"> <form action="" method=""> <!--排序--> <div class="search_add"> <div> <!--<input type="button" value="月租" class="sort_asc" οnclick="sort(this);" />--> <input type="button" value="基费" class="sort_asc" οnclick="sort(this);" /> <input type="button" value="时长" class="sort_asc" οnclick="sort(this);" /> </div> <input type="button" value="增加" class="btn_add" οnclick="location.href='fee_add.html';" /> </div> <!--启用操作的操作提示--> <div id="operate_result_info" class="operate_success"> <img src="../images/close.png" οnclick="this.parentNode.style.display='none';" /> 删除成功! </div> <!--数据区域:用表格展示数据--> <div id="data"> <table id="datalist"> <tr> <th>资费ID</th> <th class="width100">资费名称</th> <th>基本时长</th> <th>基本费用</th> <th>单位费用</th> <th>创建时间</th> <th>开通时间</th> <th class="width50">状态</th> <th class="width200"></th> </tr> <c:forEach items="${costs}" var="c"> <tr> <td>${c.costId}</td> <td><a href="fee_detail.html">${c.name}</a></td> <td>${c.baseDuration}</td> <td>${c.baseCost}</td> <td>${c.unitCost}</td> <td>${c.creatime}</td> <td>${c.startime}</td> <td> <c:if test="${c.status==0}">开通</c:if> <c:if test="${c.status==1}">暂停</c:if> </td> <td> <input type="button" value="启用" class="btn_start" οnclick="startFee();" /> <input type="button" value="修改" class="btn_modify" οnclick="location.href='fee_modi.html';" /> <input type="button" value="删除" class="btn_delete" οnclick="deleteFee();" /> </td> </tr> </c:forEach> </table> <p>业务说明:<br /> 1、创建资费时,状态为暂停,记载创建时间;<br /> 2、暂停状态下,可修改,可删除;<br /> 3、开通后,记载开通时间,且开通后不能修改、不能再停用、也不能删除;<br /> 4、业务账号修改资费时,在下月底统一触发,修改其关联的资费ID(此触发动作由程序处理) </p> </div> <!--分页--> <div id="pages"> <a href="#">上一页</a> <a href="#" class="current_page">1</a> <a href="#">2</a> <a href="#">3</a> <a href="#">4</a> <a href="#">5</a> <a href="#">下一页</a> </div> </form> </div> <!--主要区域结束--> <div id="footer"> <p>[源自北美的技术,最优秀的师资,最真实的企业环境,最适用的实战项目]</p> <p>版权所有(C)加拿大达内IT培训集团公司</p> </div> </body> </html>
- 此处NULL值没有显示为0,故在SQL语句中加入IFNULL()函数:
IFNULL(base_duration,0) as baseDuration, IFNULL(base_cost,0) as baseCost, IFNULL(unit_cost,0) as unitCost,
- 浏览器访问http://localhost:8080/netctoss-trySSM/cost/find.do,浏览器显示结果如下图: