资费功能的新增(struts2+jsp)

需要2次请求,第一个请求是打开新增页面,第二个请求是保存新增的数据。

代码书写顺序:
第一次请求:
--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>  




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值