Spring MVC+DWR Integration
What is DWR(Direct Web Remoting)?
The DWR is an open source solution under the Apache license.
It allows to call java method dirctely from jsp page
Advantages:
Tt makes remote call more easily
We can call java service method dirctely so performance is better
Reverse Ajax allows Java code running on the server to find out what clients are viewing certain pages, and to send to them JavaScript, generated either manually or using a Java API. These JavaScript generating APIs generally match a client-side APIs.
This method of remoting functions from Java to JavaScript gives DWR users a feel much like conventional RPC mechanisms like RMI or SOAP
web.xml
1.we need to configure DwrSpringServlet class for DWR and DispatcherServlet for Spring MVC.
<servlet>
<servlet-name>DWR_SPRING</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Mapping for Spring MVC -->
<servlet-mapping>
<servlet-name>DWR_SPRING</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
//Serlvet for DWR-SPRING
<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>
2.Configuration in applicationContext.xml file
Which service methods we need to access from jsp page we need for that service we need add dwr properties to that particular bean.
Here
i)RoleMasterService is service class accessing from jsp
ii).dwr:convert is userd for coverting javaobject to javascript object
<bean id="role" class="iton.service.RoleMasterService">
<property name="roleService" ref="roleService"></property>
<dwr:remote javascript="role">
<dwr:include method="createRole" />
<dwr:convert type="bean" class="com.iton.dwr.pojos.RoleDTO" >
<dwr:include method="roleName" />
<dwr:include method="roleType" />
</dwr:convert> </dwr:remote>
</bean>
</beans>
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="message" />
</bean> </beans>
3)calling a java method using DWR
first we need to import .js file which we exposed as service
<script type='text/javascript' src="http://192.168.0.175:8080/spring-mvc-dwr-1/iton/dwr/engine.js"></script>
<script type='text/javascript' src="http://192.168.0.175:8080/spring-mvc-dwr-1/iton/dwr/util.js"></script>
<script type="text/javascript" src="http://192.168.0.175:8080/spring-mvc-dwr-1/dwr/interface/role.js"></script>
</head>
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
Here is the code to call java service method using DWR-SPRING from jsp file
<script type="text/javascript">
function createRole() {
// Retrieve value of text input
alert("::::::::::::::");
var operand1 = dwr.util.getValue("roleName");
var operand2 = dwr.util.getValue("roleType");
var p = { roleName:operand1, roleType:operand2 };
alert(operand1+" ::::"+operand2);
// Pass two numbers, a callback function, and error function
role.createRole(p, {
callback : handleAddSuccess,
errorHandler : handleAddError
});
}
// data contains the returned value
function handleAddSuccess(data) {
// Assigns data to result id
dwr.util.setValue("sum", data);
alert(data +"::::::");
}
function handleAddError() {
// Show a popup message
alert("We can't add those values!");
}
</script>
Required maven dependency
<dependency>
<groupId>org.directwebremoting</groupId>
<artifactId>dwr</artifactId>
<version>3.0.0-rc3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.directwebremoting</groupId>
<artifactId>dwr</artifactId>
<version>3.0.0-rc3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.directwebremoting</groupId>
<artifactId>dwr</artifactId>
<version>3.0.M1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
0 comments: