tapestry
http://tapestry.apache.org/tapestry3/doc/api/index.html
http://archive.apache.org/dist/jakarta/tapestry/Tapestry-3.0.2-bin.zip
Mock Table utility for CRUD operations
While learning a new technology we may need a database table to store the data.
But creating a database, setting up jdbc connection and writing crud operation may be cumbersome.
So I thought it would be good to have some mock utility to represent a table which can be used just like a database table.
Here is what I came up with.
1
|
package
com.sivalabs.sample.util;
|
2
|
import
java.io.Serializable;
|
4
|
public
interface
Identifiable<K>
extends
Serializable
|
6
|
public
void
setId(K id);
|
01
|
package
com.sivalabs.sample.util;
|
03
|
import
java.util.Collection;
|
04
|
import
java.util.HashMap;
|
07
|
public
abstract
class
Table<PK
extends
Object, T
extends
Identifiable<PK>>
|
09
|
protected
final
Map<PK, T> table =
new
HashMap<PK, T>();
|
10
|
public
abstract
PK getNextId();
|
16
|
public
void
create(T obj)
|
18
|
if
(table.containsKey(obj.getId()))
|
20
|
throw
new
RuntimeException(
"PrimaryKey ["
+obj.getId()+
"] already exists"
);
|
22
|
obj.setId(getNextId());
|
23
|
table.put(obj.getId(), obj);
|
26
|
public
Collection<T> getAll()
|
28
|
return
table.values();
|
31
|
public
T getById(PK id)
|
36
|
public
void
update(T obj)
|
38
|
if
(!table.containsKey(obj.getId()))
|
40
|
throw
new
RuntimeException(
"PrimaryKey ["
+obj.getId()+
"] doesn't exists"
);
|
42
|
table.put(obj.getId(), obj);
|
45
|
public
void
delete(T obj)
|
50
|
public
void
delete(PK id)
|
52
|
if
(!table.containsKey(id))
|
54
|
throw
new
RuntimeException(
"PrimaryKey ["
+id+
"] doesn't exists"
);
|
Let us create a pojo Message.java.
01
|
package
com.sivalabs.sample;
|
03
|
import
java.util.Date;
|
04
|
import
com.sivalabs.sample.util.Identifiable;
|
06
|
public
class
Message
implements
Identifiable<Integer>
|
08
|
private
static
final
long
serialVersionUID = 1L;
|
12
|
private
String postedBy;
|
13
|
private
Date postedDate =
new
Date();
|
18
|
public
Message(Integer id, String text, String postedBy, Date postedDate)
|
22
|
this
.postedBy = postedBy;
|
23
|
this
.postedDate = postedDate;
|
26
|
public
Integer getId()
|
30
|
public
void
setId(Integer id)
|
Now let us create a mock table for storing Messages.
The Message table needs to extend Table and provide what is the type of
primary key and what type of objects MessageTable is going to contain
using generics <Integer, Message>.
01
|
package
com.sivalabs.sample.util;
|
02
|
import
java.util.concurrent.atomic.AtomicInteger;
|
03
|
import
com.sivalabs.sample.Message;
|
05
|
public
class
MessageTable
extends
Table<Integer, Message>
|
07
|
private
static
final
AtomicInteger ATOMIC_INTEGER =
new
AtomicInteger(
0
);
|
09
|
public
Integer getNextId()
|
11
|
return
ATOMIC_INTEGER.incrementAndGet();
|
Now let us create a MessageService which holds an instance of MessageTable and expose the CRUD operations to clients.
01
|
package
com.sivalabs.sample;
|
03
|
import
java.util.Collection;
|
04
|
import
java.util.Date;
|
05
|
import
com.sivalabs.sample.util.MessageTable;
|
08
|
public
class
MessageService
|
10
|
private
static
final
MessageTable MESSAGE_TABLE =
new
MessageTable();
|
13
|
MESSAGE_TABLE.create(
new
Message(
1
,
"Message1 Text"
,
"Siva"
,
new
Date()));
|
14
|
MESSAGE_TABLE.create(
new
Message(
2
,
"Message2 Text"
,
"Prasad"
,
new
Date()));
|
15
|
MESSAGE_TABLE.create(
new
Message(
3
,
"Message3 Text"
,
"Prasad"
,
new
Date()));
|
16
|
MESSAGE_TABLE.create(
new
Message(
4
,
"Message4 Text"
,
"Siva"
,
new
Date()));
|
19
|
public
Collection<Message> getMessages()
|
21
|
return
MESSAGE_TABLE.getAll();
|
24
|
public
Message getMessage(Integer id)
|
26
|
return
MESSAGE_TABLE.getById(id);
|
29
|
public
void
saveMessage(Message message)
|
31
|
MESSAGE_TABLE.create(message);
|
34
|
public
void
updateMessage(Message message)
|
36
|
MESSAGE_TABLE.update(message);
|
39
|
public
void
deleteMessage(Integer id)
|
41
|
MESSAGE_TABLE.delete(id);
|
Now if you want to create a mock table for another pojo User.java it is simple.
01
|
package
com.sivalabs.sample.util;
|
02
|
import
java.util.concurrent.atomic.AtomicInteger;
|
03
|
import
com.sivalabs.sample.User;
|
05
|
public
class
UserTable
extends
Table<Integer, User>
|
07
|
private
static
final
AtomicInteger ATOMIC_INTEGER =
new
AtomicInteger(
0
);
|
09
|
public
Integer getNextId()
|
11
|
return
ATOMIC_INTEGER.incrementAndGet();
|
If the primary key is always an auto incremented integer value we can
move getNextId() method to Table.java. Then creating mock table becomes
even more simpler.
1
|
package
com.sivalabs.sample.util;
|
2
|
import
com.sivalabs.sample.User;
|
4
|
public
class
UserTable
extends
Table<Integer, User>
|
分享到:
相关推荐
Tapestry5最新中文教程.doc 作者 Renat Zubairov & Igor Drobiazko译者 沙晓兰 发布于 2008年7月2日 下午9时30分 社区 Java 主题 Web框架 ----------------------------------------- Tapestry5.1实例教程.pdf ...
Tapestry是一个基于控件的框架以致于用它开发Web应用类似开发传统的GUI应用。你用Tapestry开发Web应用时你无需关注以操作为中心的(Operation-centric) Servlet API.引用Tapestry网站上的一句话:"Tapestry用对象...
资源名称:深入浅出Tapestry内容简介:本书以循序渐进的方式,从Tapestry框架技术的基本概念入手,讲解Tapestry框架在J2EE Web应用程序中的整体架构实现。使读者在学习如何使用Tapestry框架技术的同时,还能够获得在...
本书以循序渐进的方式,从Tapestry框架技术的基本概念入手,讲解Tapestry框架在J2EE Web应用程序中的整体架构实现。使读者在学习如何使用Tapestry框架技术的同时,还能够获得在J2EE Web应用程序中应用Tapestry框架的...
本文介绍Tapestry框架版本5。本文利用Tapestry 5开发一个简单的具有创建/读/更新/删除功能的应用,在创建这个应用的过程中,本文体会到Tapestry带来的开发效率的提升。从多方面来讲解 Tapestry应用,比如应用的...
Tapestry 4 官方文档中文版本,现在中文资料比较少,和大家共享一下
Tapestry5.0.16文档和大家一起学习
这是Tapestry5.3.8 版本的一个大Demo,集合Spring4.0, 采用Maven 项目管理工具,没有集合Hibernate。 之所以说是个大Demo,是因为这项目中包含的内容并不少,包含: 1)解决了Tapestry5.3.8中文Bug问题 2)Tapestry...
tapestry3开发指南,带tapestry3所有jar包
tapestry-bin-5.1.0.5
Tapestry4的雏形是Tapestry3.1,Tapestry的作者Howard,不光开发了Tapestry,还同时开发了一个轻量级框架Hivemind。所以Tapestry3.1的开发一开始就处于Hivemind框架之下。后来由于种种原因,Howard没有将Tapestry3.1...
tapestry5.2.6 最新jar包,Tapestry框架是一个位于java servlet容器和Tapestry应用程序之间的层
Tapestry是一个开源的基于servlet的应用程序框架,它使用组件对象模型来创建动态的,交互的web应用。一个组件就是任意一个带有jwcid属性的html标记。其中jwc的意思是Java Web Component。Tapestry使得java代码与html...
tapestry技术 Tapestry是一个开源的基于servlet的应用程序框架,它使用组件对象模型来创建动态的,交互的web应用。一个组件就是任意一个带有jwcid属性的html标记。其中jwc的意思是Java Web Component。Tapestry使得...
强大的tapestry框架的学习入门资料,详细介绍了tapestry的常用开发文档。
包含: tapestry 的源代码, tapestry集成spring2.0 tapestry快速启动 tapestry upload tapestry hibernate tapestry annotations
tapestry5组件说明使用及登陆修改等简单实例
Tapestry 5开发指南(英文) Tapestry 5开发指南(英文) Tapestry 5开发指南(英文) Tapestry 5开发指南(英文)
Tapestry通用WEB框架支持,切换皮肤,自主分页,