DWR Advanced Topics DWR and Spring integration

Source: Internet
Author: User
WR Advanced Topics DWR and Spring integration
---------
Step One: Let Dwr have access to the context of spring
To integrate DWR and spring,dwr, you need to access the context of spring. Here are two options:
1. Using Spring MVC
2. Use of Dwrspringservlet
Using Spring MVC

If you use Spring MVC, our web.xml configuration is like this:

<servlet>
  <servlet-name>springDispatcher</servlet-name>
  <servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class>
  <init-param>
    < param-name>contextconfiglocation</param-name>
    <param-value> Classpath:yourSpringContext.xml </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</ servlet>
<servlet-mapping>
  <servlet-name>springDispatcher</servlet-name>
  <url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
  <servlet-name>springDispatcher</servlet-name>
  <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
The mapping of *.html also maps the/dwr/*.
Using Dwrspringservlet
If you do not use SPRINGMVC, then you can use Dwrspringservlet. This servlet obtains the spring context that is configured in your web.xml. The Web.xml configuration is as follows:
<listener>
  <listener-class>org.springframework.web.context.contextloaderlistener</ listener-class>
</listener>
<context-param>
  <param-name>contextconfiglocation </param-name>
  <param-value>
    classpath:yourSpringContext.xml
  </param-value>
</context-param>
<servlet>
  <servlet-name>dwr</servlet-name>
  < Servlet-class>org.directwebremoting.spring.dwrspringservlet</servlet-class>
  <init-param>
    <param-name>debug</param-name>
    <param-value>true</param-value>
  </ init-param>
</servlet>
<servlet-mapping>
  <servlet-name>dwr</ servlet-name>
  <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

Step Two: Configure remote access for DWR
Configuring DWR remote Access Here are a number of options, and we normally use the Dwr.xml file to use the creator and the converter in the file. However, Spring2. X introduces new features (namespaces) that we can use to consolidate dwr and spring in dwr2.x or higher, and to define remote access (note: The version of spring must be more than 2.5):
The first way: using namespaces
There are also two ways to use namespaces, a common declaration, and the other way to comment declarations
First, add the header of the Dwr namespace to the XML configuration file of Spring

<beans ...
  Xmlns:dwr= "Http://www.directwebremoting.org/schema/spring-dwr"
  xsi:schemalocation= "....
    Http://www.directwebremoting.org/schema/spring-dwr
    http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd ">
Normal way of declaring (label description)
Controller label (SPRINGMVC)
If you do not use SPRINGMVC, you can skip this section and the controller tag only applies to SPRINGMVC configuration.
If you use SPRINGMVC, you must declare a:
<dwr:controller id= "Dwrcontroller" debug= "true"/>
This label does not allow internal tags and ID attributes to be optional.
In addition, you can specify your own simpleurlhandlermapping. DWR requires mappings for the following URLs:/engine.js,/interface.js,/call/**,/interface/**, as an example:
<bean class= "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
  <property value= " True "Name=" Alwaysusefullpath ></property>
  <property name= "Mappings" >
    <props>
      <prop key= "/dwr/**/*" >dwrController</prop>
    </props>
 </property>
</bean >
Configure labels
<dwr:configuration/> is used to simulate the behavior of configuring in Dwr.xml. This label is optional. It may be a nested label (Init,creator,signatures,...) that mimics the behavior of those dwr.xml. As an example:
<dwr:configuration>
  <dwr:convert type= "Bean" class= "org.uk.ltd.dwr.dev.model.Address"/>
< /dwr:configuration>
Remote label
You can include <dwr:remote javascript= "..." > tags in each bean, where you can specify the method that is being represented or the method that is not allowed to be represented, such as:
<bean id= "Timeconvert" class= "Com.mycompany.ui.util.TimeConvert" >
  <dwr:remote javascript= " Ajaxtimeconvert ">
    <dwr:include method=" Convert "/>
  </dwr:remote>
</bean>
How annotations are used (note description)
<dwr:annotation-scan&gt: annotation scan, allow DWR scan classpath, detect bean @remoteproxy and @remotemethod annotations,
Register them and create a builder agent for them. This element includes some properties that are available:
Base-package: Scanned Packets
Regex: A regular expression that will be used in CLASSPATH scans
SCANREMOTEPROXY:DWR Remote agent Scan, default to True
SCANDATATRANSFEROBJECT:DWR Converter scans, default to True, scans classes with @datatransferobject annotations, representing classes to convert.
Scanglobalfilte: Default to True
<dwr:annotation-config&gt: Allows DWR to scan the context of spring, detect annotations with @remoteproxy and @remotemethod, and register the creator agent for them.
(above two configuration, it seems to be two choose one ...) )
Instructions on using annotations on Java classes
@RemoteProxy annotations (for classes) indicate that this class can be accessed remotely, @RemoteMethod annotations (for methods) to indicate that this method can be invoked. Any method that is not annotated in this class will not be used by DWR. If you want to use other JavaScript object names that are different from the current class, you need to add a Name property to the @remoteproxy annotation as follows:
@RemoteProxy (name= "Someclassname")
You can use @DataTransferObject and @remoteproperty annotations when you need to work with the bean that you want to dwr convert, as follows:
@DataTransferObject public
class mybean{
    @RemoteProperty
    private String firstName;
    Public String Getfirstname () {return
        firstName
    }}
@DataTransferObject Note that the Bean can be dwr converted, @RemoteProperty note that the attribute FirstName should be converted. So, if you want some of the bean's members not to be passed back and forth between the client and the server, just cancel the corresponding @remoteproperty annotation.

The second way: Use the spring's creator in the Dwr.xml file
If you don't like the way you use annotations, we can use the traditional dwr.xml configuration, using the spring based creator. The creator finds the Bean configuration file for spring and relies on spring to instantiate them. As an example:

<allow> ...
  <create creator= "Spring" javascript= "Fred" >
    <param name= "Beanname" value= "Shiela"/>
  </ Create> ...
</allow>
Here Name= "Beanname", which means that the name of the bean is used, and value is the name of the actual bean.




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.