1.引入DWR包
2.配置web.xml文件
<!-- DWR配置 -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<!-- DWR配置结束 -->
3.WEB-INFO下新建dwr.
xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<!-- 测试DWR配置开始 -->
<convert converter="bean" match="com.estone.www.bjkpcg.manage.ztest.Per"></convert>
<create creator="new" javascript="service">
<param name="class" value="com.estone.www.bjkpcg.manage.ztest.HelloWorld"/>
</create>
<!-- 测试DWR配置结束 -->
<!--批次实体配置-->
<convert converter="bean" match="com.estone.www.bjkpcg.manage.project.vo.TblCgBatch"></convert>
<convert converter="bean" match="com.estone.www.bjkpcg.manage.production.vo.TblCgProductionType"></convert>
<!--项目类别配置-->
<create creator="new" javascript="dwrMethod">
<param name="class" value="com.estone.www.bjkpcg.util.DwrMethod"/>
</create>
<!--项目类型实体配置-->
<convert converter="bean" match="com.estone.www.bjkpcg.manage.project.vo.TblCgProjectType"></convert>
<!--项目信息添加配置-->
<create creator="new" javascript="projectDwr">
<param name="class" value="com.estone.www.bjkpcg.network.project.dwr.ProjectDwr"/>
</create>
</allow>
</dwr>
4.Dwr类
public class Dwr{
//获取上下文关系
private WebContext getWebContext() {
return WebContextFactory.get();
}
//获取sesion
public HttpSession getSession() {
return getWebContext().getSession();
}
//获取Request
public HttpServletRequest getHttpServletRequest() {
return getWebContext().getHttpServletRequest();
}
//获取Response
public HttpServletResponse getHttpServletResponse() {
return getWebContext().getHttpServletResponse();
}
//获取sesion id
public String getSessionId(){
return getSession().getId();
}
//写入sesion
public void setSession(String name,Object objct){
getSession().setAttribute(name, objct);
}
//写入清除sesion
public void deleteSession(String name){
getSession().removeAttribute(name);
}
//获取 getApplicationContext
public ApplicationContext getApplicationContext(){
return WebApplicationContextUtils.getWebApplicationContext(getWebContext().getServletContext());
}
//获取 spring 注入
public Object getBean(String key){
return getApplicationContext().getBean(key);
}
}
5.DwrMethod类
public class DwrMethod extends Dwr{
UtilService utilService = (UtilService) super.getBean("utilService");
public String isNotRep(String code) throws Exception{
if(code!=null && !"".equals(code)){
return this.utilService.isNotRep("from TblCgDistrict t where t.isDele=0 and t.code="+code);
}else{
return "2";
}
}
public List getBatch(String yearid) throws Exception{
if(yearid!=null && !"".equals(yearid)){
String []str = yearid.split(",");
return this.utilService.getHqlQuery("from TblCgBatch t where t.isDele=0 and t.fkYear.id="+str[0]);
}else{
return null;
}
}
/**
* 成果类别级联查询
*/
public List getProductionType(String parentId) throws Exception{
if(parentId!=null && !"".equals(parentId)){
return this.utilService.getHqlQuery("from TblCgProductionType p where p.isDele=0 and p.parentId="+Integer.parseInt(parentId));
}else{
return null;
}
}
}
6.页面:
<script type='text/javascript' src='/bjkpcg/dwr/interface/dwrMethod.js'></script>
<script type='text/javascript' src='/bjkpcg/dwr/engine.js'></script>
<script type='text/javascript' src='/bjkpcg/dwr/util.js'></script>
<script language="javascript">
function getNext(t){
var parentId="";
if(t==1){
parentId = document.getElementById("bselect1").value;
if(parentId!=""){
dwrMethod.getProductionType(parentId,callback1);
}
}else{
parentId = document.getElementById("btype2").value;
if(parentId!=""){
dwrMethod.getProductionType(parentId,callback2);
}else{
document.getElementById("btype3").options.length=0;
var option = new Option("-- 请选择 --", "");
document.getElementById("btype3").add(option);
}
}
}
function callback1(provinces){
document.getElementById("btype2").options.length=0;
document.getElementById("btype3").options.length=0;
var option = new Option("-- 请选择 --", "");
var option1 = new Option("-- 请选择 --", "");
document.getElementById("btype2").add(option);
document.getElementById("btype3").add(option1);
var pid = document.getElementById("bselect1").value;
for(var i=0;i< provinces.length;i ++){
var value = provinces[i].id;
var text = provinces[i].productionTypeName;
var option = new Option(text, value);
<%-- if(provinces[i].id==pid){--%>
<%-- option.selected=true;--%>
<%-- }--%>
try{
document.getElementById("btype2").add(option);
}catch(e){
}
}
}
function callback2(provinces){
document.getElementById("btype3").options.length=0;
var option = new Option("-- 请选择 --", "");
document.getElementById("btype3").add(option);
var pid = document.getElementById("btype2").value;
for(var i=0;i< provinces.length;i ++){
var value = provinces[i].id;
var text = provinces[i].productionTypeName;
var option = new Option(text, value);
if(provinces[i].id==pid){
option.selected=true;
}
try{
document.getElementById("btype3").add(option);
}catch(e){
}
}
}
</script>
<tr>
<td class="add_tit">成果类型</td>
<td>
<s:select id="bselect1" name="pruType1" onchange="getNext(1)" list="pTypeList" listKey="id" listValue="productionTypeName" headerKey="" headerValue="-- 请选择 --" />
<label class="note4">*</label>
</td>
</tr>
<tr>
<td class="add_tit">二级分类</td>
<td>
<select id="btype2" name="pruType2" onchange="getNext(2)" style="width:155px;">
<option value="">-- 请选择 --</option>
</select>
</td>
</tr>
<tr>
<td class="add_tit">三级分类</td>
<td>
<select id="btype3" name="pruType3" style="width:155px;">
<option value="">-- 请选择 --</option>
</select>
</td>
</tr>