客户端:
<script>
if (typeof($Q) != 'function') {
$Q = jQuery.noConflict();
}
var ajaxdataTemp = null;
/**当点击弹出树图的隐藏按钮时,先关闭树图,然后提交Ajax请求*/
$Q(function(){
$Q('#closeTree').click(function(){
/**隐藏树图**/
$Q('#checkboxtree').hide();
/**提交Ajax请求**/
$Q.get("<%=request.getContextPath()%>/syttyz/Syttyz/selectDistrict.do", {
dictrictcode: $Q('input[name=dictrictcode]').val()
}, function(data, textStatus){
try {
var decodeData = decodeURIComponent(decodeURIComponent(data));
alert(decodeURIComponent(decodeURIComponent(data)));//utf8解码
eval('ajaxdataTemp='+decodeData);;//把后台传过来的json数据转换为Object,否则得不到属性值
$Q('input[name=locationSheng]').attr("value",ajaxdataTemp.level3.districtName);//填充内容
$Q('input[name=locationShi]').attr("value",ajaxdataTemp.level4.districtName);//填充内容
$Q('input[name=locationXian]').attr("value",ajaxdataTemp.level5.districtName);//填充内容
// for (var i in ajaxdataTemp) {
// alert(i + ':' + ajaxdataTemp[i]);
// }
}
catch (e) {
alert(e.message);
}
});
});
});
</script>
服务器端:
private ActionForward performSelectDistrict(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
try {
String districtId = request.getParameter("dictrictcode");
PublicDao publicDao = new PublicDao();
Map districtMap = publicDao.getDistrict(districtId);
System.out.println("sss");
/
JSONObject jsonObject = JSONObject.fromObject(districtMap);
System.out.println(jsonObject);
String jsonStr = java.net.URLEncoder.encode(java.net.URLEncoder.encode(jsonObject.toString(),"UTF-8"),"UTF-8");
System.out.println(jsonStr);
PrintWriter out = response.getWriter();
out.print(jsonStr);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//该类递归取出省市县名称
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.boco.eoms.cache.ICacheDAO;
public class PublicDao extends ICacheDAO{
private Map districtAllMap = new HashMap();
private String districtLevel=null;
private String parentDistrict=null;
public Map getDistrict(String deptCode){
com.boco.eoms.db.util.BocoConnection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Map districtMap = new HashMap();
try {
conn = ds.getConnection();
String sql = new String();
sql = "select districtid,districtName,districtAbbr,parentDistrict,districtRemark from TAW_DISTRICT where districtid=?" ;
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, deptCode);
rs = pstmt.executeQuery();
while (rs.next()) {
parentDistrict = rs.getString("parentDistrict");
districtLevel = rs.getString("districtRemark");
if(districtLevel.startsWith("1") || districtLevel.startsWith("2")){
return districtAllMap;
}
districtMap.put("districtid", rs.getString("districtId"));
districtMap.put("districtName", rs.getString("districtName"));
districtMap.put("districtAbbr", rs.getString("districtAbbr"));
districtMap.put("parentDistrict", rs.getString("parentDistrict"));
districtMap.put("districtRemark", rs.getString("districtRemark"));
}
/**把县级,地级,省级这些地区信息放到map里面*/
System.out.println("level::"+districtLevel);
districtAllMap.put("level"+districtLevel.charAt(0)+"", districtMap);
if(!districtLevel.startsWith("3")){
getDistrict(parentDistrict);
}
close(rs);
close(pstmt);
} catch (SQLException e) {
close(rs);
close(pstmt);
rollback(conn);
e.printStackTrace();
} finally {
close(conn);
}
return districtAllMap;
}
}