DWR的安装使用和与spring的整合
1.安装配置
1.1拷贝dwr文件包到lib目录下
1.2编辑web.xml文件,加入如下代码:
[html]
<listener>
<listener-class>org.directwebremoting.servlet.DwrListener</listener-class>
</listener>
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
1.3拷贝dwr.xml文件到web.xml文件的目录下:
[html]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">
<dwr>
<allow>
<!--
<filter class="com.example.dwr.monitor.MonitoringAjaxFilter"/>
<filter class="org.directwebremoting.filter.ExtraLatencyAjaxFilter">
<param name="delay" value="200"/>
</filter>
-->
<!-- intro - for the test on index.html -->
<create creator="new">
<param name="class" value="org.konghao.dwr.model.MyDwr"/>
</create>
<convert converter="bean" match="org.konghao.dwr.model.User"/>
<convert converter="bean" match="org.konghao.dwr.model.Group"/>
<convert match="java.lang.Exception" converter="exception"/>
<convert converter="bean" match="java.lang.StackTraceElement"/>
</allow>
</dwr>
1.4在需要使用的页面中引入js文件(第二个是自己定义的java文件的名字)
[html]
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/inte易做图ce/MyDwr.js"></script>
2.dwr的应用(以下几个类是下面的一些操作需要用到的测试类,可以自行修改)
MyDwr.java
[java]
package org.konghao.dwr.model;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
public class MyDwr {
public String hello(String world) {
System.out.println("hello "+world);
return "hello "+world;
}
public User load() {
User u = new User(1,"张三",new Group(1,"财务处"));
return u;
}
public List<User> list() {
List<User> users = new ArrayList<User>();
users.add(new User(1,"张三",new Group(1,"财务处")));
users.add(new User(2,"李四",new Group(2,"科技处")));
users.add(new User(3,"王五",new Group(3,"宣传部")));
return users;
}
public void add(User user) {
System.out.println(user);
}
public void deleteUser() {
throw new MyException("在删除用户的时候有错");
}
public int add(int a,int b) {
return a+b;
}
public String upload(InputStream is,String filename) throws IOException {
//WebContext可以获取HttpServlet的对象
WebContext wc = WebContextFactory.get();
HttpServletRequest req = wc.getHttpServletRequest();
String realpath = req.getSession().getServletContext().getRealPath("upload");
String fn = FilenameUtils.getName(filename);
String filepath = realpath+"/"+fn;
FileUtils.copyInputStreamToFile(is, new File(filepath));
return filepath;
}
}
User.java
[java]
package org.konghao.dwr.model;
public class User {
private int id;
private String username;
private Group group;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username; &nbs
补充:Web开发 , 其他 ,