日期:2014-05-16  浏览次数:20494 次

DWR中:Java对象与JS对象互换
一 DWR配置
1、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
 <display-name>ajaxDWR</display-name>  
     <servlet>    
        <servlet-name>dwr-invoker</servlet-name>    
        <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>    
                  <init-param>      
                        <description></description>      
                        <param-name>debug</param-name>      
                        <param-value>true</param-value>  
                   </init-param>  
    </servlet>  
    <servlet-mapping>    
             <servlet-name>dwr-invoker</servlet-name>    
              <url-pattern>/dwr/*</url-pattern>  
     </servlet-mapping>
</web-app>


2、dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd"> 
<dwr>  
<allow>    
   <create creator="new" javascript="ServiceArea">      
       <param name="class" value="onlyfun.caterpillar.Service" />    
   </create> 
   <create creator="new" javascript="Office">      
       <param name="class" value="onlyfun.caterpillar.Office" />    
   </create>  
   
   <!--可以将js对象转换为Java-->
   <convert converter="bean" match="onlyfun.caterpillar.Person" />
</allow>
</dwr>



/**
  *Java对象转换为JS对象,用JS调用Java的方法
  *
  */
function change(){
	var objSelect=document.getElementById('mySelect');
	var index = objSelect.selectedIndex;
 
	if(index >= 0 ) {  
	 ServiceArea.getOptions(index,result);
	}
}

function result(list){
	DWRUtil.removeAllOptions("city");
	DWRUtil.addOptions("city", list);
}

/**
  *Java对象转换为JS对象,用JS调用Java的方法
  *
  */

function fun(){
	var person = {id:2,name:"emily",age:22};
	alert(person);
	Office.setPerson(person);
}


<body> 
     城市: <select id="mySelect" onchange="change()">
            <option id=0 value="0">please select</option> 
            <option id=1 value=1>成都</option>
            <option id=2 value=2>重庆</option> 
        </select> 
     区域: <select id="city">
            <option>please select left</option>
        </select>



public class Service {
    public Object[] getOptions(int id){
    	List<String> options = new ArrayList<String>();
    	if (id == 0) {
    		options.add("please select left");
    	}
    	else if(id == 1) {
    	 options.add("锦江区");
    	 options.add("武侯区");
    	 options.add("金牛区");
    	} 
    	else if(id == 2){
    		 options.add("渝中区");
        	 options.add("渝北区");
        	 options.add("江北区");
    	}
    	
    	return options.toArray();
    }
       
}


public class Person {
	private int id;
	private String name;
	private int age;

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getAge() {
		return age;
	}

}


public class Office {
  private Person person;

public Person getPerson() {
	  return person;
}

public void setPerson(Person person) {
	this.person = person;
	if(person != null ){
		System.out.println("person.name="+person.getName());
		System.out.println("person.id="+