需要2次请求,第一个请求是打开新增页面,第二个请求是保存新增的数据。
代码书写顺序:
第一次请求:
--struts.xml
--列表页面新增按钮,单击事件
-->JSP
第二次请求:
--DAO
--Action
--struts.xml
--addCost.jsp
Action:
struts.xml
Jsp:
代码书写顺序:
第一次请求:
--struts.xml
--列表页面新增按钮,单击事件
-->JSP
第二次请求:
--DAO
--Action
--struts.xml
--addCost.jsp
DAO:
public class CostDAOImpl implements ICostDAO{
public List<Cost> findAll() throws DAOException {
List<Cost> list = new ArrayList<Cost>();
String sql = "select * from COST";
Connection con = DBUtil.getConnection();
try {
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
Cost c = createCost(rs);
list.add(c);
}
} catch (SQLException e) {
e.printStackTrace();
throw new DAOException("查询全部资费数据失败!", e);
}finally{
DBUtil.close(con);
}
return list;
}
private Cost createCost(ResultSet rs) throws SQLException{
Cost c = new Cost();
c.setId(rs.getInt("id"));
c.setName(rs.getString("name"));
c.setBaseDuration(rs.getInt("base_duration"));
c.setBaseCost(rs.getDouble("base_cost"));
c.setUnitCost(rs.getDouble("unit_cost"));
c.setStatus(rs.getString("status"));
c.setDescr(rs.getString("descr"));
c.setCreateTime(rs.getDate("creatime"));
c.setStartTime(rs.getDate("startime"));
c.setCostType(rs.getString("cost_type"));
return c;
}
public static void main(String[] args) throws DAOException {
ICostDAO dao = new CostDAOImpl();
Integer pages = dao.findTotalpage(2);
System.out.println(pages);
}
public List<Cost> findByPage(Integer page, Integer pageSize)
throws DAOException {
List<Cost> list = new ArrayList<Cost>();
String sql = "select * from (" +
" select c.*, rownum r from cost c" +
") where r>? and r<?";
Connection con = DBUtil.getConnection();
try {
PreparedStatement ps = con.prepareStatement(sql);
//小于下一页的最小行
int nextMin = page*pageSize + 1;
//大于上一页的最大行
int lastMax = (page-1)*pageSize;
ps.setInt(1, lastMax);
ps.setInt(2, nextMin);
ResultSet rs = ps.executeQuery();
while(rs.next()){
Cost c = createCost(rs);
list.add(c);
}
} catch (SQLException e) {
e.printStackTrace();
throw new DAOException("分页查询资费数据失败!", e);
}
return list;
}
public Integer findTotalpage(Integer pageSize) throws DAOException {
//查询总行数
String sql = "select count(*) from cost";
Connection con = DBUtil.getConnection();
try {
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()){
//根据总行数计算总页数
int rows = rs.getInt(1);
if(rows%pageSize == 0){
return rows/pageSize;
}else{
return rows/pageSize + 1;
}
}
} catch (SQLException e) {
e.printStackTrace();
throw new DAOException("查询总行数失败", e);
}finally{
DBUtil.close(con);
}
return 0;
}
public void delete(Integer id) throws DAOException {
if(id==null){
return;
}
String sql = "delete from cost where id=?";
Connection con = DBUtil.getConnection();
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
throw new DAOException("删除资费数据失败", e);
}finally{
DBUtil.close(con);
}
}
public void insert(Cost cost) throws DAOException {
if(cost == null){
return;
}
String sql = "insert into cost values(cost_seq.nextval,?,?,?,?,'1',?,sysdate,null,?)";
Connection con = DBUtil.getConnection();
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.setObject(1, cost.getName());
ps.setObject(2, cost.getBaseDuration());
ps.setObject(3, cost.getBaseCost());
ps.setObject(4, cost.getUnitCost());
ps.setObject(5, cost.getDescr());
ps.setObject(6, cost.getCostType());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
throw new DAOException("新增数据失败",e);
}finally{
DBUtil.close(con);
}
}
}
Action:
public class AddCostAction {
public String execute(){
ICostDAO dao = DAOFactory.getCostDAO();
try {
dao.insert(cost);
} catch (DAOException e) {
e.printStackTrace();
return "error";
}
return "success";
}
public Cost getCost() {
return cost;
}
public void setCost(Cost cost) {
this.cost = cost;
}
//input
private Cost cost;
}
struts.xml
<!-- 跳转到新增页面 -->
<action name="toAddCost">
<result name="success">
/WEB-INF/cost/addCost.jsp
</result>
</action>
<!-- 新增保存Action -->
<action class="netctoss.action.AddCostAction" name="addCost">
<result name="success" type="redirectAction">
findCost
</result>
</action>
Jsp:
<!--findCost.jsp-->
<input type="button" value="增加" class="btn_add" οnclick="location.href='toAddCost';" />
<!--addCost.jsp-->
<form action="addCost" method="post" class="main_form">
<div class="text_info clearfix"><span>资费名称:</span></div>
<div class="input_info">
<input type="text" class="width300" name="cost.name"/>
<span class="required">*</span>
<div class="validate_msg_short">50长度的字母、数字、汉字和下划线的组合</div>
</div>
<div class="text_info clearfix"><span>资费类型:</span></div>
<div class="input_info fee_type">
<input type="radio" name="cost.costType" value="1" id="monthly" οnclick="feeTypeChange(1);" />
<label for="monthly">包月</label>
<input type="radio" name="cost.costType" value="2" checked="checked" id="package" οnclick="feeTypeChange(2);" />
<label for="package">套餐</label>
<input type="radio" name="cost.costType" value="3" id="timeBased" οnclick="feeTypeChange(3);" />
<label for="timeBased">计时</label>
</div>
<div class="text_info clearfix"><span>基本时长:</span></div>
<div class="input_info">
<input type="text" name="cost.baseDuration" class="width100" />
<span class="info">小时</span>
<span class="required">*</span>
<div class="validate_msg_long">1-600之间的整数</div>
</div>
<div class="text_info clearfix"><span>基本费用:</span></div>
<div class="input_info">
<input type="text" name="cost.baseCost" class="width100" />
<span class="info">元</span>
<span class="required">*</span>
<div class="validate_msg_long error_msg">0-99999.99之间的数值</div>
</div>
<div class="text_info clearfix"><span>单位费用:</span></div>
<div class="input_info">
<input type="text" name="cost.unitCost" class="width100" />
<span class="info">元/小时</span>
<span class="required">*</span>
<div class="validate_msg_long error_msg">0-99999.99之间的数值</div>
</div>
<div class="text_info clearfix"><span>资费说明:</span></div>
<div class="input_info_high">
<textarea name="cost.descr" class="width300 height70"></textarea>
<div class="validate_msg_short error_msg">100长度的字母、数字、汉字和下划线的组合</div>
</div>
<div class="button_info clearfix">
<input type="submit" value="保存" class="btn_save" />
<input type="button" value="取消" class="btn_save" οnclick="window.history.go(-1);"/>
</div>
</form>