Getting Beanclass object using dwr

Tuesday, January 4, 2011 Posted by Jagadeesh Manne
this example to show how to get bean object in jsp page and how to display values of that bean object
Creating beanClass
package mybean;

/**
 *
 * @author Jagadeesh Kumar
 * mannejkumar@gmail.com
 */
public class MyBeanClass {

   private String firstName ="";
   private String lastName="";

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

Creating JavaClass
package mypack;

import mybean.MyBeanClass;

public class MyJavaClass
{
    public MyBeanClass getDetails()
    {
        MyBeanClass mbobject = new MyBeanClass();
        mbobject.setFirstName("Jagadeesh");
        mbobject.setLastName("Kumar");
        return mbobject;
    }
    //we will call this method
    //in client side using javascript class
    //here we are returning bean object. so we
    //can use bean properties in jsp page
}
creating dwr.xml
<dwr>
<allow>
<create creator="new" javascript="MyJavaScriptClass">
<!-- here MyJavaScriptClass is dynamic javascript class generated by dwr-->
<param name="class" value="mypack.MyJavaClass"/>
<!-- now dwrservlet convert mypack.MyJavaClass to MyJavaScriptClass.so we can call methods of mypack.MyJavaClass using MyJavaScritpClass-->
</create>
<!-- now we have to convert our bean class using convertor type bean and we have to give bean class in match. if you put "mybean.*" it will convert all the bean class in that package. if you use "*" it will convert all the bean class in that application-->
<convert converter="bean" match="mybean.MyBeanClass"/>
</allow>
</dwr>
Creating web.xml
<web-app>
<servlet>
<servlet-name>
dwr-invoker
</servlet-name>
<servlet-class>
uk.ltd.getahead.dwr.DWRServlet
</servlet-class>
<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>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
creating jsp page
<%-- 
Document   : index
Created on : Jan 2, 2011, 7:37:30 AM
Author     : Jagadeesh
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Dwr Example</title>
<!-- You have to include these two JavaScript files from DWR -->
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<!-- This JavaScript file is generated specifically for your application -->
<!-- in dwr.xml we have converted MyJavaClass to MyJavaScriptClass.js-->
<!-- so we can call java class methods using this javascript class-->
<script type='text/javascript' src='dwr/interface/MyJavaScriptClass.js'>
</script>
<script>
function getDetailsFromServer()
{
//calling serverside method using our generated
//javascript class.if you do my first example
//then you can understand this example very easy
MyJavaScriptClass.getDetails(handleReceivedData);
}
function handleReceivedData(obj)
{
//DWRUtil.setValue and DWRUtil.getValue are the existing
//properties in engine.js, setValue sets the value to that
//particular id(including div,span,forms etc),DWRUtil.getValue
//get the value of that particular id.
DWRUtil.setValue("firstname",obj.firstName);
DWRUtil.setValue("lastname",obj.lastName);
//we got bean object and we converted that bean object to
//javascript object. so we have to call those bean properties
//using object.propertyname
}
</script>
</head>
<body>
FirstName:  <span id="firstname"></span> <br/>
LastName:  <span  id="lastname"></span><br/>
<button onclick="getDetailsFromServer()">submit</button>
</body>
</html>

in this example we got details from server using dwr with bean object
If you have any doubts post your comments below.
Download
Labels: , ,

Post a Comment