【转】 dwr使用步骤 自 lijunwyf

dwr使用步骤

1. dwr.jar包直接铐到WebRoot/WEB-INF/lib包下

2. 设置DWR的使用是简单的:将DWR的jar文件拷入Web应用的WEB-INF/lib目录中,在web.xml中增加一个servlet声明,并创建DWR的配置文件。DWR的分发中需要使用一个单独的jar文件。你必须将DWR servlet加到应用的WEB-INF/web.xml中布署描述段中去。将以下代码:

<servlet>

<servlet-name>dwr-invoker</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-invoker</servlet-name>

<url-pattern>/dwr/*</url-pattern>

</servlet-mapping>

或者是

<servlet>
<servlet-name>dwr-invoker</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>

添加到<web-app>

3.WEB-INF下建一个dwr.xml文件,拷贝文件

<!DOCTYPE dwr PUBLIC

"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"

"http://getahead.org/dwr/dwr20.dtd">

<dwr>

<allow>

<create creator="new" javascript="JDate">

<param name="class" value="java.util.Date"/>

</create>

<create creator="new" javascript="Demo">

<param name="class" value="your.java.Bean"/>

</create>

</allow>

</dwr>

或者

<dwr>
<allow>

<create creator="spring" javascript="SysUserService">
<param name="beanName" value="sysUserService" />
</create>
<!-- 验证行政区域是否重复添加 -->
<create creator="spring" javascript="TsDistrictAction">
<param name="beanName" value="/tsDistrictAction"></param>
</create>

<!-- 更新理化性质版本 -->
<create creator="spring"
javascript="DangerCargoEditionAction">
<param name="beanName" value="/dangerCargoEditionAction"></param>
</create>
<!-- 填写委托申报单中选择船舶信息 -->
<create creator="spring" javascript="dockClientApplyAction">
<param name="beanName" value="/dockClientApplyAction"></param>
</create>
<!-- 验证危险货物委托单 -->
<create creator="spring" javascript="DockClientApplyService">
<param name="beanName" value="dockClientApplyService" />
</create>
<!-- 验证危险货物港口作业申报单 -->
<create creator="spring" javascript="DockApplyService">
<param name="beanName" value="dockApplyService" />
</create>
<!-- 验证危险货物是否在作业范围之内(企业) -->
<create creator="spring" javascript="DockApplyDetailService">
<param name="beanName" value="dockApplyDetailService"></param>
</create>
<!-- 验证危险货物是否在作业范围之内(作业委托人) -->
<create creator="spring"
javascript="DockClientApplyDetailService">
<param name="beanName"
value="dockClientApplyDetailService">
</param>
</create>
<!-- 根据有效期时间自动填充有效期结束时间 -->
<create creator="spring" javascript="DangBaleTaskCert">
<param name="beanName" value="dangBaleTaskCertService"></param>
</create>
<!-- 验证表号是否已存在 -->
<create creator="spring" javascript="EmergencyService">
<param name="beanName" value="emergencyService"></param>
</create>
<!-- 验证船舶是否已存在 -->
<create creator="spring" javascript="ShipAjax">
<param name="beanName" value="shipDao"></param>
</create>
<!-- 验证车牌是否已存在 -->
<create creator="spring" javascript="CarnoAjax">
<param name="beanName" value="chinaRoadTransportPaperDao"></param>
</create>
<!-- 自动匹配表号 -->
<create creator="spring" javascript="NationalCargoService">
<param name="beanName" value="nationalCargoService"></param>
</create>
<!-- 行政管理部门 -->
<create creator="spring" javascript="AdminDepartmentService">
<param name="beanName" value="adminDepartmentService"></param>
</create>
<!-- 港口作业范围 -->
<create creator="spring" javascript="PortInfoService">
<param name="beanName" value="portInfoService"></param>
</create>
<!-- 港口企业 -->
<create creator="spring" javascript="PortEntService">
<param name="beanName" value="portEntService"></param>//小写的portEntService是在action中引用的service
</create>
<!-- 港口经营许可证 -->
<create creator="spring" javascript="PortLicenceService">
<param name="beanName" value="portLicenceService"></param>
</create>
</allow>
</dwr>

说明:每个dwr代理的一个java类都需放在<allow></allow>中。

<create creator="new" javascript="Demo">javascript=是为在JSP中所引用的Java类起的一个名字,通常和类名相同。create=代表是一个什么类型的javanew代表一个普通类型。 <param name="class" value="your.java.Bean"/>class表示Demo是一个普通的类,value表示这个类存放的路径(包名.类名)

4. 建一个类,写方法

package com.comit.dock.service;

import com.comit.dock.dao.PortEntDao;

/**
* 业务逻辑层:处理业务逻辑关系
*/
public class PortEntService {
PortEntDao portEntDao;

PortEntPropDao portEntPropDao;

public PortEntDao getPortEntDao() {
return portEntDao;
}

public void setPortEntDao(PortEntDao portEntDao) {
this.portEntDao = portEntDao;
}

{

/***
* 判断企业是否存在
* @param dockName
* @return
* @throws Exception
*/
public boolean isExistPortEnt(String dockName) throws Exception
{
boolean flag=false;
StringBuffer sql=new StringBuffer();
sql.append("select * from GDBASEDATA.PORT_ENT_PROP where PE_NAME='"+dockName+"'");
List list = portEntDao.getListBySql(sql.toString(), "");
if(list.size()>0)
{
flag=true;
}
return flag;
}

}

5. 测试运行.http://localhost:8080/工程名/dwr

6. JSP页面中添加代码:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>

<script type='text/javascript' src='<%=basePath%>dwr/interface/PortEntService.js'></script>//这是所引用类生成的JS,用时要根据情况而改变
<script type='text/javascript' src='<%=basePath%>dwr/engine.js'></script>
<script type='text/javascript' src='<%=basePath%>dwr/util.js'></script>

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<script type="text/javascript">
function submitForm()
{
var portEntName=document.getElementById("peName");

//PortEntService是dwr.xml配置的,isExistPortEnt是后台业务逻辑方法

PortEntService.isExistPortEnt(portEntName.value,licenseNumCallback); }

function licenseNumCallback(msg) //msg是后台业务方法的返回值
{
if(msg==true)
{
alert("该港口企业已经存在");
return;
}
else
{
var i = 1;
var portEntName = eval("document.getElementById('peName')");
if(portEntName.value==null || Trim(portEntName.value)=="")
{
alert("请输入企业名称(中文)!");
i=0;
return ;
}
if(i==1)
{
if(window.confirm("确定保存吗?"))
{
document.getElementById('portEntForm').action='portEntAction.do?method=saveAction&isAdd=${isAdd}';
document.getElementById('portEntForm').submit();
}
}
}
</script>

<body>

<tr>
<td align="center">
<br>
<input name="Submit" id="sub" type="button"
style="${saveButtonType}" onclick="submitForm();"
class="buttonSave" value="">
<input name="del" type="button"
onclick="if(window.confirm('确定删除吗?')){document.forms[0].action='portEntAction.do? method=deleteAction';document.forms[0].submit();}"
class="buttonDel" value="" style="${delButtonType}">
<input name="back" type="button" onclick="goBack()"
class="buttonGoBack" value="">
</td>
</tr>

</body>

</html>


你可能感兴趣的:(DWR)