dwr-Ajax开源框架
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dwr-Ajax开源框架相关的知识,希望对你有一定的参考价值。
dwr全称Direct Web Remoting原理将java代码转化成js对象,实现跨域访问
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>DWR (Direct Web Remoting)</display-name> <description>A Simple Demo DWR</description> <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> </web-app>
dwr.xml
<?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>
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; } }
package org.konghao.dwr.model; public class MyException extends RuntimeException { public MyException() { super(); // TODO Auto-generated constructor stub } public MyException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } public MyException(String message) { super(message); // TODO Auto-generated constructor stub } public MyException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } }
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; } public void setUsername(String username) { this.username = username; } public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } public User(int id, String username, Group group) { super(); this.id = id; this.username = username; this.group = group; } public User() { super(); } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", group=" + group + "]"; } }
package org.konghao.dwr.model; public class Group { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Group(int id, String name) { super(); this.id = id; this.name = name; } public Group() { super(); } @Override public String toString() { return "Group [id=" + id + ", name=" + name + "]"; } }
无刷新上传文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript" src=\‘#\‘" /dwr/engine.js"></script> <script type="text/javascript" src=\‘#\‘" /dwr/util.js"></script> <script type="text/javascript" src=\‘#\‘" /dwr/interface/MyDwr.js"></script> <script type="text/javascript"> function upload() { var file = dwr.util.getValue("myfile"); alert(file.value); alert(file); MyDwr.upload(file,file.value,function(data){ alert(data); }); } </script> </head> <body> <input type="file" id="myfile"/> <input type="button" value="上传文件" onclick="upload()"/> </body> </html>
本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1752923
以上是关于dwr-Ajax开源框架的主要内容,如果未能解决你的问题,请参考以下文章
text 来自Codyhouse框架的Browserlist片段源代码