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">层nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;次: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">专nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;业: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";
}
ArrayListUtilObject> eduLevelList=uess.getEduLevelIdByGradeNameInSpecialDetail(gradeName);
ArrayListUtilObject> 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";
}
ArrayListUtilObject> 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">层nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;次: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">专nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;业: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">专nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;业: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页面实现了填充。