jquery

=====================================================一、jsp页面部分代码=====================================================

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<head>

<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>

<title>无标题文档</title>

<scripttype='text/javascript'src='../js/jquery.js'></script>

<scripttype='text/javascript'>

varparent_id="<s:propertyvalue="user.suboffice_id"/>";

varchild_id="<s:propertyvalue="user.section_id"/>";

$(document).ready(function(){

query_dept_list();

});

//获取上级部门列表

functionquery_dept_list(){

$.ajax({

type:"GET",

url:"query_deptno_list.action?set="+newDate(),

dataType:"json",

success:function(data){

varjson=eval(data);

for(vari=0,len=json.length;i<len;i++){

$('#deptnameChild').empty();

$('#deptnameChild').append("<optionvalue=\"0\">---请选择子部门---</option>");

if(parent_id==json[i].departid)

$('#deptnameParent').append($("<optionvalue='"+json[i].departid+"'selected>"+json[i].departname+"</option>"));

else

$('#deptnameParent').append($("<optionvalue='"+json[i].departid+"'>"+json[i].departname+"</option>"));

}

$('#deptnameParent').trigger("change");

}

});

//当上级部门选项发生变化

$('#deptnameParent').change(function(){

$('#deptnameChild').empty();

$('#deptnameChild').append("<optionvalue=\"0\">---请选择子部门---</option>");

//当上级部门选项发生变化时,其对应的子部门也随着改变

query_dept_child_list();

});

}

functionquery_dept_child_list(){

//获取选中的上级部门的编号departid

vardeptnameParent=document.getElementById('deptnameParent').value;

if(deptnameParent=="0"){

$('#deptnameChild').empty();

$('#deptnameChild').append("<optionvalue=\"0\">---请选择子部门---</option>");

}else{

$.ajax({

type:"GET",

url:"query_dept_child_list.action?pid="+deptnameParent,

dataType:"json",

success:function(data){

varjson=eval(data);

for(vari=0,len=json.length;i<len;i++){

if(child_id==json[i].departid)

$('#deptnameChild').append($("<optionvalue='"+json[i].departid+"'selected>"+json[i].departname+"</option>"));

else

$('#deptnameChild').append($("<optionvalue='"+json[i].departid+"'>"+json[i].departname+"</option>"));

}

}

});

}

}

</script>

</head>

<body>

<formname="form1"method="post"action="update_user.action">

<tablewidth="100%"border="0"cellpadding="0"cellspacing="1"bgcolor="#FFFFFF">

<tr>

<tdwidth="10%"height="20"class="STYLE6">

<divalign="right">

<spanclass="STYLE10">部门:</span>

</div>

</td>

<tdnoWrapwidth="32%">

<selectname="user.suboffice_id"id="deptnameParent"style="width:155px;">

<optionvalue="0">

---请选择父部门---

</option>

</select>

&nbsp;&nbsp;

<selectname="user.section_id"id="deptnameChild"style="width:155px;">

<optionvalue="0">

---请选择子部门---

</option>

</select>

</td>

</tr>

</table>

</form>

</body>

</html>

=====================================================二、struts.xml部分代码=====================================================

<struts>

<packagename="user"extends="struts-default"namespace="/user">

<actionname="update_user"class="com.moa.user.UserAction"method="update_user">

<resultname="success">user_list.action</result>

<resultname="fail">user_list.action</result>

</action>

</package>

</struts>

=====================================================三、ImeiAction.java部分代码=====================================================

/*

*获取上级部门及其子部门列表返回给jsp

*/

publicStringquery_deptno_list()throwsException{

ImeiHandleimeiHandle=newImeiHandle();

//获取上级部门及其子部门列表

List<SysDepartmentModel>deptList=imeiHandle.getDeptPCList();

JSONArrayjsonArray=JSONArray.fromObject(deptList);

PrintWriterwriter=null;

try{

writer=ServletActionContext.getResponse().getWriter();

}catch(IOExceptione){

e.printStackTrace();

}

writer.write(jsonArray.toString());

writer.flush();

if(writer!=null){

writer.close();

}

returnnull;

}

/*

*获取上级部门编号为cid的子部门列表返回给jsp

*/

publicStringquery_dept_child_list()throwsException{

HttpServletRequestrequest=ServletActionContext.getRequest();

Stringpid=request.getParameter("pid");

ImeiHandleimeiHandle=newImeiHandle();

//获取获取上级部门编号为cid的子部门列表

List<SysDepartmentModel>deptList=imeiHandle.getDeptCList(pid);

JSONArrayjsonArray=JSONArray.fromObject(deptList);

PrintWriterwriter=null;

try{

writer=ServletActionContext.getResponse().getWriter();

}catch(IOExceptione){

e.printStackTrace();

}

writer.write(jsonArray.toString());

writer.flush();

if(writer!=null){

writer.close();

}

returnnull;

}

}

=====================================================四、ImeiHandle.java部分代码=====================================================

/**

*获取上级部门列表

*/

publicList<SysDepartmentModel>getDeptPCList(){

//上级部门的集合

List<SysDepartmentModel>sysdepartmentList=newArrayList<SysDepartmentModel>();

Stringsql="selectDEPARTID,DEPARTNAME,SUPERDEPARTIDfromsys_departmentwhereSUPERDEPARTID='0'";

DBCPPooldbcpPool=newDBCPPool();

Connectionconn=null;

Statementpstm=null;

ResultSetrs=null;

try{

conn=dbcpPool.getConnection();

pstm=dbcpPool.getStatement(conn);

rs=dbcpPool.getRowSet(pstm,sql);

while(rs.next()){

SysDepartmentModelsysDepartmentModel=newSysDepartmentModel();

sysDepartmentModel.setDepartid(rs.getString("DEPARTID"));

sysDepartmentModel.setDepartname(rs.getString("DEPARTNAME"));

//sysDepartmentModel.setSuperdepartid(rs.getString("SUPERDEPARTID"));

sysdepartmentList.add(sysDepartmentModel);

}

}catch(Exceptione){

e.printStackTrace();

}finally{

try{

dbcpPool.closeConn(conn,pstm,null,null);

}catch(Exceptione){

logger.info(e.getMessage());

}

}

returnsysdepartmentList;

}

/**

*获取上级部门编号为DEPARTID的子部门列表

*/

publicList<SysDepartmentModel>getDeptCList(Stringpid){

//上级部门的集合

List<SysDepartmentModel>smallList=newArrayList<SysDepartmentModel>();

Stringsql="selectDEPARTID,DEPARTNAME,SUPERDEPARTIDfromsys_departmentwhereSUPERDEPARTID='"+pid+"'";

DBCPPooldbcpPool=newDBCPPool();

Connectionconn=null;

Statementpstm=null;

ResultSetrs=null;

try{

conn=dbcpPool.getConnection();

pstm=dbcpPool.getStatement(conn);

rs=dbcpPool.getRowSet(pstm,sql);

while(rs.next()){

SysDepartmentModelsmallModel=newSysDepartmentModel();

smallModel.setDepartid(rs.getString("DEPARTID"));

smallModel.setDepartname(rs.getString("DEPARTNAME"));

smallModel.setSuperdepartid(rs.getString("SUPERDEPARTID"));

smallList.add(smallModel);

}

}catch(Exceptione){

e.printStackTrace();

}finally{

try{

dbcpPool.closeConn(conn,pstm,null,null);

}catch(Exceptione){

logger.info(e.getMessage());

}

}

returnsmallList;

}

=====================================================五、SysDepartmentModel.java部分代码=====================================================

packagecom.moa.model;

importjava.io.Serializable;

publicclassSysDepartmentModelimplementsSerializable{

/*

*部门编号

*/

privateStringdepartid;

/*

*部门名称

*/

privateStringdepartname;

/*

*上级部门编号

*/

privateStringsuperdepartid;

publicStringgetDepartid(){

returndepartid;

}

publicvoidsetDepartid(Stringdepartid){

this.departid=departid;

}

publicStringgetDepartname(){

returndepartname;

}

publicvoidsetDepartname(Stringdepartname){

this.departname=departname;

}

publicStringgetSuperdepartid(){

returnsuperdepartid;

}

publicvoidsetSuperdepartid(Stringsuperdepartid){

this.superdepartid=superdepartid;

}

publicSysDepartmentModel(Stringdepartid,Stringdepartname,Stringsuperdepartid){

super();

this.departid=departid;

this.departname=departname;

this.superdepartid=superdepartid;

}

publicSysDepartmentModel(){

super();

}

}

=====================================================六、User.java部分代码=====================================================

packagecom.moa.model;

importjava.io.Serializable;

publicclassUserimplementsSerializable{

privateStringsuboffice_id;

privateStringsection_id;

publicStringgetSuboffice_id(){

returnsuboffice_id;

}

publicvoidsetSuboffice_id(Stringsuboffice_id){

this.suboffice_id=suboffice_id;

}

publicStringgetSection_id(){

returnsection_id;

}

publicvoidsetSection_id(Stringsection_id){

this.section_id=section_id;

}

publicUser(Stringsuboffice_id,Stringsection_id){

super();

this.suboffice_id=suboffice_id;

this.section_id=section_id;

}

publicUser(){

super();

}

}

=====================================================七、sys_department表=====================================================

createtableSYS_DEPARTMENT

(

DEPARTIDVARCHAR(6)notnull,--部门编号

DEPARTNAMEVARCHAR(80),--部门名称

SUPERDEPARTIDVARCHAR(6),--上级部门编号

);

insertintosys_department(DEPARTID,DEPARTNAME,SUPERDEPARTID)

values('1','父部门1','0');

insertintosys_department(DEPARTID,DEPARTNAME,SUPERDEPARTID)

values('2','父部门2','0');

insertintosys_department(DEPARTID,DEPARTNAME,SUPERDEPARTID)

values('3','父部门2','0');

insertintosys_department(DEPARTID,DEPARTNAME,SUPERDEPARTID)

values('11','子部门11','1');

insertintosys_department(DEPARTID,DEPARTNAME,SUPERDEPARTID)

values('12','子部门12','1');

insertintosys_department(DEPARTID,DEPARTNAME,SUPERDEPARTID)

values('13','子部门13','1');

insertintosys_department(DEPARTID,DEPARTNAME,SUPERDEPARTID)

values('21','子部门21','2');

insertintosys_department(DEPARTID,DEPARTNAME,SUPERDEPARTID)

values('22','子部门22','2');

insertintosys_department(DEPARTID,DEPARTNAME,SUPERDEPARTID)

values('23','子部门23','2');

insertintosys_department(DEPARTID,DEPARTNAME,SUPERDEPARTID)

values('31','子部门31','3');

insertintosys_department(DEPARTID,DEPARTNAME,SUPERDEPARTID)

values('32','子部门32','3');

insertintosys_department(DEPARTID,DEPARTNAME,SUPERDEPARTID)

values('33','子部门33','3');

相关推荐