使用 DWR 的示例程序

发布于 2024-11-18 23:45:55 字数 444 浏览 5 评论 0原文

我正在尝试编写一个带有直接网络远程处理的示例程序。访问网站 http://directwebremoting.org/ 有一些基本问题。请帮助我

尝试该程序 http://directwebremoting.org/dwr-demo/simple /text.html

  1. 在哪里保存 html 和 javascript 代码以及用什么名称???
  2. 他们说要包含一些 2 个脚本(engine.js 和 util.js)。我应该在哪一页中包含这两个。
  3. 他们也给了一个java代码,现在保存在哪里???

I am trying to write a sample program with direct web remoting. went through the site http://directwebremoting.org/ have some basic questions. please help me

what to try the program http://directwebremoting.org/dwr-demo/simple/text.html

  1. where to save the html and javascript code and with what name???
  2. they say to include some 2 scripts (engine.js and util.js). in which page i should include these two.
  3. they have given a java code also, where to save this now???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

翻身的咸鱼 2024-11-25 23:45:55

要开始使用 DWR,您必须在以下位置创建 dwr.xml 文件:

/WEB-INF/dwr.xml

添加 dwr.jar 文件到

/WEB-INF/lib/dwr.jar

如果未创建 lib 文件夹,则创建它。

您必须知道,DWR 使用 servlet 类来发挥作用,因此您必须声明它们必须如何工作。您必须修改 web.xml 文件以告诉 DWR 它是一个 servlet。

    ...
    <servlet>
      <display-name>DWR Servlet</display-name>
      <servlet-name>dwr-invoker</servlet-name>
      <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
      <init-param>
        <param-name>jsonpEnabled</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 配置,以检查

    http://yourapp:port/dwr

yourapp 是否是您的应用程序的名称,port 是否是分配的端口号。

现在您必须创建一个与 DWR 和 JSP 页面交互的类控制器。像这样的

com.dwr.test.MyController

     public class MyController {
          public String doSomething() {
              return "example" ;
          }
     }

你必须告诉 DWR 这是一个控制器,所以在你的 dwr.xml 文件中你必须写

    <create creator="new" javascript="mycontroller">
        <param name="class" value="com.dwr.test.MyController "/>
     </create>

检查
javascript="mycontroller"
这就是您从 JSP 页面调用 java 类控制器的方式

现在您已经与 App 控制器和 DWR 建立了控制器连接。

稍后,您必须告诉 JSP 页面使用什么控制器。

我以index.jsp为例,

    <html>
    <head>
    <script type='text/javascript' src='/webbitacora/dwr/util.js'></script>
    <script type='text/javascript' src='/webbitacora/dwr/engine.js'></script>
    <script type='text/javascript' src='/webbitacora/dwr/interface/mycontroller.js'></script>
    ...

这个东西是如何工作的?两个脚本行(util an engine)对于DWR至关重要,这是DWR工作的主要场景。
第三行是您最近在 drw.xml 文件中声明的控制器的名称。您不需要创建该文件,DWR 负责创建该文件。

现在您可以将您的方法与任何 HTML 控制器、javascript 函数或其他任何东西一起使用,

    mycontroler.doSomething({
        callback : function (data){
            alert(data) ;
        } 
    });

以了解更多方法、传递参数参数我建议您访问 DWR 的网页

http://directwebremoting.org/dwr/index.html

To start with DWR you have to create your dwr.xml file at

/WEB-INF/dwr.xml

add dwr.jar file to

/WEB-INF/lib/dwr.jar

if lib folder is not created then create it.

And how you must kwno, DWR uses servlet classs to be functional, so you have to declare them how must work. You have to modify your web.xml file to tell DWR that it is a servlet.

    ...
    <servlet>
      <display-name>DWR Servlet</display-name>
      <servlet-name>dwr-invoker</servlet-name>
      <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
      <init-param>
        <param-name>jsonpEnabled</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>
   ...

You can test you DWR configuration going to

    http://yourapp:port/dwr

check that yourapp is the name of your app and port is the number of the assigned port.

Well now you have to create a class controller that interactues with DWR and your JSP pages. Something like this

com.dwr.test.MyController

     public class MyController {
          public String doSomething() {
              return "example" ;
          }
     }

you have to tell DWR that this is a controller, so in your dwr.xml file you have to write

    <create creator="new" javascript="mycontroller">
        <param name="class" value="com.dwr.test.MyController "/>
     </create>

Check that
javascript="mycontroller"
is how you are going to call your java class controller from your JSP page

Now you have a controller connection with your App controller and DWR.

Later, you have to tell to your JSP page what controller use.

I have index.jsp as an example

    <html>
    <head>
    <script type='text/javascript' src='/webbitacora/dwr/util.js'></script>
    <script type='text/javascript' src='/webbitacora/dwr/engine.js'></script>
    <script type='text/javascript' src='/webbitacora/dwr/interface/mycontroller.js'></script>
    ...

how this stuff works ? the two script lines (util an engine) are vital for DWR, this is the main scenario that DWR uses to work.
And the third line is the name name of your Controller that you recently declared in your drw.xml file. You don't need to create this files, DWR is responsible of the creation of this files.

now you can use your method with any HTML controller, javascript function or anything else

    mycontroler.doSomething({
        callback : function (data){
            alert(data) ;
        } 
    });

to know more methods, passing parameters arguments I recommend you visit DWR's web page

http://directwebremoting.org/dwr/index.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文