当前位置: 代码迷 >> JavaScript >> DWR兑现级联 & JSP页面的查询
  详细解决方案

DWR兑现级联 & JSP页面的查询

热度:119   发布时间:2012-11-23 00:03:43.0
DWR实现级联 & JSP页面的查询
Problem:

(1) 利用DWR实现三层级联:函数实现,但是无法将js中的object对象转换为用户自定义的对象VideoCateroy

(2)实现JSP页面的查询操作:如何将JS返回的str用于SQL语句

一、利用DWR实现三层级联

1、预期实现结构分析

七年级

---上学期

-------语文

-------数学

-------英语

---下学期

-------语文

-------数学

-------英语

2、编写Java类:getChildCategoryByParentId(int id)

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


本文来自CSDN博客,转载请标明出处:file:///C:/Documents%20and%20Settings/Administrator/桌面/我的文件夹/我下载的学习网页/JSP的那些事儿(4)----DWR实现级联%20%26%20JSP页面的查询%20-%20薛敬明的专栏%20-%20CSDN博客.htm


3、编写WEB-INF/dwr.xml:具体的配置说明在前面一章给出了详细的说明
<?xml version="1.0" encoding="GBK"?>     
<dwr>     
    <allow>       
    <create creator="new" javascript="videocategoryservice">  
      <param name="class" value="com.gslsoft.service.VideoCategoryService" />  
    </create>        
  </allow>    
</dwr> 

4、编写Test.jsp页面
view plaincopy to clipboardprint?
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>  
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>  
<%@ page import="java.util.*" %>  
<%@ page import="com.gslsoft.service.*" %>  
<%@ page import="com.gslsoft.model.*" %>  
<html>  
<head>  
<title>文件上传</title>  
<link rel="stylesheet" href="../../css/x_css1.css" mce_href="css/x_css1.css" type="text/css">  
<mce:script type='text/javascript' src="/app/dwr/interface/videocategoryservice.js" mce_src="app/dwr/interface/videocategoryservice.js"></mce:script>  
<mce:script type='text/javascript' src="/app/dwr/interface/VideoCategory.js" mce_src="app/dwr/interface/VideoCategory.js"></mce:script>  
<mce:script type='text/javascript' src="/app/dwr/engine.js" mce_src="app/dwr/engine.js"></mce:script>  
<mce:script type='text/javascript' src="/app/dwr/util.js" mce_src="app/dwr/util.js"></mce:script>  
<mce:script type='text/javascript'><!--   
//根据年级id获取上下学期   
function queryterm()   
{      
   //var gradeid=$("selectgrade").value;   
   var gradeid=window.document.getElementById("selectgradea").value;      
   //默认不选择      
   if(gradeid==0)   
   {   
     window.document.getElementById("selectterma").options.length=0;   
     window.document.getElementById("selectsubjecta").options.length=0;        
   }   
   else   
   {   
     videocategoryservice.getChildCategoryByParentId(gradeid,termCallBack);        
   }   
}   
  
function termCallBack(data)   
{   
  window.document.getElementById("selectterma").options.length=0;   
     
  for(var i=0;i< data.length;i ++){   
    alert(data.length);   
    alert(typeof(data[i]));   
    var value =videocategoryservice.ConvertVideoCategory(data[i]).getCategoryId();   
    var text=videocategoryservice.ConvertVideoCategory(data[i]).getName();       
    var option = new Option(text, value);   
    //根据每组value和text标记的值创建一个option对象   
    try{   
      window.document.getElementById("selectterma").options.add(option);//将option对象添加到第二个下拉框中   
    }catch(e){       
    }   
  }   
     
  var termid = window.document.getElementById("selectterma").value;   
  alert(termid);   
  videocategoryservice.getChildCategoryByParentId(termid,subjectCallBack);   
     
}   
  
function querysubject()   
{   
  var termid =  window.document.getElementById("selectterma").value;    
  videocategoryservice.getChildCategoryByParentId(termid,subjectCallBack);   
}   
  
function subjectCallBack(VideoCategory)   
{   
  window.document.getElementById("selectsubjecta").options.length=0;   
  for(var i=0;i< VideoCategory.length;i ++){   
       // var value = VideoCategory[i].categoryId;   
        //var text = VideoCategory[i].name;   
        var option = new Option("text", "value");   
        //根据每组value和text标记的值创建一个option对象   
        try{   
          window.document.getElementById("selectsubjecta").options.add(option);//将option对象添加到第三个下拉框中   
        }catch(e){   
        }   
      }     
}   
  
function change1()   
{   
    queryterm();   
}   
       
function change2()   
{   
    querysubject();   
}   
// --></mce:script>  
</head>  
<body>  
<p> </p>  
<FORM METHOD="POST" ACTION="UploadM.jsp" ENCTYPE="multipart/form-data">  
 <table id="Table1" bgcolor="#e1efcb" border="0" cellpadding="4" cellspacing="0" style="width: 100%">  
<tbody>  
       <tr>  
          <td bgcolor="#f7f7f7" style="width: 80px">  
           类目选择:</td>  
          <td colspan="5">  
          <%   
List<VideoCategory> listgrade,listterm,listsubject;   
VideoCategoryService service1=new VideoCategoryService();   
listgrade=service1.GetVideoCategoryByLevelID(1);   
//listterm=service1.w_getChildCategoryByParentId(Integer.parseInt(request.getParameter("selectgrade")));   
listsubject=service1.GetVideoCategoryByLevelID(3);   
%>  
<select name="selectgradea" style="width:120px" onchange="queryterm();">  
<option selected="selected" value="0">  
请选择年级...   
</option>  
<%   
for(int i=0;i<listgrade.size();i++)   
{      
%>          
<option value="<%=listgrade.get(i).getCategoryId()%>"><%=listgrade.get(i).getName()%></option>  
<%   
}   
%>  
<%   
listterm=service1.getChildCategoryByParentId(3);   
%>          
</select>  
<select name="selectterma" style="width:120px" onchange="querysubject();">    
<option selected="selected" value="0">  
请选择学期...   
</option>        
</select>  
<select name="selectsubjecta" style="width:120px"  onchange="">    
<option selected="selected" value="0">  
请选择科目...   
</option>    
</select>  
</td>  
</tr>          
</tbody>                   
</table>  
</FORM>  
</body>  
</html>  


本文来自CSDN博客,转载请标明出处:file:///C:/Documents%20and%20Settings/Administrator/桌面/我的文件夹/我下载的学习网页/JSP的那些事儿(4)----DWR实现级联%20%26%20JSP页面的查询%20-%20薛敬明的专栏%20-%20CSDN博客.htm

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()方法来调用其属性
  相关解决方案