用java打印javascript(级联菜单)

package com.wang.struts.util;

import java.util.List;

import com.wang.struts.dao.QxDao;
import com.wang.struts.dao.implement.FactoryDao;
import com.wang.struts.dao.implement.FwlxImplementDao;
import com.wang.struts.dao.implement.JdImplementDao;
import com.wang.struts.dao.implement.QxImplementDao;
import com.wang.struts.vo.CountJdVo;
import com.wang.struts.vo.FwlxVo;
import com.wang.struts.vo.JdVo;
import com.wang.struts.vo.QxVO;

public class JspUtil {
  private static StringBuffer arrayStr = null;

     private static StringBuffer qxOptionsStr = null;

     private static StringBuffer fwlxOptionsStr = null;

   
     public static StringBuffer getArrayStr() {// synchronized
         if (arrayStr == null) {
             arrayStr = new StringBuffer();
            QxImplementDao qxProcess=FactoryDao.getFactoryQxImplementDao();//得到到对象
             JdImplementDao jdProcess = FactoryDao.getFactoryJdImplementDao();
             List qxList = qxProcess.getQxList();//调用dao,得到所有的区县
             List jdList = jdProcess.getJDList();//得到所有的街道列表
             List jdNum = jdProcess.getJDNums(); //根据区县得到所有的街道
      
             int qxLength = qxList.size();
             // int jdLength = jdList.size();
             // int jdNumLength = jdNum.size();
             int pointer = 0;// 指示
             arrayStr.append("var v = new Array(" + qxLength + ");\n");// option的value
             arrayStr.append("var n = new Array(" + qxLength + ");\n");// option的显示值

             for (int i = 0; i < qxLength; i++) {
                 int qxid = ((QxVO) qxList.get(i)).getQxId();
                 arrayStr.append("v[" + qxid + "] = new Array(");
           
                 int l = 0;
                 try {
                     l = ((CountJdVo) jdNum.get(i)).getTotal();//根据区县Id得到对应的街道数量
                 } catch (Exception e) {
                     // e.printStackTrace();
                     System.out.println("========无此区县("
                             + ((QxVO) qxList.get(i)).getQxId() + ")的街道资料========");
                 }
                 for (int j = 0; j < l; j++) {
                     int value = ((JdVo) jdList.get(pointer + j)).getJdid();
                     arrayStr.append("'" + value + "'");
                     if (j != l - 1) {
                         arrayStr.append(",");
                     }
                 }
                 arrayStr.append(");\n");
                 arrayStr.append("n[" + qxid + "] = new Array(");
                 for (int j = 0; j < l; j++) {
                     String name = ((JdVo) jdList.get(pointer + j)).getJd();//根据街道数量来遍历街道名称
                     arrayStr.append("'" + name + "'");
                     if (j != l - 1) {
                         arrayStr.append(",");
                     }
                 }
                 arrayStr.append(");\n");

                 pointer += l;
             }
         }
         return arrayStr;
     }

   
     public static StringBuffer getQxOptions() {
         if (qxOptionsStr == null) {
             qxOptionsStr = new StringBuffer();
             QxImplementDao qxProcess=FactoryDao.getFactoryQxImplementDao();
             List qxList = qxProcess.getQxList();
             for (int i = 1; i <= qxList.size(); i++) {

                 qxOptionsStr.append("<option value='"
                         + ((QxVO) qxList.get(i - 1)).getQxId() + "'>"
                         + ((QxVO) qxList.get(i - 1)).getQw() + "</option>\n");

             }
         }
         return qxOptionsStr;
     }

   
     public static StringBuffer getFwlxOptions() {
         if (fwlxOptionsStr == null) {
             fwlxOptionsStr = new StringBuffer();
             FwlxImplementDao fwlxProcess=FactoryDao.getFactoryFwlxImplementDao();
             List fwlxList = fwlxProcess.getFwlxList();
             for (int i = 1; i <= fwlxList.size(); i++) {

                 fwlxOptionsStr.append("<option value='"
                         + ((FwlxVo) fwlxList.get(i - 1)).getFwid() + "'>"
                         + ((FwlxVo) fwlxList.get(i - 1)).getFwlx()
                         + "</option>\n");
             }
         }
         return fwlxOptionsStr;
     }

   
     public static StringBuffer getFwxxStr(String fwxx){
         StringBuffer fwxxRes = new StringBuffer();
         if(fwxx.length()>35){
             int rows = fwxx.length()/35 + 1;
             for(int i=0;i<rows-1;i++){
                 fwxxRes.append(fwxx.subSequence(35*i,35*(i+1))+"<br/>");
               
             }
             fwxxRes.append(fwxx.subSequence(35*(rows-1), fwxx.length()-1));
         } else {
             fwxxRes.append(fwxx);
         }

         return fwxxRes;
     }


}
在jsp 中的用<%=jspUtil.getArrayStr()%> j即可,其他方法一样

如:



<html>

<head>

<title>级联菜单</title>

</head>

<script language="javascript">
<!--

<!--   取得javascript数组信息  -->
<%=JspUtil.getArrayStr()%>
function selectjd(){
  var row = document.houseForm["fzxx.qxid"].value;//alert(row);
  var col = v[row].length;//alert(col);
  document.houseForm["fzxx.jdId"].length = 0;
  document.houseForm["fzxx.jdId"].options[0] = new Option('不限--','0');
  for(var i=0;i<col;i++) {
   if(v[row][i]!=0){
    document.houseForm["fzxx.jdId"].options[i+1] = new Option(n[row][i],v[row][i]);
   }
  }
}

-->
</script>

<body>

<table width="88%;" id="advSearch" style="border:solid 0px #000;display:none;">
  
<tr>
    <td>区县:<br><br><br></td>
    <td><select name="fzxx.qxid" onChange="selectjd()" style="width:80">
    <option value="0">不限--</option>
     <%=JspUtil.getQxOptions() %>

    </select><br><br><br></td>
    <td rowspan="6">&nbsp;<br><br><br></td>
   </tr>
   <tr>
    <td>街道:<br><br><br></td>
    <td><select name="fzxx.jdId" style="width:80">
     <option value="0">不限--</option>
    </select><br><br><br></td>
   </tr>
</table>

</body>

</html>


你可能感兴趣的:(JavaScript,java,DAO,struts,J#)