Follow below link to read Layer SuperType Design pattern, thanks.
http://asifshehzad.blogspot.com/2009/05/layer-supertype-design-pattern.html
Follow below link to read Layer SuperType Design pattern, thanks.
http://asifshehzad.blogspot.com/2009/05/layer-supertype-design-pattern.html
→ 2 CommentsCategories: Design Patterns
Tagged: Design Patterns, layer supertype, layer supertype design pattern, layer supertype pattern, software design
just recently i was assigned a task i.e. Populate a form using ActionForm that have multiple values for same property name [i.e. Array]. Although it is very straight forward, but i spent some more time, this is how i proceed. (i am putting it here, thinking it might help someone).
Scenerio/Case: A web application allow users to add/remove widgets on their homepage and set widgets display order. User selected widgets are presented on the home page in specified order when he signed-in.
We want to make a form that display the list of all widgets with indication what widgets user is currently using and in which order they are being displayed. This form allows user to:
1. Remove his current home page widgets
2. Change order
3. Add new widgets from the list of available widgets
Let us dig into code directly. This is how the ActionForm look like:
public class EditPageWidgetsForm extends ActionForm {
private Integer[] id = new Integer[100]; // the widget Id
private String[] title = new String[100]; //widget title
private Integer[] order = new Integer[100]; //widget order
private Boolean[] status = new Boolean[100]; // widet status; enable or disable
public EditPageWidgetsForm(){ }
// Generate/Write default getters and setters for each property. For example for property “id” it would look like:
public Integer getId(int index) {
return this.id[index];
}
public void setId(Integer[] id) {
this.id = id;
}
Generate getters and setters for other properties as well.
Now write another getter and setter for each property as follow:
public Integer getId( int index ) {
return this.id[index];
}
public void setId ( int index , Integer value ) {
this.id [ index ] = value ;
}
Write getter and setters as above for other properties as well, in the same way as above.
__________________________________________________________
Now come to Action that populates this ActionForm. I am using Dispatch Action, so i first populate the action form in view() method of Dispatch Action and then i submit the for to update() method, so that user current widgets preferences can be saved.
The related code of view() method looks like:
public class EditHomePageWidgetsAction extends DispatchAction {
public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
UserWidgetDAO userWidgetDAO = new UserWidgetDAO();
WidgetDAO widgetDAO = new WidgetDAO();
EditPageWidgetsForm ewForm = (EditPageWidgetsForm)form; // ewForm: Edit Widget Form
UsersList user = (UsersList)request.getSession().getAttribute(“userlist”);
// UserList is basically User, and it represent only one user, yea! a wrong name
ArrayList userWidgetsList = (ArrayList) userWidgetDAO.getUserWidgets(user.getId(), new Integer(1));
// Integer 1 is page Id
ArrayList widgetsList = (ArrayList) widgetDAO.getAllWidgets();
Iterator userWidgetsItr = userWidgetsList.iterator(); // These are user widgets
Iterator widgetsItr = widgetsList.iterator(); // these are all widgets in application
Integer[] idArr = new Integer[ widgetsList.size() ];
String[] titleArr = new String [ widgetsList.size() ];
Integer[] orderArr = new Integer[ widgetsList.size() ];
Boolean[] statusArr = new Boolean[ widgetsList.size() ];
int index = 0;
while(widgetsItr.hasNext()) {
Widget wgt = ( Widget )widgetsItr.next() ;
idArr[ index ] = wgt.getId() ;
titleArr[ index ] = wgt.getTitle();
orderArr[ index ] = null;
statusArr[ index ] = new Boolean( false );
userWidgetsItr = userWidgetsList.iterator();
while(userWidgetsItr.hasNext()) {
UserWidget uw = ( UserWidget ) userWidgetsItr.next();
if( wgt.getId().equals( ( Integer )uw.getWidget().getId( ) ) ) {
orderArr[ index ] = uw.getOrder( );
statusArr[ index ] = new Boolean( true );
break;
}
}
index++;
}
ewForm.setId( idArr ) ;
ewForm.setTitle( titleArr) ;
ewForm.setOrder( orderArr ) ;
ewForm.setStatus( statusArr ) ;
request.getSession().setAttribute( “userWidgetsList” , userWidgetsList );
return mapping.findForward( ApplicationConstants.SUCCESS );
}
You see, it basically populates the ActionForm for each user specifically. Very easy to understand, let me know if you need explanation.
___________________________________________________________________________________
So now come to the View ( i.e. a JSP page ). The JSP page looks like:
<html:form action=”EditHomePageWidgets.do?method=update” method=”post”>
<div id=”table”>
<logic:iterate name=”editPageWidgetsForm” id=”widget” indexId=”ctr”>
<div id=”divrow”>
<div id=”divcell”> <html:text size=”2″ readonly=”true” property= ‘<%=”id["+ ctr +"]” %>’/> </div>
<div id=”divcell”> <html:text size=”35″ readonly=”true” property= ‘<%=”title["+ ctr +"]“%>’ /> </div>
<div id=”divcell”> <html:text property= ‘<%=”order["+ ctr +"]“%>’ /> </div>
<div id=”divcell”> <html:checkbox property=’<%=”status["+ ctr +"]“%>’ /> </div>
</div>
</logic:iterate>
</div> <!– end div table –>
<html:submit> SAVE </html:submit> </html:form>
_______________________________________________________________
The update() method which is called at submit, is very easy to write. You will receive the ActionForm, and do your processing as appropriate for your application. So i am not putting it here. Let me know if you need more detail.
Let me have your feed back. Thanks!
→ 2 CommentsCategories: Java Web
Tagged: actionForm, actionForm with arrays, array, Struts