DWR使用步驟
DWR使用步驟;
1.導入dwr.jar包,在web.xml文件裏面配製如下:
<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文件裏面加入代碼往下:
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.