You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Emaho, Ghoot" <Gh...@PETROTECHNICS.co.uk> on 2001/07/18 13:07:59 UTC

Hello & Best Practises Question

Hi,
 
I have a question regarding what the community would consider to be Best
Practises when using Struts. I am doing an internal review of our own
usage of Struts within our products, and I have constructed an example
application to highlight my question(s) and talk around. I would
appreciate any feedback, insight or help that the community can offer.
 
I appreciate that this question requires a bit of effort on the readers
part ! Hopefully I have explained the context in enough detail to
solicit decent responses, but not so detailed as to tire you out !
 
Most Web Applications perform similar tasks which could be considered
generic regardless of domain. I have taken some of these tasks and
bundled them into this simple example. The example is a simplification
of an application I am building at the moment - a Struts based Wiki
called 'ChiKi'. For those who are not aware of what a wiki is, refer to
http://www.c2.com/cgi/wiki?WikiWikiWeb
<http://www.c2.com/cgi/wiki?WikiWikiWeb>  for details.
 
Given an object that is to manipulated, core tasks that could be
performed on that object would include

*	View
*	Edit
*	Save
*	Create [not considered in example]
*	Delete [not considered in example]

In this example, we have the object is called 'Page' which has 'id' and
'content' as string attributes. I wish to be able to View, Edit, Save,
Create and Delete 'Pages'.
 
So here are some of my questions  - all implementation details can be
found at the end of the email:

*	given the details of the components and the xml configuration at
the end of this email, how does this implementation compare to how you
would do it ? i.e. what other ways would people 'configure' to achieve
the same end result ?
*	are there any given Best Practises that could apply ?
*	other areas to consider could include:

*	separation: save and edit could be encapsualted in the same
action, would you ?
*	Page & EditForm: essentially the same, how would people avoid
duplication ? or would they bother ?
*	usage of request scope
*	...

I appreciate that this is a simple example, but I would like to get a
feel for how people would do this, and ascertain if there are differing
strategies - and the various pros/cons of each strategy.
 
It is my intention to offer up the full blown ChiKi as an example app
for Struts development once it is ready, and I would like to be able to
take feedback from this post into account. This will hopefully be useful
for developers using Struts - it's always helpful to have other ex
amples ! and it will certainly be used internally for my developers
training/introduction to Struts - as well as actually being used within
our development team as a tool - wiki's are great :)
 
[We are about to release v1.0 of our current Struts based application -
a large enterprise application globally deployed for a large
multinational. I will post more details about this as I'm sure there is
interest in Real World developments. Of particular interest may be the
results from our Performance testing, the overall Architecture and so
on. This may be one of the largest and certainly most complex Struts
based applications to go into production, and I would like to share the
experience with the community. More details to follow shortly]
 
So thank you for making it this far, and if you have any feedback at
all, please let me know. 
 
Many thanks
 
Ghoot Emaho
Development Team Leader
Petrotechnics Ltd <http://www.petrotechnics.co.uk/> 

 

In the implementation, I have the following components:

*	Page.java

*	simple bean with accessor methods for 'id' and 'content'
attributes

*	ChikiService.java

*	provides methods getPage(String pageId) and setPage(Page p) to
be used by the Actions

*	View.java

*	extends Action
*	accepts 1 request parameter 'pageId' which uniqely identifies
the page
*	requests Page 'pageId' from ChikiService
*	puts the returned Page object into the request
*	returns a forward to View.jsp

	
*	View.jsp

*	displays the Page object in the request, using:

*	<bean:write name="page" property="id" scope="request"/>
*	<bean:write name="page" property="content" scope="request"/>

*	provides a hyperlink to 'Edit' this page:

*	<html:link  page="/chiki/edit" paramID="pageId" paramName="page"
paramProperty="id" >Edit</html:link>

	
*	EditForm.java

*	simple bean with accessor methods for 'id' and 'content'
attributes
*	identical to the 'Page' object, except it extends ActionForm

*	Edit.java

*	extends Action
	
*	accepts 1 request parameter 'pageId' which uniqely identifies
the page
*	requests Page 'pageId' from ChikiService
*	populates EditForm with values from the returned Page Object
*	returns a forward to Edit.jsp

	
*	Edit.jsp

*	displays the page to be edited. 'id' attribute is a text label
and the 'content' is in a text area which the user can edit and then
submit to be saved

*	<html:form action="/save" focus="content">
<html:hidden property="id"/><br>
<html:textarea property="content" size="80" rows="23"
redisplay="false"/><BR>
<html:submit property="submit" value="Save Changes"/>
<html:reset/>
</html:form>

*	Submits the EditForm to the 'Save' action for processing

*	Save.java

*	extends Action
*	creates a new Page object based on the EditForm contents
*	requests the Page be saved by calling ChikiService.setPage(page)
with the new page object
	
*	puts the new Page object into the request
*	returns a forward to Save.jsp

*	Save.jsp

*	provides a confirmation/preview view of the page with changes
applied
*	displays the Page object in the request, using:

*	<bean:write name="page" property="id" scope="request"/>
*	<bean:write name="page" property="content" scope="request"/>

*	provides a hyperlink to 'View' this page:

*	<html:link  page="/chiki/view" paramID="pageId" paramName="page"
paramProperty="id" >View</html:link>

 
 
Struts Configuration looks like this:
 
<struts-config>
    <!-- ========== Form Bean Definitions
=================================== -->
    <form-beans>    
        <!-- Edit form bean -->
        <form-bean name="editForm"
type="org.emaho.chiki.event.EditForm"/>    
    </form-beans>
    
    <!-- ========== Global Forward Definitions
============================== -->
    <global-forwards>
        <forward name="index" path="/index.html"/>    
        <forward name="error" path="/core/error.jsp"/>    
    </global-forwards>
 
    <!-- ========== Action Mapping Definitions
============================== -->
    <action-mappings>
        <!-- View Page -->
        <action path="/view"
                type="org.emaho.chiki.event.View"
                name="View"
                scope="request">  
                <forward name="view" path="/core/view.jsp"/>

        </action>           
        <!-- Edit Page -->
        <action path="/edit"
                type="org.emaho.chiki.event.Edit"
                name="editForm"
                scope="request"  
                input="/edit.jsp">
                <forward name="edit" path="/core/edit.jsp"/>

        </action>           
        <!-- Save Page -->
        <action path="/save"
                type="org.emaho.chiki.event.Save"
                name="editForm"
                scope="request"  
                input="/edit.jsp">
                <forward name="save" path="/core/save.jsp"/>

        </action>  
         
    </action-mappings>
</struts-config>