关闭

DWR使用步骤

294人阅读 评论(0) 收藏 举报
 DWR使用步骤;
1.导入dwr.jar包,在web.xml文件里面配制如下:

              <servlet>
              <servlet-name>dwr</servlet-name>
              <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
              <init-param>
                     <param-name>debug</param-name>
                     <param-value>true</param-value>
              </init-param>
       </servlet>
       <servlet-mapping>
              <servlet-name>dwr</servlet-name>
              <url-pattern>/dwr/*</url-pattern>
       </servlet-mapping>
2.写java文件如下:
package com.softeem.dwr;
 
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.softeem.config.DataSourceFactory;
import com.softeem.dao.BookInfoDao;
import com.softeem.dto.CityDto;
 
public class GetInfo {
       Map<String,String> map = new HashMap<String,String>();
      
       public Map getCities(String id){
              Connection conn = null;
              try {
                     conn = DataSourceFactory.getBasicDataSource().getConnection();
                     BookInfoDao dao = new BookInfoDao(conn);
                     List<CityDto> list = dao.findCity(Integer.parseInt(id));
                     for(int i = 0;i < list.size();i++){
                            map.put(new Integer(list.get(i).getId()).toString(),list.get(i).getCity());
                     }
              } catch (SQLException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
              return map;
       }
      
       public CityDto getCityDto(){
              CityDto dto = new CityDto();
              dto.setId(2);
              dto.setCity("hanwan");
              return dto;
       }
 
}
 
 
 
package com.softeem.dwr;
 
public class Service {
       public String sayHello(String name){
              return "hello " + name;
       }
}
      
3.增加配制文件dwr.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<dwr>
       <allow>
              <create creator="new" javascript="service">
                     <param name="class" value="com.softeem.dwr.Service" />
              </create>
              <create creator="new" javascript="getInfo">
                     <param name="class" value="com.softeem.dwr.GetInfo" />
              </create>
              <convert converter="bean" match="com.softeem.dto.CityDto">
                     <param name="include" value="city,id" />
              </convert>
       </allow>
</dwr>
 
4.在jsp文件里面加入代码往下:
 
 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5284次
    • 积分:135
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:8篇
    • 译文:0篇
    • 评论:0条