Recently I learned advanced ext usage and found that direct, one of the new features in ext 3. X, looks good. Search online and find that directjngine has good support for ext direct. Therefore, directjngine000012.16.1.3.zip is downloaded from the official website, which is a relatively new project package.
Build the first directjngine demo step by step based on directjngine_user_guide.
Step 1: Configure directjngine servlet in Web. xml. The configured web. XML is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <web-app version = "2.5" <br/> xmlns = "http://java.sun.com/xml/ns/javaee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/javaee <br/> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <br/> <! -- The following is the default configuration of directjngine servlet --> <br/> <servlet-Name> djnservlet </servlet-Name> <br/> <servlet-class> COM. softwarementors. extjs. djn. servlet. directjngineservlet </servlet-class> </P> <p> <init-param> <br/> <param-Name> debug </param-Name> <br/> <param-value> true </param-value> <br/> </init-param> </P> <p> <init-param> <br/> <param- name> providersurl </param-Name> <br/> <param-value> djn/direct Provider </param-value> <br/> </init-param> <br/> <! -- Directjngine servlet default configuration --> </P> <p> <! -- API domain: corresponds to the prefix of each param-name below, you can set multiple domains --> <br/> <init-param> <br/> <param-Name> APIs </param-Name> <br/> <Param -value> <br/> mynamespace <br/> </param-value> <br/> </init-param> </P> <p> <! -- Mynamespace corresponds to the above API domain. Myaction corresponds to the generated JS folder. After the server runs, it will store the automatically generated three JS files under myaction. The names here are <br/> myactionapi. JS, MyActionApi-debug.js, MyActionApi-min.js <br/> --> <br/> <init-param> <br/> <param-Name> mynamespace. apifile </param-Name> <br/> <param-value> myaction/myactionapi. JS </param-value> <br/> </init-param> <br/> <! -- Mynamespace. corresponds to the above field. "Ext. zhouyang" is the value corresponding to the namespace. It will be used during page loading. --> <br/> <init-param> <br/> <param-Name> mynamespace. apinamespace </param-Name> <br/> <param-value> Ext. zhouyang </param-value> <br/> </init-param> <br/> <! -- Mynamespace. corresponds to the above domain. the full path name of the class to be used --> <br/> <init-param> <br/> <param-Name> mynamespace. classes </param-Name> <br/> <param-value> <br/> COM. softwarementors. extjs. djn. myaction. myaction <br/> </param-value> <br/> </init-param> </P> <p> <load-on-startup> 1 </load- on-startup> <br/> </servlet> </P> <p> <servlet-mapping> <br/> <servlet-Name> djnservlet </servlet-Name> <br/> <URL-pattern>/djn/directprovider/* </url-pattern> <br/> </servlet-mapping> </P> <p> <welcome -File-List> <br/> <welcome-File> <br/> index.html <br/> </welcome-File> <br/> </welcome-file-list> <br/> </Web-app>
I have loaded the Web. xml file.
Step 2: Make your server method visible to Javascript, that is, to be visible to the client.
Taking my demoas as an example, I want to call the server-side method in hello.html. Therefore, I add the following paragraph in hello.html.
<! -- The following is the basic JS file of directjngine, which can be directly referenced under the djn directory. --> <Br/> <SCRIPT type = "text/JavaScript" src = ".. /djn/djn-remote-call-support.js "> </SCRIPT> <br/> <SCRIPT type =" text/JavaScript "src = ".. /EJN/ejn-assert.js "> </SCRIPT> <br/> <! -- The above is the basic JS file of directjngine. --> <br/> <SCRIPT type = "text/JavaScript" src = "myactionapi. js"> </SCRIPT>
The first two script references are used to call some default operation functions provided by directjngine. You only need to reference it.
You cannot see the last JS before starting the server. Because it is a directjngine project, it is automatically generated according to your configuration. As for how it works, I will introduce it in detail below.
Step 3: design the server-side method. For example, the function name, whether to return values, and so on. Because on the hello.html page, I will call the method.
The specific code is used to centrally describe the latest hello.html code description.
Step 4: Use the Java language to compile the server-side method. The attached code is as follows:
Package COM. softwarementors. extjs. djn. myaction; <br/> Import COM. softwarementors. extjs. djn. config. annotations. directmethod; <br/> public class myaction {<br/> @ directmethod <br/> Public String doecho (string parm) <br/>{< br/> return "parameter is" + parm; <br/>}< br/> @ directmethod <br/> Public String doshow () <br/>{< br/> return "I am not easy"; <br/>}< br/>
Note:
@ Directmethod the label is very important. It is through this label and some web. xml configurations that will dynamically generate the JS to be introduced in step 2. In this writing mode, the method name you write in the Java code is the method name you call in the front-end Js. If you think this is not safe or professional. You can access a method by defining an alias. The writing format is as follows:
@ Directmethod (method = "nameyouwant ")
After this definition, when front-end JS calls the server method, the method name is the name you defined in red.
After the class is compiled, put the class file into the corresponding package directory of the WEB-INF/classes. It must have the same directory structure as the class file package in Web. xml.
Step 5: Tell directjngine where to find the server method.
The following code is available in the web. xml configuration:
<Init-param> <br/> <param-Name> mynamespace. classes </param-Name> <br/> <param-value> <br/> COM. softwarementors. extjs. djn. myaction. myaction <br/> </param-value> <br/> </init-param>
Note that the red part of mynamespace. classes must be the same as the mynamespace of the APIS variable in Web. xml.
Second, Com. softwarementors. extjs. djn. myaction. myaction is the full path name of the class written in Step 4. If there are multiple classes, separate them with commas.
Step 6: To enable extjs to call our server-side method, we need to register a remote call provider (A remoting provider. In the hello.html code, add the following statement to register a remote call provider for a space:
// Use the namespace to add and initialize the remote call API <br/> Ext. zhouyang. remoting_api.enablebuffer = 0; <br/> Ext. direct. addprovider (ext. zhouyang. remoting_api); </P> <p>
Note: The above Ext. zhouyang is defined in mynamespace. apinamespace in the web. xml variable.
Step 7: use JavaScript to call the server-side method.
Myaction. doshow (function (result, e) {<br/> var T = E. gettransaction (); <br/> If (E. status) {<br/> out. append (<br/> string. format ('<p> <B> Successful call to {0 }. {1} with response: </B> <XMP> {2} </XMP> </P> ', <br/> T. action, <br/> T. method, <br/> Ext. encode (result); <br/>}else {<br/> out. append (<br/> string. format ('<p> <B> call to {0 }. {1} failed with message: </B> <XMP> {2} </XMP> </P> ', <br/> T. action, <br/> T. m Ethod, <br/> E. message); <br/>}< br/> out. el. scroll ('B', 100000, true); <br/>}); </P> <p> // doecho function, which has parameters. <Br/> var parm = txtparm. value; // The parameter to be passed in to the background <br/> myaction. doecho (parm, function (result, e) {<br/> var T = E. gettransaction (); <br/> If (E. status) {<br/> out. append (string. format ('<p> <B> Successful call to {0 }. {1} with response: </B> <XMP> {2} </XMP> </P> ', <br/> T. action, T. method, ext. encode (result); <br/>}else {<br/> out. append (string. format ('<p> <B> call to {0 }. {1} failed with message: </B> <XMP> {2} </XMP> </P> ', <br/> T. action, T. method, E. message); <br/>}< br/> out. el. scroll ('B', 100000, true); <br/> });
The code above is a little messy. Here I will introduce the download URL of this demo. You can directly view the code.
We can see that for the function structure. If you need to input parameters, write the parameters before the function.
Because JavaScript calls the server-side method asynchronously, the best way is to define a callback function to process data, rather than terminating the program.
Therefore, a callback function is defined for the above two methods. This function is used to process the data returned by the server.
The result parameter is the data returned by the server method, and E is an even object, including a transaction object, which contains the action and method names and other information.
E. Status indicates that the function is successfully executed on the server. If false is returned, an error occurs on the server. You can view error information through E. Message.
Other parameters:
<Init-param> <br/> <param-Name> debug </param-Name> <br/> <param-value> true </param-value> <br/> </init-param>
If this parameter is set to true, related print information is entered in the stdout_2010 ***. log file in the Tomcat log directory. For example:
Info: COM. softwarementors. extjs. djn. servlet. directjngineservlet-"servlet global configuration: DEBUG = true, providersurl = djn/directprovider, Minify = true, batchrequestsmultithreadingenabled = true, bytes = 16, bytes = 80, batchrequestsmaxthreadsperrequest = 8, batchrequestsmaxthreadkeepaliveseconds = 60, gsonbuilderconfiguratorclass = com. softwarementors. extjs. djn. gson. defaultgsonbuilderconfigurator, dispatcherclass = com. softwarementors. extjs. djn. servlet. SSM. ssmdispatcher, jsonrequestprocessorthreadclass = com. softwarementors. extjs. djn. servlet. SSM. ssmjsonrequestprocessorthread, contextpath = -- not specified: calculated via JavaScript --, createsourcefiles = true "() <br/> info: COM. softwarementors. extjs. djn. servlet. directjngineservlet-"servlet global configuration: registryconfiguratorclass =" () <br/> info: COM. softwarementors. extjs. djn. servlet. directjngineservlet-"servlet APIs configuration: APIs = mynamespace" () <br/> info: COM. softwarementors. extjs. djn. servlet. directjngineservlet-"servlet 'mynamespace' API configuration: apinamespace = ext. zhouyang, actionsnamespace =, apifile = myaction/myactionapi. JS => full API file: C:/program files/Apache Software Foundation/tomcat 6.0/webapps/directdemo/myaction/myactionapi. JS, classes = com. softwarementors. extjs. djn. myaction. myaction "() <br/> info: COM. softwarementors. extjs. djn. jscodegen. codefilegenerator-"creating source files for APIS... "()
If the status is not debug, you can set it to false.
After completing the preceding steps, start Tomcat and find that three files are generated under the/tomcat 6.0/webapps/directdemo/myaction directory.
As follows:
Myactionapi. JS, MyActionApi-debug.js, MyActionApi-min.js. Myactionapi. JS is the Javascript introduced in step 2.
It serves the same purpose as the server-side code API. Because of its existence, the client's webpage knows what methods are defined on the server. In my demo, the generated myactionapi. js code is as follows:
/*************************************** * **************************** <Br/> * <br/> * code generated automatically by directjngine <br/> * copyright (c) 2009, Pedro agull ósoliveres <br/> * do not modify manually !! <Br/> * <br/> ******************************* ***************************************/ </P> <p> Ext. namespace ('ext. zhouyang'); </P> <p> Ext. zhouyang. provider_base_url = Window. location. protocol + '//' + window. location. host + '/' + (window. location. pathname. split ('/'). length> 2? Window. location. pathname. split ('/') [1] + '/': '') + 'djn/directprovider '; </P> <p> Ext. zhouyang. polling_urls ={< br/>}</P> <p> Ext. zhouyang. remoting_api = {<br/> URL: ext. zhouyang. provider_base_url, <br/> type: 'remoting ', <br/> actions: {<br/> myaction: [<br/>{< br/> Name: 'doecho '/* (string) => string */, <br/> Len: 1, <br/> formhandler: false <br/> }, <br/> {<br/> name: 'doshow'/* () => string */, <br/> Len: 0, <br/> formhandler: false <br/>}< br/>] <br/>}</P> <p>
You can see that function names, parameter types, and number of parameters are defined.
So far, directjngine and ext direct have successfully called the Java Server method.
The demo runs as follows:
The demo is as follows:
Http://download.csdn.net/source/2768164
This article is just a quick demo, in-depth principles and advanced usage. I will write an article later.
Please indicate the source of positive: http://blog.csdn.net/Achilles_Dynasty