Posted on 2006-05-25 01:10
誰伴我闖蕩 閱讀(2821)
評論(3) 編輯 收藏
接著上次的話題,下面的就是學生注冊時需要的學院,專業,班級,三層列表,
學院:
<html:select property="instituteId" onchange="getDepartments(this.value)">
?????<html:options collection="institutes" property="value" labelProperty="label"/>
????</html:select>
專業:
<html:select property="departmentId" styleId="department" onchange="getClasses(this.value)"></html:select>
班級:
<html:select property="classId" styleId="classid" ></html:select>
學院是上來就應該有的,我們把他放到了LabelValueBean里
public Vector<LabelValueBean> getInstitutes()
??? {
??????? Connection connection = null;
??????? PreparedStatement pstmt = null;
??????? ResultSet rs = null;
??????? try
??????? {
??????????? connection = getConnection();
??????????? pstmt = connection.prepareStatement( "select * from institute" );
??????????? rs = pstmt.executeQuery();
??????????? Vector<LabelValueBean> institutes = new Vector<LabelValueBean>();
??????????? institutes.add( new LabelValueBean( "請選擇所在學院", "" ) );
??????????? while ( rs.next() )
??????????? {
??????????????? institutes.add( new LabelValueBean(
??????????????????????? rs.getString( "institute" ), rs.getString( "id" ) ) );
??????????? }
??????????? return institutes;
??????? }
??????? catch ( Exception e )
??????? {
??????????? e.printStackTrace();
??????? }
??????? finally
??????? {
??????????? close( rs );
??????????? close( pstmt );
??????????? close( connection );
??????? }
??????? return null;
??? }
而當它選擇了一個學院后,相應的getDepartments(this.value)的js腳本就該工作了,還是四步
var xmlHttp;
function createXMLHttpRequest()
{
?if (window.XMLHttpRequest)
?{
??xmlHttp = new XMLHttpRequest();
?}
?else if (window.ActiveXObject)
?{
??xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
??? }
}
發出請求
function getDepartments(institute)
{
?createXMLHttpRequest()
?var url = "ajax.do?institute="+institute+"&method=getDepartments"
?xmlHttp.open("GET",url, true)
?xmlHttp.onreadystatechange = departments
?xmlHttp.send(null)
}
處理響應
function departments()
{
?if (xmlHttp.readyState == 4)
?{
??if (xmlHttp.status == 200)
??{
???resText = xmlHttp.responseText
???each = resText.split("|")
???buildSelect( each, document.getElementById("departmentId"), "請選擇所在專業");
??}
?}
}
function buildSelect(str,sel,label)
{
?sel.options.length=0;
?sel.options[sel.options.length]=new Option(label,"")
?for(var i=0;i<str.length;i++)
?{
??each=str[i].split(",")
??sel.options[sel.options.length]=new Option(each[0],each[1])
?}
}
我把從數據庫中得到的各個專業進行了編碼,之后再這里再回歸回去,下面的是編碼過程
public StringBuffer getDepartmentsByInstituteIdForAjax( Long instituteId )
??? {
??????? Connection connection = null;
??????? PreparedStatement pstmt = null;
??????? ResultSet rs = null;
??????? try
??????? {
??????????? connection = getConnection();
??????????? pstmt = connection
??????????????????? .prepareStatement( "select * from department where instituteID=?" );
??????????? pstmt.setLong( 1, instituteId );
??????????? rs = pstmt.executeQuery();
??????????? StringBuffer sb = new StringBuffer();
??????????? while ( rs.next() )
??????????? {
??????????????? sb.append( rs.getString( "department" ) + ","
??????????????????????? + rs.getLong( "id" ) );
??????????????? if ( !rs.isLast() ) sb.append( "|" );
??????????? }
??????????? return sb;
??????? }
??????? catch ( Exception e )
??????? {
??????????? e.printStackTrace();
??????? }
??????? finally
??????? {
??????????? close( rs );
??????????? close( pstmt );
??????????? close( connection );
??????? }
??????? return null;
??? }
當然這些都是由
public ActionForward getDepartments( ActionMapping mapping,
??????????? ActionForm form, HttpServletRequest req, HttpServletResponse res )
??????????? throws Exception
??? {
??????? Service service = getService();
??????? res.getWriter().write(
??????????????? service.getDepartmentsByInstituteIdForAjax(
??????????????????????? Long.parseLong( req.getParameter( "institute" ) ) )
??????????????????????? .toString() );
??????? return null;
??? }
來控制
===========班級的再這里
public ActionForward getClasses( ActionMapping mapping, ActionForm form,
??????????? HttpServletRequest req, HttpServletResponse res ) throws Exception
??? {
??????? Service service = getService();
??????? res.getWriter().write(
??????????????? service.getClassesByDepartmentIdForAjax(
??????????????????????? Long.parseLong( req.getParameter( "department" ) ) )
??????????????????????? .toString() );
??????? return null;
??? }
public StringBuffer getClassesByDepartmentIdForAjax( Long departmentId )
??? {
??????? Connection connection = null;
??????? PreparedStatement pstmt = null;
??????? ResultSet rs = null;
??????? try
??????? {
??????????? connection = getConnection();
??????????? pstmt = connection
??????????????????? .prepareStatement( "select * from class where departmentID=?" );
??????????? pstmt.setLong( 1, departmentId );
??????????? rs = pstmt.executeQuery();
??????????? StringBuffer sb = new StringBuffer();
??????????? while ( rs.next() )
??????????? {
??????????????? sb.append( rs.getString( "class" ) + "," + rs.getLong( "id" ) );
??????????????? if ( !rs.isLast() ) sb.append( "|" );
??????????? }
??????????? return sb;
??????? }
??????? catch ( Exception e )
??????? {
??????????? e.printStackTrace();
??????? }
??????? finally
??????? {
??????????? close( rs );
??????????? close( pstmt );
??????????? close( connection );
??????? }
??????? return null;
??? }
function getClasses(department)
{
?createXMLHttpRequest()
?var url = "ajax.do?department="+department+"&method=getClasses"
?xmlHttp.open("GET",url, true)
?xmlHttp.onreadystatechange = classes
?xmlHttp.send(null)
}
function classes()
{
?if (xmlHttp.readyState == 4)
?{
??if (xmlHttp.status == 200)
??{
???resText = xmlHttp.responseText
???each = resText.split("|")
???buildSelect( each, document.getElementById("classid"), "請選擇所在班級");
??}
?}
}