添加分销商

采用ajax+servlet完成分销商代码是否重复的判断

function validateClientId(field){
  if(trim(field.value)!=""){
		var xmlHttp = null;
		//表示当前浏览器不是ie,如ns,firefox
		if(window.XMLHttpRequest) {
			xmlHttp = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		var url = "servlet/ClientIdValidateServlet?clientId=" + trim(field.value);
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
		xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				if (trim(xmlHttp.responseText) != "") {
					document.getElementById("spanClientId").innerHTML = xmlHttp.responseText; 
				}else {
					document.getElementById("spanClientId").innerHTML = "";
				}
			}else {
				alert("请求失败,错误码=【" + xmlHttp.status + "】");
			}
		}else{
		document.getElementById("spanClientId").innerHTML = "<font color='red'>正在检查,请稍候……</font>";
		}
	};
}else{
  document.getElementById("spanClientId").innerHTML="<font color='red'>分销商代码不能为空!</font>";
}
}

检查的servlet

public class ClientIdValidateServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		String clientId = request.getParameter("clientId");
		boolean flag = ClientManager.getInstance().findClientById(clientId);
		if(flag){
			response.getWriter().println("<font color='red'>分销商代码已经存在!</font>");
		}
	}
	
}

添加分销商页面

<!-- WebRoot/basedata/client_node_crud.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.study.drp.basedata.manager.*" %>
<%@ page import="com.study.drp.basedata.domain.*" %>
<%@ page import="com.study.drp.util.*" %>
<html>
<%
		int id = Integer.parseInt(request.getParameter("id"));
		
		Client client = ClientManager.getInstance().findClientOrRegionById(id);
		
		//删除区域
		String command = request.getParameter("command");
		if(Constants.Del.equals(command)){
			ClientManager.getInstance().delClientOrRegion(id);
%>
<script type="text/javascript">
	alert("用户【<%=client.getName()%>】删除成功!");
	window.parent.clientTreeFrame.location.reload();
</script>
<%			
		}
%>
	<head>
		<link rel="stylesheet" href="../style/drp.css" />
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>分销商维护</title>
		<script type="text/javascript">

	function addRegion() {
		window.self.location = "client_node_add.jsp?id=<%=id%>";	
	}
	
	function modifyRegion() {
		window.self.location = "client_node_modify.jsp?id=<%=id%>";
	}
	
	function deleteRegion() {
		if(window.confirm("确认删除吗?")){
			with(document.forms[0]){
				action = "client_node_crud.jsp";
				submit();
			}
		}
	}
	
	function addClient() {
		window.self.location = "client_add.jsp?id=<%=id%>";
	}
	
</script>
	</head>

	<body class="body1">
		<form id="clientForm" name="clientForm" method="post" action="">
			<input type="hidden" name="command" value="<%=Constants.Del %>">
			<input type="hidden" name="id" value="<%=id %>">
			<table width="95%" border="0" cellspacing="0" cellpadding="0"
				height="8">
				<tr>
					<td width="522" class="p1" height="2" nowrap="nowrap">
						<img src="../images/mark_arrow_02.gif" width="14" height="14" />
						 
						<b>基础数据管理>>分销商维护</b>
					</td>
				</tr>
			</table>
			<hr width="97%" align="center" size="0" />
			<p></p>
			<p></p>
			<table width="95%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td width="213">
						<div align="right">
							当前区域名称:
						</div>
					</td>
					<td width="410">
						<label>
							<input name="name" type="text" class="text1" id="name"
								readonly="true" value="<%=client.getName() %>"/>
						</label>
					</td>
				</tr>
			</table>
			<p></p>
			<label>
				<br />
			</label>
			<hr />
			<p align="center">
				<input name="btnAddRegion" type="button" class="button1"
					id="btnAddRegion" onClick="addRegion()" value="添加区域" />
				 
				<%
					if(id!=10000){//如果是所有分销商,就隐藏删除区域
				 %>
				<input name="btnDeleteRegion" type="button" class="button1"
					id="btnDeleteRegion" value="删除区域" onClick="deleteRegion()" />
				 
				<%} %>
				<input name="btnModifyRegion" type="button" class="button1"
					id="btnModifyRegion" onClick="modifyRegion()" value="修改区域" />
				 
				<input name="btnAddClient" type="button" class="button1"
					id="btnAddClient" onClick="addClient()" value="添加分销商" />
			</p>
		</form>
	</body>
</html>

添加页面

<!-- WebRoot/basedata/client_node_add.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.study.drp.basedata.domain.*" %>
<%@ page import="com.study.drp.basedata.manager.*"%>
<%@ page import="com.study.drp.util.*" %>
<%
	String command = request.getParameter("command");
	int id = Integer.parseInt(request.getParameter("id"));//原区域的ID作为新区域的pid
	String name = request.getParameter("name");//区域的名字
	if(command!= null && command.equals(Constants.Add)){
		Client client = new Client();
		client.setPid(id);
		client.setName(name);
		client.setIsLeaf(Constants.YES);
		client.setIsClient(Constants.NO);
		ClientManager.getInstance().addClientOrRegion(client);
		out.println("区域【"+name+"】添加成功");
	}
	
%>
<!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>
		<link rel="stylesheet" href="../style/drp.css" />
		<script src="../script/client_validate.js"></script>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>添加区域节点</title>
		<script type="text/javascript">
			function validateForm(form) {
				if (trim(form.name.value) == "") {
					alert("区域名称不能为空!");
					return false;
				}
				return true;
			}
		</script>
	</head>

	<body class="body1">
		<form name="regionForm" method="post" action="client_node_add.jsp?id=<%=id %>" οnsubmit="return validateForm(this)">
		<input type="hidden" name="command" value="<%=Constants.Add %>"/><!-- 传入隐含域 -->
			<table width="95%" border="0" cellspacing="0" cellpadding="0"
				height="8">
				<tr>
					<td width="522" class="p1" height="2" nowrap="nowrap">   
						<img src="../images/mark_arrow_03.gif" width="14" height="14" />
						 
						<b>基础数据管理>>分销商维护>>添加区域节点</b>
					</td>
				</tr>
			</table>
			<hr width="97%" align="center" size="0" />
			<p></p>
			<p></p>
			<table width="95%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td width="213">
						<div align="right">
							<font color="#FF0000">*</font>区域名称:
						</div>
					</td>
					<td width="410">
						<label>
							<input name="name" type="text" class="text1" id="name" />
						</label>
					</td>
				</tr>
			</table>
			<p></p>
			<label>
				<br />
			</label>
			<hr />
			<p align="center">
				<input name="btnAdd" class="button1" type="submit" value="添加" />
				    
				<input name="btnBack" class="button1" type="button" value="返回"
					οnclick="history.go(-1)" />
			</p>
		</form>
	</body>
</html>

添加分销商方法

public void addClientOrRegion(Client clientOrRegion) {
		// 1.取得分销商或区域的ID
		int id = IdGenerator.generate("t_client");
		// 2.添加分销商或区域
		StringBuffer sbSql = new StringBuffer();
		sbSql.append("insert into t_client ( ").append("id, pid, client_level_id, ").append("name, client_id, bank_acct_no, ")
		.append("contact_tel, address, zip_code, ").append("is_leaf, is_client) ").append("values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = DbUtil.getConnection();
			pstmt = conn.prepareStatement(sbSql.toString());
			// 手动控制事务提交
			DbUtil.beginTransaction(conn);
			pstmt.setInt(1, id);pstmt.setInt(2, clientOrRegion.getPid());
			pstmt.setString(3, clientOrRegion.getClientLevel() == null ? null: clientOrRegion.getClientLevel().getId());
			pstmt.setString(4, clientOrRegion.getName());pstmt.setString(5, clientOrRegion.getClientId());
			pstmt.setString(6, clientOrRegion.getBankAcctNo());pstmt.setString(7, clientOrRegion.getContactTel());
			pstmt.setString(8, clientOrRegion.getAddress());
			pstmt.setString(9, clientOrRegion.getZipCode());
			pstmt.setString(10, clientOrRegion.getIsLeaf());
			pstmt.setString(11, clientOrRegion.getIsClient());
			pstmt.executeUpdate();
			// 3.判断是否为叶子,若为叶子则修改为非叶子
			int pid = clientOrRegion.getPid();
			Client client = findClientOrRegionById(pid);
			String isLeaf = client.getIsLeaf();
			if (Constants.YES.equals(isLeaf)) {
				modifyIsLeafField(conn, pid, Constants.NO);
			}
			DbUtil.commitTransaction(conn);
		} catch (SQLException e) {
			e.printStackTrace();
			DbUtil.rollbackTransaction(conn);
		} finally {
			DbUtil.close(pstmt);
			DbUtil.close(conn);
		}
	}

修改叶子

/**
	 * 修改isLeaf字段
	 * 
	 * @param conn
	 *            连接
	 * @param id
	 * @param leaf
	 *            是否为叶子Y/N
	 */
	private void modifyIsLeafField(Connection conn, int id, String leaf) {
		String sql = "update t_client t set t.is_leaf=? where id=?";
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, leaf);
			pstmt.setInt(2, id);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.close(pstmt);
		}
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值