多余的話就不說了,請(qǐng)看DWR的例子程序:
web.xml

?2

?3

?4

?5

?6

?7

?8

?9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

?2 ? ????{
?3 ? ???????? try
?4 ? ????????{
?5 ? ????????????builder.set(req,?resp,?getServletConfig(),?getServletContext(),?container);
?6 ? ????????????ServletLoggingOutput.setExecutionContext( this );
?7 ?
?8 ? ????????????processor.handle(req,?resp);//該方法對(duì)所有request路徑/dwr/*有效,在引用JS的時(shí)候,使用這個(gè)路徑執(zhí)行dwr生成的javascript代碼
???? finally
11 ? ????????{
12 ? ????????????builder.unset();
13 ? ????????????ServletLoggingOutput.unsetExecutionContext();
14 ? ????????}
15 ? ????}
index.html
?2 ? <! DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.1//EN"?"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >
?3 ? < html? xmlns ="http://www.w3.org/1999/xhtml" >
?4 ? < head >
?5 ? ?? < title > DWR?-?Test?Home </ title >
?6 ? ?? < script? type ='text/javascript'? src ='dwr/interface/Test.js' ></ script >
?7 ? ?? < script? type ='text/javascript'? src ='dwr/engine.js' ></ script >
?8 ? ?? < script? type ='text/javascript'? src ='dwr/util.js' ></ script >
?9 ? ?? < script >
10 ? ???? function ?init()?{
11 ? ?????? // ?This?turns?off?the?no-javascript?message
12 ? ??????document.getElementById( " start " ).style.display? = ? " none " ;
13 ? ?????? // ?This?checks?for?file:?URLs?and?loading?problems
14 ? ?????? if ?(window.DWREngine? == ? null ? || ?window.DWRUtil? == ? null )?{
15 ? ????????document.getElementById( " file " ).style.display? = ? " block " ;
16 ? ???????? return ;
17 ? ??????}
18 ? ?????? // ?DWR?setup
19 ? ??????DWREngine.setErrorHandler( function (message)?{?alert(message);?});
20 ? ??????DWREngine.setWarningHandler( function (message)?{?alert(message);?});
21 ? ??????DWRUtil.useLoadingMessage();
22 ? ?????? // ?Turn?on?the?generic?error?div
23 ? ??????$( " error " ).style.display? = ? " block " ;
24 ? ?????? // ?Load?a?message?from?the?server
25 ? ??????Test.getInsert(load);
26 ? ????}
27 ?
28 ? ???? function ?load(data)?{
29 ? ??????$( " error " ).style.display? = ? " none " ;
30 ? ??????DWRUtil.setValue( " reply " ,?data);
31 ? ??????$( " reply " ).style.display? = ? " block " ;
32 ? ????}
33 ? ?? </ script >
34 ? </ head >
35 ?
36 ? < body? onload ="init()" >
37 ?





?2 ? ????{
?3 ? ????????String?pathInfo? = ?req.getPathInfo();
?4 ? ????????String?servletPath? = ?req.getServletPath();
?5 ?
?6 ? ???????? if ?(nullPathInfoWorkaround? && ?pathInfo? == ? null )
?7 ? ????????{
?8 ? ????????????pathInfo? = ?req.getServletPath();
?9 ? ????????????servletPath? = ?HtmlConstants.PATH_ROOT;
10 ? ????????????log.debug( " Default?servlet?suspected.?pathInfo= " ? + ?pathInfo? + ? " ;?contextPath= " ? + ?req.getContextPath()? + ? " ;?servletPath= " ? + ?servletPath);? // $NON-NLS-1$? // $NON-NLS-2$? // $NON-NLS-3$
11 ? ????????}
12 ?
13 ? ???????? if ?(pathInfo? == ? null ? ||
14 ? ????????????pathInfo.length()? == ? 0 ? ||
15 ? ????????????pathInfo.equals(HtmlConstants.PATH_ROOT))
16 ? ????????{
17 ? ????????????resp.sendRedirect(req.getContextPath()? + ?servletPath? + ?HtmlConstants.FILE_INDEX);
18 ? ????????}
19 ? ???????? else ? if ?(pathInfo.startsWith(HtmlConstants.FILE_INDEX))
20 ? ????????{
21 ? ????????????index.handle(req,?resp);
22 ? ????????}
23 ? ???????? else ? if ?(pathInfo.startsWith(HtmlConstants.PATH_TEST))
24 ? ????????{
25 ? ????????????test.handle(req,?resp);
26 ? ????????}
27 ? ???????? else ? if ?(pathInfo.startsWith(HtmlConstants.PATH_INTERFACE))
28 ? ????????{
29 ? ????????????iface.handle(req,?resp);//這個(gè)方法是我們要關(guān)注的
? ??????????? }
?????。。。。。。。
? ????}
?2 ? ????{
?3 ? ????????String?pathinfo? = ?req.getPathInfo();
?4 ? ????????String?servletpath? = ?req.getServletPath();
?5 ? ???????? if ?(pathinfo? == ? null )
?6 ? ????????{
?7 ? ????????????pathinfo? = ?req.getServletPath();
?8 ? ????????????servletpath? = ?HtmlConstants.PATH_ROOT;
?9 ? ????????}
10 ? ????????String?scriptname? = ?pathinfo;
11 ? ????????scriptname? = ?LocalUtil.replace(scriptname,?HtmlConstants.PATH_INTERFACE,?HtmlConstants.BLANK);
12 ? ????????scriptname? = ?LocalUtil.replace(scriptname,?HtmlConstants.EXTENSION_JS,?HtmlConstants.BLANK);
13 ? ????????Creator?creator? = ?creatorManager.getCreator(scriptname);
14 ?
15 ? ???????? // resp.setContentType("text/javascript");
16 ? ????????PrintWriter?out? = ?resp.getWriter();
17 ? ????????out.println();
18 ?
19 ? ????????out.println( " function? " ? + ?scriptname? + ? " ()?{?} " );? // 從這里開始DWR自動(dòng)生成javascript
?????????????String???path? = ?overridePath;
22 ? ???????? if ?(path? == ? null )
23 ? ????????{
24 ? ????????????path? = ?req.getContextPath(
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
