Various Java calling methods in DWR

Source: Internet
Author: User
Tags cdata

DWR is a framework. Simply put, it can directly call Java methods in Javascript without having to write a lot of JavaScript code. It is implemented based on Ajax and can achieve no refreshing effect.

There are many DWR examples on the Internet, but most of them are just some method calls. This article only introduces DWR at the usage level and does not involve more technologies and designs, the objective is to allow beginners to quickly learn how various Java methods are called in JavaScript.

This article is based on DWR 1.1. For DWR 2.0, we will not introduce it because it has not been officially released.

I,Web. XML in DWR Configuration

1Minimum Configuration <Servlet>

<Servlet-Name> DWR-invoker </servlet-Name>

<Servlet-Class> UK. Ltd. getahead. DWR. dwrservlet </servlet-Class>

</Servlet>

<Servlet-mapping>

<Servlet-Name> DWR-invoker </servlet-Name>

<URL-pattern>/DWR/* </url-pattern>

</Servlet-mapping>

2When IAreWant to seeDWRFromDynamicGeneratedTest page(Using debug/Test Mode)Hour, You canServletAdd to configuration <Init-param>

<Param-Name> debug </param-Name>

<Param-value>True</Param-value>

</Init-param> the default value of DWR is false. If you select true, you can view each deployed DWR class through http: // localhost: Port/APP/DWR. You can also test whether each method of Java code runs normally. For security consideration, you must set this parameter to false in the official environment. 3. configuration of multiple DWR. xml files may be in the following situations. One servlet contains multiple DWR. xml configuration files. Multiple servlets correspond to one or more DWR. xml configuration files. 3.1 One servlet, multiple DWR. xml configuration files <servlet>

<Servlet-Name> DWR-invoker </servlet-Name>

<Servlet-Class> UK. Ltd. getahead. DWR. dwrservlet </servlet-Class>

<Init-param>

<Param-Name> config-1 </param-Name>

<Param-value> WEB-INF/dwr1.xml </param-value>

</Init-param>

<Init-param>

<Param-Name> config-2 </param-Name>

<Param-value> WEB-INF/dwr2.xml </param-value>

</Init-param>

</Servlet> In this configuration, the value of param-name must start with config. The number of param-names can be greater than or equal to 0. Without param-name, the WEB-INF/DWR. XML is read. If there are more than zero param-names, the WEB-INF/DWR. xml file will not be read. 3.2. Multiple servlets. Each servlet corresponds to one or more DWR. xml <servlet>

<Servlet-Name> DWR-invoker </servlet-Name>

<Servlet-Class> UK. Ltd. getahead. DWR. dwrservlet </servlet-Class>

</Servlet>

<Servlet>

<Servlet-Name> dwr-invoker1 </servlet-Name>

<Servlet-Class> UK. Ltd. getahead. DWR. dwrservlet </servlet-Class>

<Init-param>

<Param-Name> config-Admin </param-Name>

<Param-value> WEB-INF/dwr1.xml </param-value>

</Init-param>

<Init-param>

<Param-Name> debug </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>

<Servlet-mapping>

<Servlet-Name> dwr-invoker1 </servlet-Name>

<URL-pattern>/dwr1/* </url-pattern>
</Servlet-mapping> in this case, we can control permissions based on J2EE Security and add different roles for different URLs.

II,DWR usage

1,TuneNo response is returned.ValueAnd ParameterJavaMethod

1.1. configuration of DWR. xml

<DWR>

<Allow>

<Create creator = "new" javascript = "testclass">

<Param name = "class" value = "com. DWR. testclass"/>

<Include method = "testmethod1"/>

</Create>

</Allow>

</DWR>

<Allow> the tag contains things that can be exposed to JavaScript access. <Create> the tag specifies the Java class that can be accessed in JavaScript and defines how DWR gets the instance of the class to be remotely implemented. Creator = "new" attribute specifies the Java class instance generation method. New means that DWR should call the default constructor of the class to obtain the instance. Other methods include the spring method, by integrating with the IOC container spring to obtain instances. The javascript = "testclass" attribute specifies the name used by the JavaScript code to access the object. <Param> label specifies the Java class name to be exposed to JavaScript. <Include> the tag specifies the method to be exposed to JavaScript. If this parameter is not specified, all methods are exposed. <Exclude> label specifies the method to prevent access.

1.2 calls in Javascript

First, introduce JavaScript scripts

<SCRIPT src = 'dwr/interface/testclass. js'> </SCRIPT>

<SCRIPT src = 'dwr/engine. js'> </SCRIPT>

<SCRIPT src = 'dwr/util. js'> </SCRIPT>

Testclass. JS is automatically generated by DWR according to the configuration file, and engine. js and util. js are built-in script files of DWR.

Second, compile the JavaScript function calltestmethod1 () {testclass. testmethod1 ();} that calls the Java method ();}

2,TuneAvailableSimpleReturnValueOfJavaMethod

2.1. the configuration of DWR. XML is the same as that of 1.1.

<DWR>

<Allow>

<Create creator = "new" javascript = "testclass">

<Param name = "class" value = "com. DWR. testclass"/>

<Include method = "testmethod2"/> </create>

</Allow>

</DWR>

2.2 calls in Javascript

First, introduce JavaScript scripts

Second, write the JavaScript function that calls the Java method and the callback function that receives the returned value.

Function calltestmethod2 (){

Testclass. testmethod2 (callbackfortestmethod2 );

}

Function callbackfortestmethod2 (data ){

// The Return Value of the date receiving Method

// The returned values can be processed and displayed here. Alert ("the return value is" + data );

}

Callbackfortestmethod2 is the callback function that receives the returned value.

3,TuneAvailableSimpleParameterJavaMethod

3.1. The configuration of DWR. XML is the same as that of 1.1.

<DWR>

<Allow>

<Create creator = "new" javascript = "testclass">

<Param name = "class" value = "com. DWR. testclass"/>

<Include method = "testmethod3"/>

</Create>

</Allow>

</DWR>

3.2 calls in Javascript

First, introduce JavaScript scripts

Second, write JavaScript Functions that call Java methods.

Function calltestmethod3 (){

// Define the parameters to be passed to the Java method

VaR data;

// Construct parameters

Data = "test string ";

Testclass. testmethod3 (data );

}

4,TuneReturnJavaBeanOfJavaMethod

4.1. configuration of DWR. xml

<DWR>

<Allow>

<Create creator = "new" javascript = "testclass">

<Param name = "class" value = "com. DWR. testclass"/>

<Include method = "testmethod4"/>

</Create>

<Convert converter = "Bean" match = "" com. DWR. testbean ">

   <Param name = "include" value = "username, password"/>

</Convert>

</Allow>

</DWR>

<Creator> labels are used to publish methods for remote web classes and classes. <convertor> labels are used to specify the parameters and return types of these methods.

The function of the convert element is to tell DWR how to convert the data type between the Java object representation on the server and the serialized JavaScript.

DWR automatically adjusts simple data types between Java and JavaScript representations.

These types include the Java Native type and their respective encapsulation class representation, as well as the string, date, array, and set types.

DWR can also convert Javabean to Javascript, but explicit configuration is required for security reasons. The <convertor> label completes this function.

Converter = "Bean" attribute specifies the conversion method using the JavaBean naming convention, match = "" com. DWR. testbean "attribute specifies the name of the JavaBean to be converted, and the <param> label specifies the JavaBean attribute to be converted.

4.2 calls in Javascript

First, introduce JavaScript scripts

Second, write the JavaScript function that calls the Java method and the callback function that receives the returned value.

Function calltestmethod4 (){

Testclass. testmethod4 (callbackfortestmethod4 );

}

Function callbackfortestmethod4 (data ){

// The Return Value of the date receiving Method

// There are two ways to process the return value of JavaBean

// If you do not know the attribute name, use the following method:

For (VAR property in data ){

Alert ("Property:" + property );

Alert (property + ":" + data [property]);

}

// Use the following method when you know the property name

Alert (data. username );

Alert (data. Password );

 

} Callbackfortestmethod4 is the callback function that receives the returned value.

5,TuneAvailableJavaBeanParameterJavaMethod

5.1. The configuration of DWR. XML is the same as that of 4.1.

<DWR>

<Allow>

<Create creator = "new" javascript = "testclass">

<Param name = "class" value = "com. DWR. testclass"/>

<Include method = "testmethod5"/>

</Create>

<Convert converter = "Bean" match = "com. DWR. testbean">

      <Param name = "include" value = "username, password"/>

</Convert>

</Allow>

</DWR>

5.2 calls in Javascript

First, introduce JavaScript scripts

Second, write JavaScript Functions that call Java methods.

Function calltestmethod5 (){

// Define the parameters to be passed to the Java method

VaR data;

// Construct a parameter. date is actually an object

Data = {Username: "user", password: "password "}

Testclass. testmethod5 (data );

}

6,TuneReturnList,SetOrMapOfJavaMethod

6.1. The configuration of DWR. XML is the same as that of 4.1.

<DWR>

<Allow>

<Create creator = "new" javascript = "testclass">

<Param name = "class" value = "com. DWR. testclass"/>

<Include method = "testmethod6"/>

</Create>

<Convert converter = "Bean" match = "com. DWR. testbean">

<Param name = "include" value = "username, password"/>

</Convert>

</Allow>

</DWR>

Note: The <convert> label is not required if the elements in list, set, or map are of simple type (including its encapsulation class), String, date, array, and set type.

6.2 calls in JavaScript (taking the returned list as an example, the list element is testbean)

First, introduce JavaScript scripts

Second, write the JavaScript function that calls the Java method and the callback function that receives the returned value.

Function calltestmethod6 (){

Testclass. testmethod6 (callbackfortestmethod6 );

}

Function callbackfortestmethod6 (data ){

// The Return Value of the date receiving Method

// There are two ways to process the return value of JavaBean

// If you do not know the attribute name, use the following method:

For (VAR I = 0; I <data. length; I ++ ){

For (VAR property in data ){

Alert ("Property:" + property );

Alert (property + ":" + data [property]);

}

} // When you know the attribute name, use the following method:
For (VAR I = 0; I <data. length; I ++ ){

Alert (data. username );

Alert (data [I]. username); // This is OK

Alert (data. Password); // Error

}

}

7,TuneAvailableList,SetOrMapParameterJavaMethod7

. 1. Configure DWR. xml

<DWR>

<Allow>

<Create creator = "new" javascript = "testclass">

<Param name = "class" value = "com. DWR. testclass"/>

<Include method = "testmethod7"/>

</Create>

<Convert converter = "Bean" match = "com. DWR. testbean">

<Param name = "include" value = "username, password"/>

</Convert>

</Allow>

<Signatures>

<! [CDATA [import java. util. List;

Import com. DWR. testclass;

Import com. DWR. testbean;

Testclass. testmethod7 (list <testbean> );

]>

</Signatures>

</DWR> <signatures> labels are used to declare the exact classes contained in the list, set, or map parameters of Java methods, so that Java code can make judgments.

7.2 calls in JavaScript (taking the returned list as an example, the list element is testbean). First, introduce JavaScript scripts. Second, compile the JavaScript function call calltestmethod7 (){

// Define the parameters to be passed to the Java method

VaR data;

// Construction parameter. date is actually an object array, that is, each element of the array is

Object Data = [

{

Username: "user1 ",

Password: "password2"

},

{

Username: "user2 ",

Password: "password2"

}

];

Testclass. testmethod7 (data);} Note: 1. For 6th cases, if the return value of the Java method is map, the Javascript callback function that receives the return value will be processed as follows: function callbackfortestmethod (data ){

// The Return Value of the date receiving Method

For (VAR property in data ){

VaR bean = data [property];

Alert (bean. username );

Alert (bean. Password );

}

} 2. For 7th cases, if the Java method parameter is map (assuming its key is string and value is testbean ), the following method is used in the JavaScript function that calls this method to construct the parameter to be passed: function calltestmethod (){

// Define the parameters to be passed to the Java method

VaR data;

// Construct a parameter. date is actually an object. Its attribute name is the key of MAP and its attribute value is the value of map.

Data = {

"Key1 ":{

Username: "user1 ",

Password: "password2"

},

"Key2 ":{

Username: "user2 ",

Password: "password2"

}

};

Testclass. testmethod (data );

} Add the following configuration segments to DWR. xml:

 <Signatures>

<! [CDATA [import java. util. List;

Import com. DWR. testclass;

Import com. DWR. testbean;

Testclass. testmethod7 (Map <string, testbean> );

]>

</Signatures>

3. From the above, we can find that when the return value of the Java method is list (SET), DWR converts it to an object array and passes JavaScript; when the return value of the Java method is map, DWR converts it into an object. The attribute of the object is the key value of the original map, and the attribute value is the value corresponding to the original map.

4. If the parameters of the Java method are list (SET) and MAP, JavaScript also constructs the corresponding JavaScript data to pass to Java based on the three parameters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.