我在JSP表单中遇到问题:
我需要根据用户选择的学生群体填写学生下拉列表。但它不起作用:Dropdown框仍然是空的。
studentBar.jsp
的代码:
<%--suppress JSUnresolvedFunction --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<c:url var="loadStudentsByGroup" value="/loadStudents" />
<script type="text/javascript">
$(document).ready(function() {
$('#groupId').change(
function() {
$.get('${loadStudentsByGroup}', {
groupId : $(this).val(),
ajax : 'true'
}, function(data) {
var html = '<option value="">Первый элемент</option>';
var len = data.length;
for ( var i = 0; i < len; i++) {
html += '<option value="' + data[i].studentsId + '">'
+ data[i].stFamily + '</option>';
}
html += '</option>';
$('#studentId').html(html);
});
});
});
</script>
<body>
<form:form method="post" modelAttribute="studentLogin" action="checkStudent">
<form:select id="groupId" path="">
<c:forEach items="${groupList}" var="group">
<form:option value="${group.groupId}" >${group.groupName}</form:option>
</c:forEach><br/>
</form:select>
<form:select id="studentId" path="name">
</form:select>
</form:form>
</body>
</html>
代码controller.java
@RequestMapping(value = "/loadStudents", headers = "Accept=*/*", method = RequestMethod.GET)
public @ResponseBody
List<StudentsEntity> loadStudents(@RequestParam(value = "groupId", required = true) Integer groupId) throws IllegalStateException {
List<StudentsEntity> stList = loadStudent(groupId);
return stList;
}
loadStudent
功能代码:
public static List<StudentsEntity> loadStudent(Integer grId){
Session session = getSession();
Query query = session.createQuery("FROM StudentsEntity where stGroup="+grId);
List<StudentsEntity> res = query.list();
session.close();
return res;
}