tangxiong0 2008-05-18
AJAX,或者说是异步JavaScript和XML,
描述了一种使用混合了HTML(或XHTML)和层叠样式表作为表达信息,
来创建交互式的Web应用的开发技术;文档对象模型(DOM),
JavaScript,动态地显示和与表达信息进行交互;并且XMLHttpRequest对象与Web服务器异步地交换和处理数据。
DWR(直接Web远程控制)项目是在Apache许可下的一个开源的解决方案,
它供给那些想要以一种简单的方式使用AJAX和XMLHttpRequest的开发者。
它具有一套Javascript功能集,它们把从HTML页面调用应用服务器上的Java对象的方法简化了。
它操控不同类型的参数,并同时保持了HTML代码的可读性,它可以与spring结合在一起。
举个非常实用的小例子,检测是否有重复的用户名
1。web.xml的配置(包括spring和dwr)<?xmlversion="1.0"encoding="ISO-8859-1"?>
<!DOCTYPEweb-app
PUBLIC"-//SunMicrosystems,Inc.//DTDWebApplication2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<context-param><param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/main-servlet.xml</param-value>
</context-param>
<listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener><servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>dwr</servlet-name>
<display-name>DWRServlet</display-name>
<description>DirectWebRemoterServlet</description>
<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>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>2.dwr.xml的配置<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEdwrPUBLIC"-//GetAheadLimited//DTDDirectWebRemoting1.0//EN""http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<allow>
<create
creator="spring"
javascript="UserManager">
<paramname="beanName"value="UserManager"/>
</create>
</allow>
</dwr>
3。applicationContext.xml文件
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<beanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><propertyname="location">
<value>/WEB-INF/jdbc.properties</value>
</property>
</bean>
<!--LocalDataSourcethatworksinanyenvironment-->
<beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<propertyname="url">
<value>${jdbc.url}</value>
</property>
<propertyname="username">
<value>${jdbc.username}</value>
</property>
<propertyname="password">
<value>${jdbc.password}</value>
</property>
</bean>
<!--OrderManager-->
<beanid="UserManager"class="dwr.manager.UserManager">
<propertyname="dataSource">
<reflocal="dataSource"/>
</property>
</bean></beans>
4。spring的bean
packagedwr.manager;importorg.springframework.jdbc.core.support.JdbcDaoSupport;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;publicclassUserManagerextendsJdbcDaoSupport...{
privatefinalLoglog=LogFactory.getLog(getClass().getName());
publicbooleangetUser(Stringusername)
{
Stringsql="selectcount(*)fromuserwhereuserName=?";
inti=getJdbcTemplate().queryForInt(sql,newObject[]...{username});
if(i>0)
returntrue;
else
returnfalse;
}}5。jsp的代码<%...@pageclanguage="java"%>
<html>
<head><title>Simplejsppage</title></head>
<scripttype='text/javascript'src='<%=request.getContextPath()%>.../dwr/interface/UserManager.js'>
</script>
<scripttype='text/javascript'src='<%=request.getContextPath()%>/dwr/engine.js'>
</script>
<scripttype='text/javascript'
src='<%=request.getContextPath()%>/dwr/util.js'></script>
<scripttype="text/javascript">
functionshowResult(b){
if(b){
alert("已经有重复的登录名了!");
}else{
alert("登录名尚未注册过!");
}
}functioncheckRepeat(s){
varo=getElement(s);
if(o){
UserManager.getUser(o.value,showResult);
}
}
functiongetElement(name){
varresult=null;
varobjs=document.getElementsByName(name);
if(objs){
varo=objs[0];
result=o;
}
returnresult;
}
</script>
<body>
<inputtype="text"name="user">
<inputtype="button"COLOR:#000000;BACKGROUND-COLOR:#f5f5f5">'user')"value="检查是否重复">
</body>
</html>
6classpath加入dwr和spring的jar文件,注意必须有hibernate3.jar和bsf.jar,在IE和FireFox下,就可以用了。