dwr入門代碼一
系統(tǒng)
2106 0
WEB-INF目錄下創(chuàng)建一個(gè)dwr.xml文件
dwr.xml 配置代碼:
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>????
<!-- without allow, DWR isn't allowed to do anything -->????
<allow>????????
<!--creator設(shè)定為new,表示使用Hello的無參數(shù)建構(gòu)子來生成物件,javascript設(shè)定為Hello,表示客戶端JavaScript程式可以使用Hello來呼叫對(duì)應(yīng)的onlyfun.caterpillar.Hello物件-->????
????<create creator="new" javascript="Hello" scope="application">??????????
????????<param name="class" value="com.xzj.service.HelloWorldService"/>??????????
????</create>????
</allow>????
</dwr>??
web.xml中的配置如下:
Xml代碼
<?xml version="1.0" encoding="UTF-8"?>????
<web-app xmlns="
http://java.sun.com/xml/ns/j2ee
" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance
" version="2.4"
xsi:schemaLocation="
http://java.sun.com/xml/ns/j2ee
?? <A href="
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
"
target=_blank>http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd</A>">????
??<!-- DWR配置開始 -->????
?? <servlet>????
????<servlet-name>dwr-invoker</servlet-name>????
????<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-invoker</servlet-name>????
????<url-pattern>/dwr/*</url-pattern>????
??</servlet-mapping>????????
??<!-- DWR配置結(jié)束 -->????
????????
??<welcome-file-list>????
????<welcome-file>index.jsp</welcome-file>????
??</welcome-file-list>????
</web-app>????
然后我們?cè)趕rc目錄下創(chuàng)建一個(gè)com.xzj.service包在次包中創(chuàng)建一個(gè)類名為:HelloWorldService
在這個(gè)包中寫兩個(gè)方法
在這個(gè)包中寫兩個(gè)方法
代碼如下:
Java代碼
package com.xzj.service;??????
????
public class HelloWorldService {??????
????private static String msg="請(qǐng)輸入姓名!";??????
??????????
????public String helloWorld(){??????
????????return "Hello DWR World!";??????
????}??????
??????????
????public String hello(String name){
????????
???? if("".equals(name)||null==name){
????????return msg;
????}
???? if(!"".equals(name)||null==name){
??????????msg="Hello"+name+" 先生";
???? }
??????return msg;
????}
}????
然后我們回到前臺(tái)index.jsp頁面中 代碼如下:
Java代碼
<%@ page language="java"??pageEncoding="UTF-8"%>??????
<html>??????
??<head>??????
????<title>DWR Hello World!</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">??
<!--dwr/interface/Hello.js是由DWRServlet根據(jù)dwr.xml中的設(shè)定生成的,engine.js負(fù)責(zé)客戶端伺服端溝通,util.js是一些好用的JavaScript程式,可以讓您少寫很多JavaScript。-->????
???????? <!--下面三個(gè)必須要第一個(gè)和你dwr.xml配置的javascript="Hello"一樣-->??????
????<script type="text/javascript" src='dwr/interface/Hello.js'></script>??????
?????? <script type="text/javascript" src='dwr/engine.js'></script>??????
?????? <script type="text/javascript" src='dwr/util.js'></script>??????
????<script type="text/javascript">??????
????????function helloWorld(){??????
????????????Hello.helloWorld(showMessage);??????
????????????function showMessage(msg){??????
????????????????alert(msg);??????
????????????}??????
????????}??????
??????????????
????????function hello(){??
??????????// var name = $('txtName').value;
??????????//Hello.hello(name,showMessage());????
????????????Hello.hello(txtName.value,showMessage);??????
????????????function showMessage(msg){??????
????????????????alert(msg);??????
????????????}??????
????????}??????
????</script>??????
??</head>??????
??<body>??????
????<center>??????
????????<input type="button" name="btnHello" value="HelloWord" onclick="helloWorld()"/>??????
????????<br><br><br><br>??????
????????Please Enter You Name:<input type="text" name="name" id="txtName"/>??????
????????<input type="button" name="btnHello" value="HelloWord" onclick="hello()"/>??????
????</center>??????
??</body>??????
</html>????
另外版本需要的是5.5的,6.0的配置變了
dwr入門代碼一
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元