GWT Example
The developer writes ..
The file My.mlg is written by the developer, it contains the definition of one or more beans (or DTO's) and one service, with one or more methods.
variable vApplicationExceptionPackage = myapp.client.common;
variable vBaseServicePackage = myapp.server.common;
package myapp.client;
bean MyBean
{
int id;
String name;
}
package myapp.server;
service MyService
{
List<MyBean> getNameList()
<<
select id, name
from t_name
>>
}
At the beginning of the above file, a variable vBaseServicePackage is defined, which contains the path to the BaseService class, as you already saw in the client/server example. But now there also is an variable vApplicationExceptionPackage which defines the package in which the ApplicationException class is to be found.
.. and Amalegeni generates.
Bean or DTO
File: myapp/client/MyBean.java
package myapp.client;
import com.google.gwt.user.client.rpc.IsSerializable;
public class MyBean implements IsSerializable
{
private int id;
private String name;
public MyBean()
{
}
public MyBean(int id, String name)
{
this.id=id;
this.name=name;
}
public void setId(int _n) { id=_n; }
public int getId() { return id; }
public void setName(String _n) { name=_n; }
public String getName() { return name; }
}
Service Interface
File: myapp/client/MyService.java
package myapp.client;
import java.util.*;
import myapp.client.common.ApplicationException;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import myapp.client.MyBean;
@RemoteServiceRelativePath("MyService")
public interface MyService extends RemoteService
{
public List<MyBean> getNameList()
throws ApplicationException;
}
Async Service Interface
File: myapp/client/MyServiceAsync.java
package myapp.client;
import java.util.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import myapp.client.MyBean;
public interface MyServiceAsync
{
public void getNameList( AsyncCallback<List<MyBean>> callback);
}
Service Implementation
File: myapp/server/MyServiceImpl.java
package myapp.server;
import java.sql.*;
import java.util.*;
import myapp.server.common.BaseService;
import myapp.client.common.ApplicationException;
import myapp.client.MyService;
import myapp.client.MyBean;
public class MyServiceImpl extends BaseService implements MyService
{
@Override
public List<MyBean> getNameList()
throws ApplicationException
{
Connection connection = null;
try
{
connection = getConnection();
PreparedStatement pstmt= connection.prepareCall(
"select id, name "+
"from t_name ");
ResultSet resultSet=pstmt.executeQuery();
List<MyBean> c = new ArrayList<MyBean>();
while ( resultSet.next() )
{
c.add(resultSetToMyBean( resultSet ) );
}
resultSet.close();
pstmt.close();
return c;
}
catch (SQLException sqe)
{
throw (new ApplicationException(sqe.getMessage()));
}
finally
{
try
{
returnConnection(connection);
}
catch(SQLException sqqe)
{
throw new ApplicationException("SQLException on returning connection: "
+ sqqe.getMessage());
}
}
}
}
A textfile with code snippets
File: ./myapp/client/MyService-cut and paste.txt
*******************************************************************************
Cut and paste snippets
*******************************************************************************
When deploying into a web-container like tomcat, add this to your web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
.. other entries.. WEBXMLENTRYSTART
<servlet>
<servlet-name>MyService</servlet-name>
<servlet-class>myapp.server.MyServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyService</servlet-name>
<url-pattern><!--/MODULENAME-->/MyService</url-pattern>
</servlet-mapping>
.. other entries.. WEBXMLENTRYEND
</web-app>
*******************************************************************************
Cut and paste your CALLBACK code snippets from here.
*******************************************************************************
..
MyServiceAsync myServiceAsync=null;
..
// lazy initialization of service proxy
if (myServiceAsync == null)
{
myServiceAsync = GWT.create(MyService.class);
}
..
*******************************************************************************
Method List<MyBean> getNameList()
*******************************************************************************
statusBar.setMessage("getNameList");
AsyncCallback<List<MyBean>> callback = new AsyncCallback<List<MyBean>>()
{
public void onFailure(Throwable caught)
{
statusBar.setErrorMessage(caught.getMessage());
GWT.log( "Exception in getNameList",caught);
}
public void onSuccess(List<MyBean> rv)
{
statusBar.setMessage("ok");
// do something with rv
}
};
myServiceAsync.getNameList( callback);
How do I do this myself ?
Download
If you haven't done so already, download amalegeni and set it up. See the download page.
Create a definition file
Create a file 'My.mlg', and copy/paste the following definitions into it:
variable vApplicationExceptionPackage = myapp.client.common;
variable vBaseServicePackage = myapp.server.common;
package myapp.client;
bean MyBean
{
int id;
String name;
}
package myapp.server;
service MyService
{
List<MyBean> getNameList()
<<
select id, name
from t_name
>>
}
Get a copy of the client/server template
Invoke amalegeni to print out the client server template, and pipe it into a file called 'gwt.stg'.
amalegeni -s gwt.stg > gwt.stg
(if you run amalegeni -s you get a list of available templates).
Now generate those java files !
amalegeni -t gwt.stg My.mlg
Output:
Using template: gwt.stg
Handling file: My.mlg
Writing to file: myapp/client/MyBean.java
Writing to file: myapp/client/MyService.java
Writing to file: myapp/client/MyServiceAsync.java
Writing to file: myapp/server/MyServiceImpl.java
Writing to file: myapp/client/MyService-cut_and_paste.txt
|