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)
描述了一种使用混合了HTML(或XHTML)和层叠样式表作为表达信息,
来创建交互式的Web应用的开发技术;文档对象模型(DOM),
JavaScript,动态地显示和与表达信息进行交互;并且XMLHttpRequest对象与Web服务器异步地交换和处理数据。
DWR(直接Web远程控制)项目是在Apache许可下的一个开源的解决方案,
它供给那些想要以一种简单的方式使用AJAX和 XMLHttpRequest的开发者。
它具有一套Javascript功能集,它们把从HTML页面调用应用服务器上的Java对象的方法简化了。
它操控不同类型的参数,并同时保持了HTML代码的可读性,它可以与spring结合在一起。
举个非常实用的小例子,检测是否有重复的用户名
1。web。xml的配置(包括spring和dwr)
<?
xml version="1.0" encoding="ISO-8859-1"
?>
<! DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.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 > DWR Servlet </ display-name >
< description > Direct Web Remoter Servlet </ 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 >
<! DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.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 > DWR Servlet </ display-name >
< description > Direct Web Remoter Servlet </ 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的配置
<?
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 ="spring"
javascript ="UserManager" >
< param name ="beanName" value ="UserManager" />
</ create >
</ allow>
</ dwr >
<! DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd" >
< dwr >
< allow >
< create
creator ="spring"
javascript ="UserManager" >
< param name ="beanName" value ="UserManager" />
</ create >
</ allow>
</ dwr >
3。applicationContext.xml文件
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="propertyConfigurer" class ="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
< property name ="location" >
< value > /WEB-INF/jdbc.properties </ value >
</ property >
</ bean >
<!-- Local DataSource that works in any environment -->
< bean id ="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource" >
< property name ="driverClassName" >
< value > ${jdbc.driverClassName} </ value >
</ property >
< property name ="url" >
< value > ${jdbc.url} </ value >
</ property >
< property name ="username" >
< value > ${jdbc.username} </ value >
</ property >
< property name ="password" >
< value > ${jdbc.password} </ value >
</ property >
</ bean >
<!-- OrderManager -->
< bean id ="UserManager" class ="dwr.manager.UserManager" >
< property name ="dataSource" >
< ref local ="dataSource" />
</ property >
</ bean >
</ beans >
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="propertyConfigurer" class ="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
< property name ="location" >
< value > /WEB-INF/jdbc.properties </ value >
</ property >
</ bean >
<!-- Local DataSource that works in any environment -->
< bean id ="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource" >
< property name ="driverClassName" >
< value > ${jdbc.driverClassName} </ value >
</ property >
< property name ="url" >
< value > ${jdbc.url} </ value >
</ property >
< property name ="username" >
< value > ${jdbc.username} </ value >
</ property >
< property name ="password" >
< value > ${jdbc.password} </ value >
</ property >
</ bean >
<!-- OrderManager -->
< bean id ="UserManager" class ="dwr.manager.UserManager" >
< property name ="dataSource" >
< ref local ="dataSource" />
</ property >
</ bean >
</ beans >
4。。spring的bean
package
dwr.manager;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class UserManager extends JdbcDaoSupport ... {
private final Log log = LogFactory.getLog(getClass().getName());
public boolean getUser(String username)
...{
String sql="select count(*) from user where userName=? ";
int i=getJdbcTemplate().queryForInt(sql,new Object[]...{username});
if(i>0)
return true;
else
return false;
}
}
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class UserManager extends JdbcDaoSupport ... {
private final Log log = LogFactory.getLog(getClass().getName());
public boolean getUser(String username)
...{
String sql="select count(*) from user where userName=? ";
int i=getJdbcTemplate().queryForInt(sql,new Object[]...{username});
if(i>0)
return true;
else
return false;
}
}
5。jsp的代码
<%
...
@ page contentType="text/html;charset=GBK" language="java"
%>
< html >
< head >< title > Simple jsp page </ title ></ head >
< script type ='text/javascript' src ='<%=request.getContextPath()% > ... /dwr/interface/UserManager.js'>
</script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'>
</script>
<script type='text/javascript'
src='<%=request.getContextPath()%>/dwr/util.js'></script>
<script type="text/javascript">
function showResult(b) {
if (b) {
alert("已经有重复的登录名了!");
} else {
alert("登录名尚未注册过!");
}
}function checkRepeat(s) {
var o = getElement(s);
if (o) {
UserManager.getUser(o.value, showResult);
}
}
function getElement(name) {
var result = null;
var objs = document.getElementsByName(name);
if (objs) {
var o = objs[0];
result = o;
}
return result;
}
</script>
<body>
<input type="text" name="user" >
<input type="button" onclick="checkRepeat('user')" value="检查是否重复">
</body>
</html>
6 classpath加入dwr和spring的jar文件,注意必须有hibernate3.jar和bsf.jar,在IE和FireFox下,就可以用了。
其中参考了magicgod's blog( http://www.matrix.org.cn/blog/magicgod/archives/week_2005_09_04.html#001419)
< html >
< head >< title > Simple jsp page </ title ></ head >
< script type ='text/javascript' src ='<%=request.getContextPath()% > ... /dwr/interface/UserManager.js'>
</script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'>
</script>
<script type='text/javascript'
src='<%=request.getContextPath()%>/dwr/util.js'></script>
<script type="text/javascript">
function showResult(b) {
if (b) {
alert("已经有重复的登录名了!");
} else {
alert("登录名尚未注册过!");
}
}function checkRepeat(s) {
var o = getElement(s);
if (o) {
UserManager.getUser(o.value, showResult);
}
}
function getElement(name) {
var result = null;
var objs = document.getElementsByName(name);
if (objs) {
var o = objs[0];
result = o;
}
return result;
}
</script>
<body>
<input type="text" name="user" >
<input type="button" onclick="checkRepeat('user')" value="检查是否重复">
</body>
</html>
6 classpath加入dwr和spring的jar文件,注意必须有hibernate3.jar和bsf.jar,在IE和FireFox下,就可以用了。
其中参考了magicgod's blog( http://www.matrix.org.cn/blog/magicgod/archives/week_2005_09_04.html#001419)