Dwr级联的小例子(一)

2014-11-24 03:29:12 · 作者: · 浏览: 2

1.引入DWR包

2.配置web.xml文件
[html]


dwr-invoker
org.directwebremoting.servlet.DwrServlet

debug
true


crossDomainSessionSecurity
false



dwr-invoker
/dwr/*

3.WEB-INFO下新建dwr.
xml
[html]
< xml version="1.0" encoding="UTF-8" >
http://www.getahead.ltd.uk/dwr/dwr20.dtd">





























4.Dwr类

[java]
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类
[java]
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.页面:
[html]
<script type='text/java script' src='/bjkpcg/dwr/interface/dwrMethod.js'>
<script type='text/java script' src='/bjkpcg/dwr/engine.js'>
<script type='text/java script' src='/bjkpcg/dwr/util.js'>

<script language="java script">
function getNext(t){
var parentId="";
if(t==1){
parentId = document.getElementById("bsel