dwr前后台交互

阅读更多
  在网上找了很多dwr的例子。不是很齐全,补充如下:

需要的jar是dwr.jar。180kb的。不是最新版本。

dwr.xml和web.xml是同级目录

1.index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
%>



	
		
		My JSP 'index.jsp' starting page
		
		
		
		
		
		

		
		
		

		

	

	
		

Ajax _DWR





2.web.xml





	
		dwr-invoker
		uk.ltd.getahead.dwr.DWRServlet
		
			debug
			true
		
	
	
		dwr-invoker
		/dwr/*
	

	
		index.jsp
	





3.dwr.xml


   


	
		
			
		
		
		
			
		

		
			
			
			
			
			
		
		
	




4.Class.java


package com.dwr;

public class Class
{
    /**
     * 从前台传递参数到后台方法调用
     * @param par
     * @return
     */
    public boolean getSomething(String par)
    {
        System.out.println("par == " + par);
        String par2 = "b";
        System.out.println("par2 == " + par2);
        if (par.equals("a")&&par2.equals("b"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    /**
     * 从前台传递对象到后台
     * @param 
     * @return
     */
    
    public boolean getSomethingObject(ObjectValues obj)
    {
        System.out.println("aString = " + obj.getaString());
        System.out.println("bInt = " + obj.getbInt());
        System.out.println("ObjectV =  " + obj.getObjectV().getString());
        
        if (obj.getaString().equals("x"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }  
}



5.ObjectValues.java


package com.dwr;

public class ObjectValues
{
    /**
     * 前台传递到后台的对象包装
     */
    private String aString;
    private int bInt;

    private ObjectV objectV;

    public String getaString()
    {
        return aString;
    }

    public void setaString(String aString)
    {
        this.aString = aString;
    }

    public int getbInt()
    {
        return bInt;
    }

    public void setbInt(int bInt)
    {
        this.bInt = bInt;
    }

    public ObjectV getObjectV()
    {
        return objectV;
    }

    public void setObjectV(ObjectV objectV)
    {
        this.objectV = objectV;
    }

}


6.ObjectV.java


package com.dwr;

import java.util.List;

public class ObjectV
{
    private String list;
    private String string;

    public String getList()
    {
        return list;
    }

    public void setList(String list)
    {
        this.list = list;
    }

    public String getString()
    {
        return string;
    }

    public void setString(String string)
    {
        this.string = string;
    }

}







  • dwr.jar (179.3 KB)
  • 描述: dwr.jar 180KB 左右的
  • 下载次数: 5
  • 运行截图.zip (34.1 KB)
  • 下载次数: 23

你可能感兴趣的:(dwr,json,Ajax,JavaScript)