dwr 简单应用

1.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'dwr.jsp' starting page</title>
     <script type="text/javascript" src="/dwr/interface/ShowMap.js"></script>
     <script type="text/javascript" src="/dwr/engine.js"></script>
     <script type="text/javascript" src="/dwr/util.js"></script> 
     <script>
     window.onload=function(){
       ShowMap.getMap(function(data){
        DWRUtil.removeAllOptions("map");//清空
       // var array=['-请选择-'];
       // DWRUtil.addOptions('map',array);//数组形式的值也为:-请选择-
        DWRUtil.addOptions('map',{'':'-请选择-'});
     	DWRUtil.addOptions('map',data);//Map模式[加在请选择后面]
     	});
     }
    function showValue(){
  	   alert($('map').value);
    }
    function showMapValue(){
  	   ShowMap.getMap(function(map){
        alert(map['1']);//返回text值[由key得value]
     	});
    }
     </script>
  </head>
  <body>
  <center>
  <select id="map" name="map">
  <option value="">请选择...</option>
  </select>
      <hr width="100%" size="1" noshade style="border:1px dashed #cccccc;"/> 
       <input type="submit" value="查看select值" onclick="showValue()"/>
       <hr width="100%" size="1" noshade style="border:1px dashed #cccccc;"/> 
       <input type="submit" value="查看map值" onclick="showMapValue()"/>
  </center>
  </body>
</html>


2.java代码
package com.dwr;

import java.util.HashMap;
import java.util.Map;

public class ShowMap {
	
	public HashMap getMap(){
		HashMap map=new HashMap();
		map.put(1, "aaaa");
		map.put(2, "bbbb");
		map.put(3, "cccc");
		return map;
	}

}


3.*.xml配置
<!--web.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<servlet>
     <servlet-name>dwr-invoker</servlet-name>
     <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
      	<init-param>
         <param-name>classes</param-name>
        <param-value>java.lang.Object</param-value>
     </init-param>
      	<init-param>
			<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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
<!--dwr.xml-->
<?xml version="1.0" encoding="UTF-8"?>
  <dwr>
     <allow>	 
       <create creator="new" javascript="ShowMap">
        	<param name="class" value="com.dwr.ShowMap"/>
       </create>
     </allow>	  
  </dwr>


4.注意引入两个jar包:dwr.jar、commons-logging.jar

你可能感兴趣的:(java,xml,jsp,servlet,DWR)