用java打印javascript(级联菜单)

本文介绍了一种使用Java和JSP实现动态级联菜单的方法,通过后台获取区县及街道数据,并将其转换为JavaScript数组,前端根据用户选择动态加载二级菜单选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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"> <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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值