(1) DWR and SSH Framework Integration Tutorial DWR Framework Introduction.
DWR (Direct Web Remoting) is a remote server-side AJAX open-source framework for improving the interaction of WEB pages with Java classes, which can help developers develop Web sites that include Ajax technology. It can allow The code in the browser uses the Java function that runs on the Web server as if it were in the browser. It contains two main sections: allows JavaScript to fetch data from a servlet on the Web server that adheres to the Ajax principles. On the other hand, a JavaScript library can help site developers easily use the data they get to dynamically change the content of the page. DWR takes a new Ajax-like approach to dynamically generate JavaScript code based on Java classes. So that Web developers can use Java code in JavaScript as if they were the local code of the browser (client code); But Java code runs on the Web server side and has free access to the Web server's resources. For security reasons, Web developers must properly configure which Java classes can be safely used externally.
This remote functional approach from Java to JavaScript brings a much more traditional RPC mechanism to DWR users, like RMI or soap, and has the benefit of running on the web without the need for a browser plugin. DWR does not think the browser/web server protocol is important, And more willing to ensure that the programming interface is simple and natural. The biggest challenge is to combine the asynchronous nature of Ajax with the synchronous nature of normal Java method invocations. In asynchronous mode, the resulting data can be accessed asynchronously after a period of time after it has been called. DWR allows web developers to pass a callback function to handle the Java function call process asynchronously.
(2) The introduction of the DWR package, the version used in this example is dwr2.0.5. The Dwr.jar package is introduced into the project's Web-inf/lib directory.
, after the introduction of success, review to learn about the relevant information.
(3) Configure the information about DWR in Web. Xml. As shown below
Note: The relevant parameter descriptions have been explained in the diagram.
(4) Create a new Dwr.xml file in the same directory as Web. Xml.
The initial configuration is as follows
The configuration section has been completed, using an example to illustrate the specific configuration of DWR, using the SSH program, and changing the user login part to DWR technology.
(5) Background business logic. First write the action method of the business process
The method is named Showhello (String name,string Passpword), which adds a new user through the name and password passed in, or fail if the success is successfully returned. Specific implementation
As follows:
The method is named Showadd (User u), which adds a new user through the passed in user object and returns fail if the success is returned successfully. The implementation is as follows:
The method named Showlist () returns the user list& #60 by invoking the Getusers method of the service layer; user& #62; a collection. The implementation is as follows:
The specific method is implemented as follows:
(6) Configure the Dwr.xml file. Configure the Showhello, Showadd, showlist methods to dwr.xml so that they can be accessed. The specific configuration and explanation are as follows:
(7) Configure the DWR framework's debug function, so that it can be visualized debugging. The specific method is to set debug to True in the DWR configuration in Web. Xml.
After the setup is successful, redeploy the project and start the server. Enter localhost in the browser: Port name/project name/dwr to open the Dwr debug page, such as my project name is SSH, port number is 80, then the URL is: http://127.0.0.1:8080/combine/dwr/ index.html, the following page appears after opening:
If you see the page above stating that your DWR has been configured successfully, here is how the test method will work. Clicking on the Hello method will open the following page (section):
Contains three parts, the first part is:
It indicates the JS file location of the background method and the location of the DWR engine, the two JS files must be included on the page file for the project to normally invoke the DWR framework. The second part is optional, and the recommendation is also included on the page file.
The third part is a declarative approach, which lists all of the methods, but the red warning is not available because the return value is not a basic data type, and the DWR information is configured without declaring its access rights. Only
There is no warning message that it can be accessed normally.
(8) Debug Page test method can run normally. Find the Showhello method
, enter our user name and password (Rush 123456) in the two parameters as follows:
, click Execute to execute the method, and if the following results appear, the execution succeeds:
。 Description: The Debug page is for debugging purposes, please close its debugging function after the actual release.
(9) write the foreground implementation code.
Test_hello.html is used to test the Showhello method test_add.html used to test the Showadd method test_list.html used to test Showlist method test_hello.html build Test_ Hello.html
Add the onclick effect to the page by introducing the three JS commit button just mentioned, and execute the method for OnSave () to introduce JS
Note: username and password get the user name and password value of the controls on the Web page respectively, and two if the value is empty, if it is empty, prompt the user and terminate the commit, thus realizing the login verification. User.showHello:Hello is a background dwr configuration in the JS name, Showhello is the background business logic implementation function, this function has three parameters, username and password is a parameter, callbackmsg is a callback function, The system displays the return value in the callback function.
(10) The code work is done, and now the actual test. Redeploy the project to start the server. To open a Web page in a browser:
First fill in the password and name are 222, after submitting the data display:
Here's a look at whether the database has this data:
The data is displayed as normal. Let's go back to the page and repeat the data again.
Tip fail. Indicates that the user is prevented from repeating the data, and the Showhello () function is complete. (11) As test_hello.html operation method, we create test_add.html and test_list.html first is test_add.html because it is similar to test_hello.html function is to submit data function. The only difference with test_hello.html is that the data received are different. The test_add corresponding function is
It needs to receive a user object, so be sure to set the Convert object in Dwr.xml
The program automatically converts the data into a user object. The HTML code of the page is the same as test_hello.html, except that JS is different, JS is as follows:
Here's a quick test: http://127.0.0.1:8080/combine/test_add.html If the effect is the same, the proof function is complete.
(one) test_list.html test Showlist () method two points need to note: the first showlist () is an argument-free method, so only need to pass the callback function. The second returned data is an array of objects, using the For statement for data reading. The HTML code is as follows:
The following test function: http://127.0.0.1:8080/combine/test_list.html
Yes, it's a button: After clicking the button, the page appears as follows:
Ok, all done.
JS call Java function--DWR Framework