温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

dwr-Ajax开源框架

发布时间:2020-07-04 14:47:40 来源:网络 阅读:457 作者:jethai 栏目:开发技术


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="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/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>


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI