JSP页面中如何用select标签实现级联
发布时间:2024-04-22 12:59:20
标签:JSP,select标签,级联
做查询页面,查询条件比较多的时候往往会涉及到级联。举个简单的例子,拿教务系统来说,我们要查询教学计划信息,查询条件是入学批次、学生层次(专升本、高升专)、专业、课程。
它们之间有什么级联关系呢?入学批次影响学生层次(某个入学批次可能只有专升本或者高升专一个学生层次)、专业、课程,学生层次影响专业、课程,专业又影响课程。也就是说当选择入学批次时,学生层次、专业和课程的下拉框会局部刷新,选择学生层次时,专业和课程的下拉框会局部刷新,选择专业时,课程的下拉框也会局部刷新。
我们当然不希望已经选择的操作随着页面的刷新又被初始化,再者前面提到选择一项后相关的下拉框是局部刷新。很容易想到用填充页面的方法来实现级联。
笔者的填充方法是通过提交JS,由Controller获取数据,将数据传到辅助的JSP页面,再用回调函数将辅助JSP页面中的数据填充给相应下拉框。说的抽象,直接上代码了,四级级联稍微麻烦一些,知道原理后也好做,笔者上 * 级联的代码。级联样式如下图:
<table>
<tr>
<td width="400px" align="left">入学批次:<SELECT NAME="grade"
id="grade" onchange="refreshEduLevelAndSpecialAjax();"> //选择入学批次会刷新层次和专业
<OPTION VALUE="0">
--请选择--
<c:forEach items="${gradeInfo}" var="gradeInfo">
<OPTION VALUE="${gradeInfo.gradeName}">${gradeInfo.gradeName}
</c:forEach>
</SELECT></td>
<td width="400px" align="left">统考课程:<SELECT
NAME="uniExamCourseId" id="uniExamCourseId">
<OPTION VALUE="0">
--请选择--
<c:forEach items="${unifiedExamCourseList}" var="uniExamCourse">
<OPTION VALUE="${uniExamCourse.id}">${uniExamCourse.uniExamCourseName}
</c:forEach>
</SELECT></td>
</tr>
<tr>
<td colspan="2" id="refreshEduLevelAndSpecialAjax"> //设置ID,用于填充层次和专业的下拉框
<table>
<tr>
<td width="400" align="left">层 次:<SELECT
NAME="eduLevelId" id="eduLevelId"
onchange="refreshSpecialAjax();"> //选择层次后刷新专业
<OPTION VALUE="0">--请选择--</OPTION>
<c:forEach items="${educationLevel}" var="educationLevel">
<OPTION VALUE="${educationLevel.id}">${educationLevel.educationLevelName}
</c:forEach>
</SELECT></td>
<td width="400" align="left" id="refreshSpecialAjax">专 业:<SELECT //设置ID,用于填充专业的下拉框
NAME="specialId" id="specialId">
<OPTION VALUE="0">--请选择--</OPTION>
<c:forEach items="${specialList}" var="special">
<OPTION VALUE="${special.id}">${special.specialName}
</c:forEach>
</SELECT></td>
</tr>
</table>
</td>
</tr>
</table>
JS的代码如下:
//JavaScript Document
var xmlHttp; //用于保存XMLHttpRequest对象的全局变量
//用于创建XMLHttpRequest对象
function createXmlHttp() {
//根据window.XMLHttpRequest对象是否存在使用不同的创建方式
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest(); //FireFox、Opera等浏览器支持的创建方式
} else {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE浏览器支持的创建方式
}
}
function refreshEduLevelAndSpecialAjax() {
var grade = document.getElementById("grade").value;
refreshEduLevelAndSpecial(grade);
}
function refreshEduLevelAndSpecial(grade) {
createXmlHttp(); //创建XMLHttpRequest对象
xmlHttp.onreadystatechange = refreshEduLevelAndSpecialElement; //设置回调函数
xmlHttp.open("POST", "eduLevelAndSpecialByGradeNameInSpecialDetail",
true); //发送POST请求
xmlHttp.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
xmlHttp.send("grade=" + grade);
}
//处理服务器返回的信息 更新层次专业下拉框
function refreshEduLevelAndSpecialElement() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
//此处xmlHttp.responseText是请求的*Controller的某个方法返回的渲染页面的源代码
document.getElementById("refreshEduLevelAndSpecialAjax").innerHTML = xmlHttp.responseText;
}
}
}
function refreshSpecialAjax() {
var grade = document.getElementById("grade").value;
var eduLevelId = document.getElementById("eduLevelId").value;
refreshSpecial(grade, eduLevelId);
}
function refreshSpecial(grade, eduLevelId) {
createXmlHttp(); //创建XMLHttpRequest对象
xmlHttp.onreadystatechange = refreshSpecialElement; //设置回调函数
xmlHttp.open("POST", "specialByGradeNameAndEduLevelIdInSpecialDetail",
true); //发送POST请求
xmlHttp.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
xmlHttp.send("grade=" + grade + "&eduLevelId=" + eduLevelId);
}
//处理服务器返回的信息 更新专业下拉框
function refreshSpecialElement() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
//此处xmlHttp.responseText是请求的*Controller的某个方法返回的渲染页面的源代码
document.getElementById("refreshSpecialAjax").innerHTML = xmlHttp.responseText;
}
}
}
Controller代码:
@RequestMapping(value = "/eduLevelAndSpecialByGradeNameInSpecialDetail")
public ModelAndView getEduLevelAndSpecialByGradeNameInSpecialDetail(HttpServletRequest request,
HttpServletResponse response) throws JsonParseException, JsonMappingException, JSONException, IOException{
String gradeName=request.getParameter("grade");
String eduLevelId=request.getParameter("eduLevelId");
if(gradeName==null||gradeName.equals("0")){
gradeName="null";
}
if(eduLevelId==null||eduLevelId.equals("0")){
eduLevelId="null";
}
ArrayList<UtilObject> eduLevelList=uess.getEduLevelIdByGradeNameInSpecialDetail(gradeName);
ArrayList<UtilObject> specialIdList=uess.getSpecialIdByGradeNameAndEduLevelIdInSpecialDetail(gradeName, eduLevelId);
mav.addObject("educationLevel", eduLevelList);
mav.addObject("specialList", specialIdList);
mav.setViewName("scoreManage/uniExamScore/eduLevelAndSpecialAjax");
return mav;
}
@RequestMapping(value = "/specialByGradeNameAndEduLevelIdInSpecialDetail", method = RequestMethod.POST)
public ModelAndView getSpecialByGradeNameAndEduLevelIdInSpecialDetail(HttpServletRequest request,
HttpServletResponse response) throws JsonParseException, JsonMappingException, JSONException, IOException{
String gradeName=request.getParameter("grade");
String eduLevelId=request.getParameter("eduLevelId");
System.out.println("grade:"+gradeName+" eduLevelId:"+eduLevelId);
if(gradeName==null||gradeName.equals("0")){
gradeName="null";
}
if(eduLevelId==null||eduLevelId.equals("0")){
eduLevelId="null";
}
ArrayList<UtilObject> specialList=uess.getSpecialIdByGradeNameAndEduLevelIdInSpecialDetail(gradeName, eduLevelId);
mav.addObject("specialList", specialList);
mav.setViewName("scoreManage/uniExamScore/specialAjax");
return mav;
}
后台代码没有给出来,但应该看得懂,就是获取后台数据传到eduLevelAndSpecialAjax.jsp和specialAjax.jsp页面。这两个页面用于填充原页面,通过ID来填充相应区域,两个页面代码如下。
eduLevelAndSpecialAjax.jsp辅助页面:
<td id="refreshEduLevelAndSpecialAjax"> //ID用于填充原页面
<table>
<tr>
<td width="400px" align="left">层 次:<select
id="eduLevelId" name="eduLevelId" onchange="refreshSpecialAjax();">
<option value="0">--请选择--</option>
<c:forEach items="${educationLevel}" var="educationLevel">
<option value="${educationLevel.id}">${educationLevel.name}</option>
</c:forEach>
</select></td>
<td width="400px" align="left" id="refreshSpecialAjax">专 业:<SELECT //ID用于填充原页面
NAME="specialId" id="specialId">
<option value="0">--请选择--</option>
<c:forEach items="${specialList}" var="special">
<OPTION VALUE="${special.id}">${special.name}
</c:forEach>
</SELECT></td>
</tr>
</table>
</td>
specialAjax.jsp辅助页面:
<td width="400" align="left" id="refreshSpecialAjax">专 业:<SELECT
NAME="specialId" id="specialId"> //ID用于填充原页面
<option value="0">--请选择--</option>
<c:forEach items="${specialList}" var="special">
<OPTION VALUE="${special.id}">${special.name}
</c:forEach>
</SELECT></td>
这样就在JSP页面实现了填充。
0
投稿
猜你喜欢
- 前言yield的英文单词意思是生产,刚接触Python的时候感到非常困惑,一直没弄明白yield的用法。最近又重新学习了下,所以整理了下面这
- 本文实例为大家分享了python比特币初始配置的具体代码,供大家参考,具体内容如下# -*- coding: utf-8 -*- "
- 目录pyspark创建DataFrameRDD和DataFrame使用二元组创建DataFrame使用键值对创建DataFrame使用rdd
- 加载相关库import randomimport seaborn as snsimport matplotlib.pyplot as plt
- python中使用requests模块http请求时,发现中文参数不会自动的URL编码,并且没有找到类似urllib (python3)模块
- 该方是基于uiautomator2如下版本进行验证的:PS C:\windows\system32> pip show uiautom
- 本文主要介绍了Python3中PyQt5简单实现文件打开及保存,分享给大家,具体如下:# -*- coding: utf-8 -*-# Fo
- 如果遇到死锁了,怎么解决呢?找到原始的锁ID,然后KILL掉一直持有的那个线程就可以了, 但是众多线程,可怎么找到引起死锁的线程
- 环境:Ubuntu14、Python3.4、Pycharm2018一、使用command=lambda: 的形式传参代码如下from tki
- Python安装新包,pip是很好用的安装工具。pip list 可以查询所有已安装的包和版本。怎么知道本地安装包的版本是否有可以更新的新版
- 在Python中的while或者for循环之后还可以有else子句,作用是for循环中if条件一直不满足,则最后就执行else语句。for
- 简介Python发送邮件的教程本人在网站搜索的时候搜索出来了一大堆,但是都是说了一大堆原理然后就推出了实现代码,我测试用给出的代码进行发送邮
- 通过python与ffmpeg结合使用,可生成进行视频点播、直播的压力测试脚本。可支持不同类型的视频流,比如rtmp或者hls形式。 通过如
- 主要利用了setTimeout(),递归和String.substring();做出的效果就像是有一个打字员在打字.<!doctype
- 1 前言之前已经介绍了基金的变动信息,但是这些基金都是属于场外的,今天我们要介绍的是一个带门槛的投资产品-ETF。只有开立证券账户的玩家才能
- 上一篇博客selenium+python自动化测试(二)–使用webdriver操作浏览器讲解了使用webdriver操作浏览器的各种方法,
- 1 通过System.DBNull判断,网上大部分都使用这个方法。DataTable dt;
- Application Name(应用程序名称):应用程序的名称。如果没有被指定的话,它的值为.NET SqlClient Data Pro
- 一、自动化测试的概念性能系统负载能力稳定性过载操作下的系统瓶颈自动化测试,使用程序代替人工,可以提高测试效率性,自动化测试能自动化使用代码模
- 1、前言 MySQL 是完全网络化的跨平台关系型数据库系统,同时是具有客户机/服务器体系结构的分布式数据库管理系统。它具有功能强、使用简便、