DWR method call in a variety of java

DWR is a framework, a simple javascript that is able to call java methods directly, without having to write a lot of javascript code. It is based on the realization of the ajax, the effect can be achieved without refresh.
DWR online there are many examples, but most just call one way or another, this article is only introductory level in the use of DWR, is not related to more technical and design, its purpose is to allow beginners to quickly learn to a variety of java method is how the javascript call.

In this paper, based on DWR 1.1 for DWR 2.0, because there are no official release, so not to introduce.

A, dwr configuration of web.xml articles
1, minimal 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>
2, when we want to see automatically generated DWR test page (Using debug / test mode) can be added in the servlet configuration
<init-param>
<param-name> debug </ param-name>
<param-value> true </ param-value>
</ init-param>
DWR default this parameter is false. If you choose true, we can see you through the deployment of http://localhost:port/app/dwr each DWR class. Java code and can test whether each method is working correctly. For security considerations, in a formal environment you must set this parameter to false.
3, a number of configuration files dwr.xml
There are several possible situations, we have to name a few. A servlet, a number of configuration files dwr.xml; a number of servlet, each corresponding to one or more servlet configuration file dwr.xml.
3.1, a servlet, a number of configuration files dwr.xml
<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, param-name value must be at the beginning of config. can be param-name> = 0. If there is no param-name, it will be read WEB-INF/dwr.xml. Greater than zero if there are param-name, then the document will not be WEB-INF/dwr.xml read.
3.2, a number of servlet, each corresponding to a servlet 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 in accordance with J2EE security permissions for different url, plus a different role.

Second, dwr use articles
1, did not return calls and parameters of the value of the JAVA method
1.1, dwr.xml configuration
<dwr>
<allow>
<create creator="new" javascript="testClass">
<param name="class" value="com.dwr.TestClass" />
<include method="testMethod1"/>
</ create>
</ allow>
</ dwr>
<allow> label can be exposed to the javascript include a visit to East and West.
javascript tab <create> specified in the java category may be accessed, and the definition should be how to get DWR to remote instances of the class. creator = "new" attribute of the generated java class instance methods, new category means that DWR should call the default constructor to obtain examples of spring there are other ways, through the IOC container and Spring to get examples of integrated and so on. javascript = "testClass" attributes specify javascript code used to access the name of the object.
Tags <param> designated javascript must be open to the java class name.
Tags <include> designated to be made available to the javascript method. Do not specify if all the methods on the public.
Tags <exclude> to prevent access to the specified method.
1.2, javascript call
First of all, the introduction of javascript script
<script src='dwr/interface/ testClass.js'> </ script>
<script src='dwr/engine.js'> </ script>
<script src='dwr/util.js'> </ script>
TestClass.js which is under the dwr configuration file automatically generated, engine.js and dwr own util.js is the script file.
Secondly, the preparation of java method call javascript function
Function callTestMethod1 () (
testClass.testMethod1 ();
)
2, call a simple java method return value
2.1, dwr.xml configuration
Configuration with 1.1
<dwr>
<allow>
<create creator="new" javascript="testClass">
<param name="class" value="com.dwr.TestClass" />
<include method="testMethod2"/>
</ create>
</ allow>
</ dwr>
2.2, javascript call
First of all, the introduction of javascript script
Secondly, the preparation of java method call javascript function and receive the return value of callback function
Function callTestMethod2 () (
testClass.testMethod2 (callBackFortestMethod2);
)
Function callBackFortestMethod2 (data) (
/ / Which way to date to receive the return value of
/ / Can return value to the processing and display, etc.
alert ( "the return value is" + data);
)
CallBackFortestMethod2 which is to receive the return value of callback function
3, call a simple java method parameters
3.1, dwr.xml configuration
Configuration with 1.1
<dwr>
<allow>
<create creator="new" javascript="testClass">
<param name="class" value="com.dwr.TestClass" />
<include method="testMethod3"/>
</ create>
</ allow>
</ dwr>
3.2, javascript call
First of all, the introduction of javascript script
Secondly, the preparation of java method call javascript function
Function callTestMethod3 () (
/ / Define methods to reach the parameters of java
var data;
/ / Structure parameter
data = "test String";
testClass.testMethod3 (data);
)
4, call the java method to return to JavaBean
4.1, dwr.xml configuration
<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>
Tags <creator> responsible for long-range open for Web classes and class methods, <convertor> label is responsible for these parameters and return type. role is to convert the elements to tell the DWR on the server-side Java object serialization of JavaScript and how to convert between data types. DWR automatically between Java and JavaScript that the adjustment of simple data types. These types include native Java types and their respective categories, said the package, as well as String, Date, and set the type of the array. JavaBean can also DWR into JavaScript that, but for safety reasons, require explicit configuration, <convertor> tags is the completion of this feature. converter = "bean" attribute specified in the use of conversion JavaBean naming convention, match = "" com.dwr.TestBean "attribute specifies the name of javabean conversion, <param> labels specify the JavaBean properties of the conversion.
4.2, javascript call
First of all, the introduction of javascript script
Secondly, the preparation of java method call javascript function and receive the return value of callback function
Function callTestMethod4 () (
testClass.testMethod4 (callBackFortestMethod4);
)
Function callBackFortestMethod4 (data) (
/ / Which way to date to receive the return value of
/ / Return value for the JavaBean, there are two ways to deal with
/ / Do not know the name attribute, use the following method
for (var property in data) (
alert ( "property:" + property);
alert (property +":"+ data [property]);
)
/ / Know the name attribute, use the following method
alert (data.username);
alert (data.password);
)
CallBackFortestMethod4 which is to receive the return value of callback function
5, there are JavaBean call java method parameters
5.1, dwr.xml configuration
Configuration with 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, javascript call
First of all, the introduction of javascript script
testClass.testMethod (data);
)
And dwr.xml configuration to add the following paragraph
<signatures>
<! [CDATA [
import java.util.List;
import com.dwr.TestClass;
import com.dwr.TestBean;
TestClass.testMethod7 (Map <String,TestBean>);
]]>
</ signatures>
3, can be found from the above, for the return value of java method for List (Set) situation, DWR will be translated into a Object array, pass a javascript; for the return value of java method for Map of the situation, DWR will be translated into a Object, of which the properties of Object as the key value of the original Map, Map attribute values for the original value of the corresponding value.
4, If the java method parameters List (Set) and Map of the situation, javascript should be based on three kinds of said data structure corresponding to the javascript to java in the transmission.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of DWR method call in a variety of java

  • Build flex + spring + blazeds + hibernate application

    Build flex + spring + blazeds + hibernate application First, set up the project blazeds 1, will blazeds.war extract to a directory, such as: myflex /; 2, set up java works were such as: MyFlex, in the orientation of selection create project from exis ...

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.1 ...

  • hibernate generic generic DAO

    hibernate generic generic DAO

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and oth ...

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • The level Hibernate cache

    Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

Leave a Reply

Recent
Recent Entries
Tag Cloud
Random Entries