关于DWR

今天突然要用到DWR实现局部刷新,可是一想,原来做过可是现在一点印象都没故写此篇(一个简单的例子):
首先导入dwr.jar包,再写一个dwr.xml文件跟web.xml文件同级目录:(虽然dwr.xml文件没有像dtd或者shema这样的规范,但是有个潜在的规范)格式如下:
<?xml version="1.0" encoding="UTF-8"?>
<dwr>
	<allow>
		<!-- 生成一个class对象. -->
		<create creator="new" javascript="personInfo" class="com.dao.PersonInfoDao">
		</create>
	</allow>
</dwr>

dwr.xml文件配置完成后:配置web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
		
	<!-- dwr包 -->
	 <servlet>
		<servlet-name>aa</servlet-name>
		<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
		<init-param>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
	</servlet>
    <servlet-mapping>
		<servlet-name>aa</servlet-name>
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

上述的类如下:
public class PersonInfoDao {
	public String getUsername(String username){
		if("true".equals(username)){
			return "true";
		}		
		return "false";
	}
}

页面如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>index.html</title>
    <script type="text/javascript" src="/DWRAjax/dwr/engine.js"></script>
    <script type="text/javascript" src="/DWRAjax/dwr/util.js"></script>
    <script type="text/javascript" src="/DWRAjax/dwr/interface/personInfo.js"></script>
    <script type="text/javascript">
    	function check(){
        	var user = document.getElementById('username').value;
    		//personInfo.getUsername(user,responseMethod);
    		personInfo.getUsername(user,responseMethod);
    	}
    	function responseMethod(val){
			alert(val);
    	}
    </script>
  </head>
  
  <body>
    <input type="text" id="username" />
    <input type="button" value="button" onclick="check();"/>
    
  </body>
</html>

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