本文共 4775 字,大约阅读时间需要 15 分钟。
dwr全称Direct Web Remoting原理将java代码转化成js对象,实现跨域访问
web.xml
DWR (Direct Web Remoting) A Simple Demo DWR org.directwebremoting.servlet.DwrListener dwr-invoker org.directwebremoting.servlet.DwrServlet debug true 1 dwr-invoker /dwr/*
dwr.xml
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 Listlist() { List users = new ArrayList (); 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"%>Insert title here
本文出自 “” 博客,请务必保留此出处
转载地址:http://onmhx.baihongyu.com/