Step by Step develop a Jdon application with jdon-struts1x
- Quick starting configuration(for xml)
- Create/Read/Update/Delete(CRUD) of the Model;
- Batch inquiry and multi-page display
this document will show you how to develop these functions by Jdon Framework.
the source of this document's sample is the package of JdonCRUDsample.rar or jdonsample.rar.
this is no Domain Events or DCI.
DEMO Example
Source Download
let's begin...
Step By Step develop CRUD function
In CRUD developing process there are two parts that are coding and configuration.
Coding:
In coding phase there are 3 steps:
1. create a Model class code: sample.model.Message, it's content is below:
//@Model
public class Message extends Model {
private Long messageId;
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
} |
note:
- the class must be annotated by Model annotation ,or extends com.jdon.controller.model.Model of the jdon framework,
or implements the interface:com.jdon.controller.model.ModelIF.
- the class must have a unique key/primary key so can indicate a object, the key is equals
to the primary ke of the table in database.
enable Domain Model in memory, in persistence layer,such as jdbc:
@Component()
@Introduce("modelCache")
public class DAOJdbc implements MessageRepository{
@Around
public Message getMessage(String Id) {
...... //fetch from key-value stores or relation DB
}
} |
2. create the service of the Model, a interface and a implements class of the interface,
the interface is sample.service. MessageService:
public interface MessageService {
void createMessage(EventModel em);
void updateMessage(EventModel em);
void deleteMessage(EventModel em);
Message getMessage(String messageId);
} |
you can creat the concrete class of the MessageService in any time, in this sample it is sample.service.MessageServiceImp
3. create the UI form class:sample.web.MessageForm, the content of the class MessageForm
is same as the Model class sample.model.Message, the MessageForm's content can bigger than Message,
public class MessageForm extends ModelForm {
private String messageId;
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
} |
configuration:
There are two steps:
- configure the relation of these class: Message/MessageService/MessageForm
- configure page flow in the struts's struts-config.xml
Frist Step . the configuration about Jdon Framework
by the configuration we will create the relations among the Model Message.
the MessageService and the UI form MessageForm, the relations will tell
Jdon Framework: this is a CRUD implemention about a Model.
this configuration is a XML file about Jdon Framework,so it's name can be
jdonframework.xml, however we can change the name, but need tell Jdon the name.
we can tell the name in struts-config.xml as below :
<plug-in className="com.jdon.strutsutil.InitPlugIn">
<set-property property="modelmapping-config" value="jdonframework.xml" />
</plug-in> |
so, when our project start, the jdonframework.xml configuration will be loaded by
the Jdon Framework.
the content about jdonframework.xml as below:
<models>
<!-- the Model class is sample.model.Message, and the primary key of the Model class is messageId -->
<model key="messageId" class ="sample.model.Message">
<!-- configuration about UI Form: MessageForm -->
<actionForm name="messageForm"/>
<handler>
<!-- configuration about the Model service : MessageService -->
<service ref="messageService">
<getMethod name="getMessage" />
<createMethod name="createMessage" />
<updateMethod name="updateMessage" />
<deleteMethod name="deleteMessage" />
</service>
</handler>
</model>
......
</models>
<services>
<!-- the Ioc configuration about MessageService -->
<pojoService name="messageService" class="sample.service.MessageServiceImp"/>
......
</services> |
we will show how to configure those lines in jdonframework.xml
One. Model's configuration:
this line will define a Model class and its primary filed:
<model key="messageId" class ="sample.model.Message"> |
messageId is a field of the class: sample.model.Message, and one messageId'value
indicate unique one object of the Model class Message. the messageId can be the
Object ID, some like the primary key of a table.
String is recommended for the type of primary key.
Two. MessageForm's configuration:
<actionForm name="messageForm"/> |
messageForm is the name of the class sample.web.MessageForm, but where the name is defined?
it is defined in struts-config.xml, such as :
<struts-config>
<form-beans>
<form-bean name="messageForm" type="sample.web.MessageForm" />
……
</form-beans>
…..
</struts-config> |
Three. MessageService's configuration:
in jdonframework.xml, we will declare the POJO service,
we can define the name of the class sample.service.MessageServiceImp is 'messageService'.
<pojoService name="messageService" class="sample.service.MessageServiceImp"/>
if there any dependences in MessageServiceImp, we only configure the dependent class in here, if the
dependent class is a Dao class, so we can configure the Dao class as below:
<component name="messageDao" class="sample.dao.hibernate.MessageDaoHB" >
<constructor value="java:/TestDS"/>
</component>
ok, let's go back the CRUD configuration, after the POJO service name is configured,
so we must tell Jdon Framework the service's method names about CRUD operations.
<handler>
<!-- this will refer to service: messageService-->
<service ref="messageService">
<!--getMessage is the method name of MessageService -->
<getMethod name="getMessage" />
<!--createMessage is the method name of MessageService -->
<createMethod name="createMessage" />
<!--updateMessage is the method name of MessageService -->
<updateMethod name="updateMessage" />
<!--deleteMessage is the method name of MessageService -->
<deleteMethod name="deleteMessage" />
</service>
</handler> |
those methods are actually the name of the interface sample.service. MessageService too.
let's step into the second part of the CRUD configuration!
Second Step .: page flow in struts-config.xml
there are two page flows in the CRUD:
One. the server will push a page that user will create/update to the client .
we can configure a action and jsp in struts-config.xml:
<action name="messageForm" path="/messageAction" type="com.jdon.strutsutil.ModelViewAction"
scope="request" validate="false">
<forward name="create" path="/message.jsp" />
<forward name="edit" path="/message.jsp" />
</action>
|
the type value "com.jdon.strutsutil.ModelViewAction" is the implemention of Jdon Framework.
when the users call http://localhost:8080/messageAction.do, ModelViewAction will be active, and
will enter the "create" flow of the 'forward', and the server will push '/message.jsp'.
when the users call http://localhost:8080/messageAction.do?action=edit&messageId=18, ModelViewAction
will enter the "edit" flowof the 'forward', and the server will push '/message.jsp' that contains a
existed old datas, so the users can update those old form datas.
there a coding rule in /message.jsp, as below:
<html:form action="/messageSaveAction.do" method="POST" >
<html:hidden property="action"/> <!-- this is a rule -->
MessageId:<html:text property="messageId"/>
<br>Name:<html:text property="name"/>
<br><html:submit property="submit" value="Submit"/>
</html:form> |
in the message.jsp, there is a hidden field named "action". this is a rule of CRUD
when the page was submited, that will active '/messageSaveAction.do', this is
about next flow configuration:
Two the server should accept the submited datas in the last page, as below:
<action name="messageForm" path="/messageSaveAction" type="com.jdon.strutsutil.ModelSaveAction"
scope="request" validate="true" input="/message.jsp">
<forward name="success" path="/result.jsp" />
<forward name="failure" path="/result.jsp" />
</action>
|
com.jdon.strutsutil.ModelSaveAction is another implementions of Jdon Framework.
'ModelSaveAction' will delegate 'ModelHandler' (default it is XMLModelHandler) call
the service, in the '<handler></handler>' of the jdonframework.xml, we have configured
the CRUD method of your server as above.
so, we finish the CRUD development.
Step By Step develop Batch Inquiry and muilt-pages Display function
Coding:
In this phase there are three steps:
First Step. code a query class that implements com.jdon.strutsutil.ModelListAction, the class named sample.web.MessageListAction, in this class we must implements two methods: 'getPageIterator' and 'findModelByKey'.
'getPageIterator' method will call the MessageService:
MessageService messageService = (MessageService) WebAppUtil.getService("messageService",request);
return messageService.getAllMessages(start, count);
|
the interface MessageService must have the getAllMessages mehtod that by yourself implement.
'findModelByKey' method will call the getMessage of the service:
MessageService messageService = (MessageService) WebAppUtil.getService("messageService", request);
return messageService.getMessage((String)key);
|
Second Step: Implement the getAllMessages in Service layer, we will call the MessageDao to implement the function (see the source),
Third Step: In MessageDao we create a PageIterator object and return it ,this can done by Jdon Frameowork.
public PageIterator getMessages(int start, int count) throws Exception {
String GET_ALL_ITEMS_ALLCOUNT = "select count(1) from testmessage ";
String GET_ALL_ITEMS = "select messageId from testmessage ";
return pageIteratorSolver. getPageIterator (GET_ALL_ITEMS_ALLCOUNT,
GET_ALL_ITEMS, "",start, count);
}
|
if there are method parameters, we can implement them as below:
public PageIterator getMessages(Long categoryId, int start, int count) {
String GET_ALL_ITEMS_ALLCOUNT =
"select count(1) from message where categoryId = ? ";
String GET_ALL_ITEMS =
"select messageId from message where categoryId = ? ";
Collection params = new ArrayList(1);
params.add(categoryId);//paramters will be put into Collection
return pageIteratorSolver.getPageIterator(GET_ALL_ITEMS_ALLCOUNT, GET_ALL_ITEMS, params, start, count);
}
|
Configuration One:
JdonFramework's configuration, we need put our POJO component into Ioc container, so we can call them in coding, this step is same as CRUD.
<services>
<pojoService name="messageService" class="sample.service.MessageServiceImp"/>
<component name="messageDAO" class="sample.dao.MessageDAO"/>
<component name="constants" class="sample.Constants">
<constructor value="java:/TestDS"/>
</component>
</services>
|
in the MessageServiceImp, there are such construct method:
public class MessageServiceImp implements MessageService{
private MessageDAO messageDAO;
public MessageServiceImp(MessageDAO messageDAO){
this.messageDAO = messageDAO;
}
} |
the dependencs between MessageService with MessageDAO will be solved by Jdon Ioc container.
Configuration Two:
this step is a normal struts action configurtion , as below:
<form-beans>
……
<form-bean name="listForm" type="com.jdon.strutsutil.ModelListForm" />
</form-beans>
|
com.jdon.strutsutil.ModelListForm is the default class of Jdon Framework, we configure action:
<action name="listForm" path="/messageListAction"
type="sample.web.MessageListAction" scope="request">
<forward name="success" path="/messageList.jsp" />
</action>
|
sample.web.MessageListAction is our implements as above.
when the users browse http://localhost:8080/ messageListAction.do, they will see the all messages items sist, default count is 30 .
in the messageList.jsp we need add the multi-pages tag:
<MultiPages:pager actionFormName="listForm" page="/messageListAction.do">
<MultiPages:prev name="[Prev ]" />
<MultiPages:index displayCount="1" />
<MultiPages:next name="[Next ]" />
</MultiPages:pager> |
and in the messageList.jsp we can get a Message object by the tag "logic:iterator" :
<logic:iterate indexId="i" id="message" name="listForm" property="list" >
<bean:write name="message" property="name" />
.........
</logic:iterate |
We finish the Batch Inquiry and muilt-pages Display function! All source code can download by click here, and the running demo click here
|