java中的dwr框架怎么配置啊?主要用于什么地方啊?
3个回答
展开全部
配置很简单~主要用于直接调用java函数~!!!
首先需要用到dwr.jar
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">
<display-name>test</display-name>
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>org.directwebremoting.servlet.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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
dwr.xml
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
"http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<allow>
<create javascript="UserDAOAjax" creator="new">
<param name="class" value="org.thj.UserDAOAjax"></param>
</create>
<create javascript="MyProjectDao" creator="new">
<param name="class" value="org.thj.MyProjectDao"></param>
</create>
<convert match="org.thj.User" converter="bean"></convert>
</allow>
</dwr>
MyProjectDao.java
package org.thj;
import java.util.HashMap;
import java.util.Map;
public class MyProjectDao {
public String getName(String str){
String returnStr="";
returnStr="((("+str+"))))MyProjectDao.getName->return String";
return returnStr;
}
public Map getMap(){
Map<String,String> map=new HashMap<String,String>();
map.put("1", "v1");
map.put("2", "v2");
map.put("3", "v3");
return map;
}
}
my.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="dwr/engine.js"></script><%--固定写法--%>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/MyProjectDao.js"></script><%--由框架自动生成,文件名根据DWR文件而来--%>
<title>测试</title>
<script type="text/javascript">
function validate()
{
var name = form1.username.value;
MyProjectDao.getName("黄东辉",callBack);
//UserDAOAjax.exitUser(name,show);<%--exitUser()函数的返回值会赋给show()函数--%>
}
function callBack(data){
alert("the return value is " + data);
}
function getMap(){
MyProjectDao.getMap(getMapCallBack);
}
function getMapCallBack(data){
for(var property in data){
alert("property:"+property);
alert(property+":"+data[property]);
}
}
</script>
</head>
<body onload="validate()">
<form action="#" name="form1">
<table id="table1" border="0">
<tr><td> 用户名:<input type="text" name="username"/></td></tr>
<tr><td> 密码:<input type="text" name="password"/> </td></tr>
<tr><td> <input type="button" value="点击" onclick="getMap();" /> </td></tr>
</table>
</form>
</body>
</html>
首先需要用到dwr.jar
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">
<display-name>test</display-name>
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>org.directwebremoting.servlet.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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
dwr.xml
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
"http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<allow>
<create javascript="UserDAOAjax" creator="new">
<param name="class" value="org.thj.UserDAOAjax"></param>
</create>
<create javascript="MyProjectDao" creator="new">
<param name="class" value="org.thj.MyProjectDao"></param>
</create>
<convert match="org.thj.User" converter="bean"></convert>
</allow>
</dwr>
MyProjectDao.java
package org.thj;
import java.util.HashMap;
import java.util.Map;
public class MyProjectDao {
public String getName(String str){
String returnStr="";
returnStr="((("+str+"))))MyProjectDao.getName->return String";
return returnStr;
}
public Map getMap(){
Map<String,String> map=new HashMap<String,String>();
map.put("1", "v1");
map.put("2", "v2");
map.put("3", "v3");
return map;
}
}
my.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="dwr/engine.js"></script><%--固定写法--%>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/MyProjectDao.js"></script><%--由框架自动生成,文件名根据DWR文件而来--%>
<title>测试</title>
<script type="text/javascript">
function validate()
{
var name = form1.username.value;
MyProjectDao.getName("黄东辉",callBack);
//UserDAOAjax.exitUser(name,show);<%--exitUser()函数的返回值会赋给show()函数--%>
}
function callBack(data){
alert("the return value is " + data);
}
function getMap(){
MyProjectDao.getMap(getMapCallBack);
}
function getMapCallBack(data){
for(var property in data){
alert("property:"+property);
alert(property+":"+data[property]);
}
}
</script>
</head>
<body onload="validate()">
<form action="#" name="form1">
<table id="table1" border="0">
<tr><td> 用户名:<input type="text" name="username"/></td></tr>
<tr><td> 密码:<input type="text" name="password"/> </td></tr>
<tr><td> <input type="button" value="点击" onclick="getMap();" /> </td></tr>
</table>
</form>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询