关闭

ajax开发步骤

标签: ajaximportdwrstringbeanclass
2068人阅读 评论(1) 收藏 举报
分类:

1、修改web.xml,加入以下代码:
 <servlet>
  <servlet-name>dwr-progress-invoker</servlet-name>
  <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
  <init-param>
   <param-name>debug</param-name>
   <param-value>true</param-value>
  </init-param>
  <init-param>
   <param-name>logLevel</param-name>
   <param-value>DEBUG</param-value>
  </init-param>
  <init-param>
   <param-name>config-sysmgr</param-name>
   <param-value>WEB-INF/configs/progress/dwr.xml</param-value>
  </init-param>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>dwr-progress-invoker</servlet-name>
  <url-pattern>/dwr/progress/*</url-pattern>
 </servlet-mapping>
 
2、在工程的路径下增加dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
  <init>
  </init>
  <allow>
    <create creator="new" javascript="SaveSecPlanAjax">
      <param name="class" value="com.spsoft.progress.secondplan.web.ajax.SaveSecPlanAjax"/>
    </create>
  </allow>
</dwr>

3、增加ajax要调用的类
public class SaveSecPlanAjax extends BaseAjax{
 public String saveSecPlan(final String taskName) {
  return (String) getAjaxTemplate().execute(new AjaxCallback() {  
   public Object executeInAjax() {    
    SecPlanService service = (SecPlanService)Services.GetService(SecPlanService.SERVICE_NAME);
    System.out.println(taskName);
    return "success";
   } 
  });
 }
}

以下是要用到的基类:
a、BaseService.java
package com.spsoft.framework.service;
public interface BaseService {

}

b、BaseAjax.java
package com.spsoft.framework.ajax;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import uk.ltd.getahead.dwr.WebContextFactory;
import com.spsoft.framework.service.BaseService;

public class BaseAjax {
 private AjaxTemplate template;
 public AjaxTemplate getAjaxTemplate(){
  if (template==null){
   template = new AjaxTemplate();
  }
  return template;
 }
 
 public ServletContext getServletContext(){
  return WebContextFactory.get().getServletContext();
 }
 
 public ApplicationContext getContext() {
  return WebApplicationContextUtils.getWebApplicationContext(getServletContext());
 }

 public BaseService getService(HttpServletRequest request,String serviceName) {
  return (BaseService)getContext().getBean(serviceName);
 }

}

4、在页面上调用
   a、增加js的声明
  <script src="<%=request.getContextPath()%>/dwr/engine.js"></script>
    <script src="<%=request.getContextPath()%>/dwr/util.js"></script>
   <script src="<%=baseUrl%>/dwr/progress/interface/SaveSecPlanAjax.js"></script>
  
  b、增加调用的方法
  function saveSecPlanInfo(taskName){
     SaveSecPlanAjax.saveSecPlan(taskName);
  } 

ajax返回java对象

1, dwr:在对应的dwr.xml里面增加定义增加如下对象的定义指向即可
 <convert converter="bean" match="com.spsoft.test.HelloBean"></convert>
2,
 HelloBean:对象的属性,包括其get set方法
 id,name....
3,
 ajax:可以返回数组或者单对象
 public HelloBean[] getHelloBean
4,
 js:回调方法如下:

              for (var i=0;i<data.length;i++)
              {
                var bean = {};
                bean=data[i];
               
               
                alert(bean.id);
                alert(bean.name);
              }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:221928次
    • 积分:2568
    • 等级:
    • 排名:第14758名
    • 原创:57篇
    • 转载:17篇
    • 译文:0篇
    • 评论:43条
    文章分类
    最新评论
    eclipse插件开发