DWR框架作用:能返回很多东西。你把对象放置request中,页面在拿出对象时,会刷新整个页面。用DWR就只用刷新局部。Ajax能返回页面和字符串。不能返回对象?
实例展示:用DWR返回String类型。作用:获得前台输入的内容与“abc”比较,相等返回“1”否则返回“2”。类似于判断用户名是否存在。
DWR的使用步骤:
1、下载dwr的jar包,复制到web-info的lib目录下。(导入似乎无效)
2、配置web.xml
<servlet>
<servlet-name>dwr-what</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<!-- 为解决这个问题严重: A request has been denied as a potential CSRF attack 。没报这个错误的可以不加-->
<!-- 新加corssDomainSessionSecurity参数 -->
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
<!-- 新加corssDomainSessionSecurity参数 -->
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-what</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
以上配置主要是让系统加载时找到servlet,在真正部署时建议不要使用debug
3、新建drw.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<!--js中通过creatJavaScriptIndwr调用后台servlet中的函数-->
<create javascript="creatJavaScriptIndwr" creator="new">
<param name="class" value="cn.bl.control.FristDWR"></param>
<include method="doLogin"/>
</create>
</allow>
</dwr>
creator中用new创建对象,好处是dwr创建的对象生命期短,内存消耗低
把以上两个xml文件放一起(都在WEB-INF目录下),就可以开始写cn.bl.control.FristDWR
public class FristDWR {
public String doLogin(String user_name){
System.out.println("-----------"+user_name);
if("abc".equals(user_name)){
return "1";
}else{
return "2";
}
}
}
检验一下是否能运行
将工程部署到服务器,浏览器访问“....工程名/dwr”(向平时访问页面一样)出现一下页面则说明以上配置无误。点击进行测试。在小红圈中输入“abc”点击Excute 后面显示”1”,输入其他后面则显示”2”。好现在说明一切OK。
写jsp页面(以下为部分代码)
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type='text/javascript' src='/TestDWR/dwr/interface/creatJavaScriptIndwr.js'></script>
<script type='text/javascript' src='/TestDWR/dwr/engine.js'></script>
<script type='text/javascript' src='/TestDWR/dwr/util.js'></script>
<script type="text/javascript">
function checkIsExist() {
creatJavaScriptIndwr.doLogin(document.getElementById("a").value, callHaHaBack);
}
function callHaHaBack(result) {
alert(result);
if(2==result){
document.getElementById("date").innerHTML="<font color='green'>不存在</font>";
}else{
document.getElementById("date").innerHTML="<font color='red'>已存在</font>";
}
}
</script>
</head>
<body>
-------测试返回字符串----<br>
<table>
<tr>
<td>测试是否存在</td>
<td><input type="text" id="a" οnblur="checkIsExist()">
</td>
<td><span id="date"></span></td>
</tr>
<tr>
<td>酱油</td>
<td><input type="text">
</td>
</tr>
</table>
<br>----------------测试返回对象------------------
</body>
</html>
测试:在输入框中输入内容后,输入框失去焦点时到后台验证输入的内容是否为“abc”
实例展示:用DWR返回String类型。作用:获得前台输入的内容与“abc”比较,相等返回“1”否则返回“2”。类似于判断用户名是否存在。
DWR的使用步骤:
1、下载dwr的jar包,复制到web-info的lib目录下。(导入似乎无效)
2、配置web.xml
<servlet>
<servlet-name>dwr-what</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<!-- 为解决这个问题严重: A request has been denied as a potential CSRF attack 。没报这个错误的可以不加-->
<!-- 新加corssDomainSessionSecurity参数 -->
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
<!-- 新加corssDomainSessionSecurity参数 -->
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-what</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
以上配置主要是让系统加载时找到servlet,在真正部署时建议不要使用debug
3、新建drw.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<!--js中通过creatJavaScriptIndwr调用后台servlet中的函数-->
<create javascript="creatJavaScriptIndwr" creator="new">
<param name="class" value="cn.bl.control.FristDWR"></param>
<include method="doLogin"/>
</create>
</allow>
</dwr>
creator中用new创建对象,好处是dwr创建的对象生命期短,内存消耗低
把以上两个xml文件放一起(都在WEB-INF目录下),就可以开始写cn.bl.control.FristDWR
public class FristDWR {
public String doLogin(String user_name){
System.out.println("-----------"+user_name);
if("abc".equals(user_name)){
return "1";
}else{
return "2";
}
}
}
检验一下是否能运行
将工程部署到服务器,浏览器访问“....工程名/dwr”(向平时访问页面一样)出现一下页面则说明以上配置无误。点击进行测试。在小红圈中输入“abc”点击Excute 后面显示”1”,输入其他后面则显示”2”。好现在说明一切OK。
写jsp页面(以下为部分代码)
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type='text/javascript' src='/TestDWR/dwr/interface/creatJavaScriptIndwr.js'></script>
<script type='text/javascript' src='/TestDWR/dwr/engine.js'></script>
<script type='text/javascript' src='/TestDWR/dwr/util.js'></script>
<script type="text/javascript">
function checkIsExist() {
creatJavaScriptIndwr.doLogin(document.getElementById("a").value, callHaHaBack);
}
function callHaHaBack(result) {
alert(result);
if(2==result){
document.getElementById("date").innerHTML="<font color='green'>不存在</font>";
}else{
document.getElementById("date").innerHTML="<font color='red'>已存在</font>";
}
}
</script>
</head>
<body>
-------测试返回字符串----<br>
<table>
<tr>
<td>测试是否存在</td>
<td><input type="text" id="a" οnblur="checkIsExist()">
</td>
<td><span id="date"></span></td>
</tr>
<tr>
<td>酱油</td>
<td><input type="text">
</td>
</tr>
</table>
<br>----------------测试返回对象------------------
</body>
</html>
测试:在输入框中输入内容后,输入框失去焦点时到后台验证输入的内容是否为“abc”