JSP的那些事兒(4)—-DWR實現級聯 & JSP頁面的查詢

來源:互聯網
上載者:User

Date:2009.05.07

 

Problem:

(1) 利用DWR實現三層級聯:函數實現,但是無法將js中的object對象轉換為使用者自訂的對象VideoCateroy

(2)實現JSP頁面的查詢操作:如何將JS返回的str用於SQL語句

 

 一、利用DWR實現三層級聯

1、預期實現結構分析

七年級

---上學期

-------語文

-------數學

-------英語

---下學期

-------語文

-------數學

-------英語

2、編寫Java類:getChildCategoryByParentId(int id)

package com.gslsoft.dao;<br />/*通過父類Id尋找子類目*/<br />@SuppressWarnings("finally")<br />public List<VideoCategory> getChildCategoryByParentId(int id)<br />{<br />String sql="select * from video_category where parentId='"+id+"'";<br />List<VideoCategory> list=new ArrayList();<br />VideoCategory Category=null;<br />try{<br />CachedRowSet rs = DBConnection.getResultSet(sql);<br />while(rs.next()) {<br />Category= new VideoCategory();<br />Category.setName(rs.getString("name"));<br />Category.setRemark(rs.getString("remark"));<br />Category.setLevel(rs.getInt("level"));<br />Category.setImage(rs.getString("image"));<br />Category.setCategoryId(rs.getInt("id"));<br />list.add(Category);<br />}<br />rs.close();<br />} catch (java.sql.SQLException e) {<br />Logger.log(Logger.ERROR,<br />"DataRepository_getVideoFileByCategoryId: with CategoryId=" + id);<br />Logger.log(Logger.ERROR, e.toString());<br />} finally {<br />return list;<br />}<br />}

 

3、編寫WEB-INF/dwr.xml:具體的配置說明在前面一章給出了詳細的說明

<?xml version="1.0" encoding="GBK"?><br /><dwr><br /> <allow><br /><create creator="new" javascript="videocategoryservice"><br /> <param name="class" value="com.gslsoft.service.VideoCategoryService" /><br /></create><br /> </allow><br /></dwr></p><p>

 

4、編寫Test.jsp頁面

<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%><br /><%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%><br /><%@ page import="java.util.*" %><br /><%@ page import="com.gslsoft.service.*" %><br /><%@ page import="com.gslsoft.model.*" %><br /><html><br /><head><br /><title>檔案上傳</title><br /><link rel="stylesheet" href="../../css/x_css1.css" mce_href="css/x_css1.css" type="text/css"><br /><mce:script type='text/javascript' src="/app/dwr/interface/videocategoryservice.js" mce_src="app/dwr/interface/videocategoryservice.js"></mce:script><br /><mce:script type='text/javascript' src="/app/dwr/interface/VideoCategory.js" mce_src="app/dwr/interface/VideoCategory.js"></mce:script><br /><mce:script type='text/javascript' src="/app/dwr/engine.js" mce_src="app/dwr/engine.js"></mce:script><br /><mce:script type='text/javascript' src="/app/dwr/util.js" mce_src="app/dwr/util.js"></mce:script><br /><mce:script type='text/javascript'><!--<br />//根據年級id擷取上下學期<br />function queryterm()<br />{<br /> //var gradeid=$("selectgrade").value;<br /> var gradeid=window.document.getElementById("selectgradea").value;<br /> //預設不選擇<br /> if(gradeid==0)<br /> {<br /> window.document.getElementById("selectterma").options.length=0;<br /> window.document.getElementById("selectsubjecta").options.length=0;<br /> }<br /> else<br /> {<br /> videocategoryservice.getChildCategoryByParentId(gradeid,termCallBack);<br /> }<br />}</p><p>function termCallBack(data)<br />{<br /> window.document.getElementById("selectterma").options.length=0;</p><p> for(var i=0;i< data.length;i ++){<br /> alert(data.length);<br /> alert(typeof(data[i]));<br /> var value =videocategoryservice.ConvertVideoCategory(data[i]).getCategoryId();<br /> var text=videocategoryservice.ConvertVideoCategory(data[i]).getName();<br /> var option = new Option(text, value);<br /> //根據每組value和text標記的值建立一個option對象<br /> try{<br /> window.document.getElementById("selectterma").options.add(option);//將option對象添加到第二個下拉框中<br /> }catch(e){<br /> }<br /> }</p><p> var termid = window.document.getElementById("selectterma").value;<br /> alert(termid);<br /> videocategoryservice.getChildCategoryByParentId(termid,subjectCallBack);</p><p>}</p><p>function querysubject()<br />{<br /> var termid = window.document.getElementById("selectterma").value;<br /> videocategoryservice.getChildCategoryByParentId(termid,subjectCallBack);<br />}</p><p>function subjectCallBack(VideoCategory)<br />{<br /> window.document.getElementById("selectsubjecta").options.length=0;<br /> for(var i=0;i< VideoCategory.length;i ++){<br /> // var value = VideoCategory[i].categoryId;<br /> //var text = VideoCategory[i].name;<br /> var option = new Option("text", "value");<br /> //根據每組value和text標記的值建立一個option對象<br /> try{<br /> window.document.getElementById("selectsubjecta").options.add(option);//將option對象添加到第三個下拉框中<br /> }catch(e){<br /> }<br /> }<br />}</p><p>function change1()<br />{<br /> queryterm();<br />}</p><p>function change2()<br />{<br />querysubject();<br />}<br />// --></mce:script><br /></head><br /><body><br /><p> </p><br /><FORM METHOD="POST" ACTION="UploadM.jsp" ENCTYPE="multipart/form-data"><br /> <table id="Table1" bgcolor="#e1efcb" border="0" cellpadding="4" cellspacing="0" style="width: 100%"><br /><tbody><br /> <tr><br /> <td bgcolor="#f7f7f7" style="width: 80px"><br /> 類目選擇:</td><br /> <td colspan="5"><br /> <%<br />List<VideoCategory> listgrade,listterm,listsubject;<br />VideoCategoryService service1=new VideoCategoryService();<br />listgrade=service1.GetVideoCategoryByLevelID(1);<br />//listterm=service1.w_getChildCategoryByParentId(Integer.parseInt(request.getParameter("selectgrade")));<br />listsubject=service1.GetVideoCategoryByLevelID(3);<br />%><br /><select name="selectgradea" style="width:120px" onchange="queryterm();"><br /><option selected="selected" value="0"><br />請選擇年級...<br /></option><br /><%<br />for(int i=0;i<listgrade.size();i++)<br />{<br />%><br /><option value="<%=listgrade.get(i).getCategoryId()%>"><%=listgrade.get(i).getName()%></option><br /><%<br />}<br />%><br /><%<br />listterm=service1.getChildCategoryByParentId(3);<br />%><br /></select><br /><select name="selectterma" style="width:120px" onchange="querysubject();"><br /><option selected="selected" value="0"><br />請選擇學期...<br /></option><br /></select><br /><select name="selectsubjecta" style="width:120px" onchange=""><br /><option selected="selected" value="0"><br />請選擇科目...<br /></option><br /></select><br /></td><br /></tr><br /></tbody><br /></table><br /></FORM><br /></body><br /></html>

 

5、運行出現錯誤

 

6、問題所在:

如何將data[i]的類型object轉換為使用者自訂的VideoCategory類型?

 

7、解決方案

(1)在dwr.xml加入:

//VideoCategory表示使用者自訂的類型

<convert converter="bean" match="com.gslsoft.model.VideoCategory" />

(2)jsp頁面直接使用

data[i].id取得其id屬性,而不要使用getid()方法來調用其屬性

--------------------------------------------

到此為止,利用dwr解決三層級聯的問題已經全部解決。

 二、實現JSP查詢

1、通過條件判斷搜尋索引鍵

alert("查詢條件為:where categoryid='"+window.document.getElementById("selectsubject1").value+"'");

2、寫java 類:

public List<VideoFile> GetAllvideoFileinfowithKey(String key,int start,int limit)

3、問題所在:

如何將搜尋索引鍵作為參數傳送給java類作為參數?

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.