what is dwr in java?
Description Direct Web Remoting, or DWR, is a Java open-source library that helps developers write web sites that include Ajax technology. It allows code in a web browser to use Java functions running on a web server as if those functions were within the browser. Wikipedia
As demonstrated in the Raw Ajax section, using the XmlHttpRequest directly for creating asynchronous requests is cumbersome. Not only is it verbose in the JavaScript code, you must consider the server-side work needed to route your Ajax requests to the appropriate service, and marshal the result back to the browser.
DWR was invented to handle all the plumbing required to wire up your Web page to your backend services. It is a Java framework you can easily plug into your Web applications to allow your JavaScript code to call services on the server. It even integrates directly with the Spring Framework to allow you to expose your beans directly to the Web client.
What's really neat about DWR is that once you configure the services you want to expose to the client, it uses reflection to generate JavaScript objects that your Web page can use to access your service. Your Web page then simply interfaces with the generated JavaScript objects as if they were using the service directly; DWR seamlessly takes care of all of the dirty details of Ajax and request routing.
Let's dissect our example code to clarify how this works.
The first thing to notice about the application is that it is a standard Java application using the Layered Architecture design pattern. The fact you will use DWR to expose some of your services via JavaScript doesn't affect your design.
Figure 2.
Here is a simple Java service that we will expose directly to our JavaScript code using the DWR framework:
This is an incredibly simplified example, as you only have one Article that can be voted on. This service is managed by Spring under the bean name ajaxSampleSvc, and depends on the ArticleDao for its persistence needs. See applicationContext.xml for details.
To configure DWR to expose this service as a JavaScript object, you add the dwr.xml file to the WEB-INF directory:
The dwr.xml file tells DWR what services you want to expose directly to your JavaScript code. Notice that you have asked to expose the ajaxSampleSvc Spring bean. DWR will automatically find the Spring ApplicationContext set up by your application. To do this, it requires you to use the standard ContextLoaderListener servlet filter to initialize your Spring ApplicationContext.
DWR is set up as a servlet, and so you add its definition to your web.xml:
After this is complete, you can load http://localhost:7001/ajax-demo/dwr to see firsthand what services are available. Doing so shows the following:
Figure 3. The available services
Clicking the ajaxSampleSvc link takes you to a sample implementation of how you could use the service directly within your HTML page. Two included JavaScript files do the majority of the work:
The ajaxSampleSvc.js is generated dynamically:
Now you can go back and refactor your raw-ajax.html file by replacing all the XmlHttpRequest code with this ajaxSampleSvc JavaScript object. The result of this change can be seen in the dwr-ajax.html file; following are the new JavaScript functions:
Amazingly simple, isn't it? The Article domain object returned by the ajaxSampleSvc object is serialized into a JavaScript object, allowing you to call methods on it such as numberOfVotes() and voteAverage(). You use this data within the HTML code that you dynamically generate and insert into the "votes" DIV element.
In a follow-up article, I will extend the conversation on Ajax to include these concepts:
DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and call each other as simply as possible. DWR is Easy Ajax for Java.
Website http://directwebremoting.org Forum https://discourse.dojo.io/c/dwr-users
Old mailing lists Mailman archive: http://mail.dojotoolkit.org/mailman/listinfo/dwr-users http://mail.dojotoolkit.org/mailman/listinfo/dwr-dev
Old mailing lists Nabble archive: http://dwr.2114559.n2.nabble.com/DWR-Users-f2114559.html http://dwr.2114559.n2.nabble.com/DWR-Dev-f5409447.html
DWR is primarily built using ant. The important ant targets are:
The DWR build system can be adapted to create outputs containing different modules (see Terminology) so it is simple to create a dwr.jar file that contains exactly the modules that you need. See the 'modules' property and the execution of the 'build' macrodef.
The ant outputs are stored in dwr/target/ant.
Module: DWR is made up of a number of modules. The idea is that these modules are as independent as possible. Spring integration is an example of a module. The modules are grouped into four areas: core, protocol, serverside, ui
Each module may contain a number of source trees. One for test, one for demos, one for the main sources and maybe one for generated code. The module may also contain a web directory to be merged into an output war file.
- Add the DWR JAR file – .
- Add the Commons Logging JAR file.
- Add the DWR servlet and mapping to the deployment descriptor web.
- Add the DWR configuration file (dwr.
- Add JavaScript file.
DWR is a Java library which allows JavaScript functions to call Java methods and Java methods to call JavaScript functions(reverse ajax). It dynamically generates the JavaScript library of service class or servlet class to allow web browsers to call java code.
DWR has two main parts:
It provides integration with servlets and popular frameworks such as Spring, Hibernate, Struts and others. It is open source.
Having a basic understanding of DWR, let us move to the code area where we will build web applications using spring boot and DWR.
Following are steps for configuring DWR in spring boot project
2. Add spring.xml to the resource folder to configure dwr scanning.
3. Configure spring boot startup main class to load spring.xml
4. If the project is based on servlets then dwr.xml is used for configuration but in case of spring boot we need to add configuration with java code.
5. In the final step we have to create a service class which will be used for writing business logic.
6. Final step integration with html
Final result is below:-
To know more about dwr, Kindly go to this link DWR — Easy Ajax for JAVA.
You can find the full code base at my github repo.
GitHub
References:-
DWR — Easy Ajax for JAVA
DWR article
More Questions
- How to diagnose yourself with diabetes?
- What is the best name for a wi-fi connection?
- What is the difference between mph and km/h?
- How to tell if walmart has something in stock?
- What is data treatment?
- What is feed in digital marketing?
- Amazon rds stands for?
- When swallowing the glottis covers the epiglottis?
- What is the best b&b norfolk broads?
- What does dlj stand for?